add string sanitization, update example

This commit is contained in:
Tai Kedzierski 2017-08-01 19:32:08 +01:00
parent ac3d6e63d3
commit c705dc3499
4 changed files with 44 additions and 16 deletions

View File

@ -1,6 +1,11 @@
-- Example Engine File
-- You can use this file as an example to base your own engine off of
-- Compliance notes
-- If the web service requires translations to be announced,
-- this will be the compliance string to be displayed.
babel.compliance = "Translations are Powered by Example.com"
-- Declare the engine name
-- This is used in displaying messages by the main library
babel.engine = "EXAMPLE"
@ -15,23 +20,36 @@ babel.langcodes = {
-- The API URL for the service
local serviceurl = "https://translations.example.com/api/v42/json?"
local httpapi
function babel.register_http(hat)
httpapi = hat
end
-- The public-facing translation function
function babel.translate(self, phrase, lang)
function babel.translate(self, phrase, lang, handler)
-- phrase : A string of the phrase to translate
-- lang : the language code to pass to the server
-- handler : a handler function to return the data to babelfish core
-- Construct the request URL
-- We sanitize both lang and phrase as they can be player-entered data
-- For example:
local transurl = serviceurl ..
"key="..babel.key.."&"..
"text="..phrase:gsub(" ","+").."&"..
"lang="..lang
"text="..babel.sanitize(phrase).."&"..
"lang="..babel.sanitize(lang)
-- make the request
local response = babel:request(transurl)
httpapi.fetch({url = transurl}, function(htresponse)
if htresponse.succeeded then
handler(extract_phrase(htresponse.data) )
else
handler("Failed request")
minetest.log("error", "Error on requesting -- "..dump(htresponse))
end
end)
-- do further processing on response, or just return it as-is
return response
end
-- Compliance notes
-- If the web service requires translations to be announced,
-- this will be the compliance string to be displayed.
babel.compliance = "Translations are Powered by Example.com"

View File

@ -9,6 +9,8 @@ babel = {}
local modpath = minetest.get_modpath("babelfish")
dofile(modpath.."/chat.lua" )
dofile(modpath.."/utilities.lua" )
local langprefs = minetest.get_worldpath().."/babel_langprefs"
local engine = minetest.setting_get("babelfish.engine") or "yandex"
babel.key = minetest.setting_get("babelfish.key")
@ -141,7 +143,7 @@ end)
local function f_babel(player, argstring)
local targetplayer = argstring
if not player_pref_language[player] then
player_pref_language[player] = "en"
player_pref_language[player] = babel.defaultlang
end
local targetlang = player_pref_language[player]
@ -163,6 +165,7 @@ local function f_babel(player, argstring)
end
local function f_babelshout(player, argstring)
-- babel equivalent of shout - broadcast translated message
local targetlang, targetphrase = components(argstring)
local validation = babel:validate_lang(targetlang)
@ -178,6 +181,7 @@ local function f_babelshout(player, argstring)
end
local function f_babelmsg(player, argstring)
-- babel equivalent of private message
local targetplayer, targetphrase = components(argstring)
local targetlang = player_pref_language[targetplayer]
@ -201,6 +205,7 @@ end
local function setplayerlanguage(tplayer, langcode)
if minetest.get_player_by_name(tplayer) then
player_pref_language[tplayer] = langcode
prefsave()
end
end
@ -216,9 +221,8 @@ minetest.register_chatcommand("bblang", {
babel.chat_send_player(player, validation)
return
else
player_pref_language[player] = args
setplayerlanguage(player, args) -- FIXME this should use the above pref functions
babel.chat_send_player(player, args.." : OK" )
prefsave()
end
end
})

6
utilities.lua Normal file
View File

@ -0,0 +1,6 @@
babel.sanitize = function(stringdata)
stringdata = stringdata:gsub(" ","+")
stringdata = stringdata:gsub("&", "%26")
return stringdata
end

View File

@ -5,6 +5,8 @@
-- in a LICENSE.txt file
-- If not, please see https://www.gnu.org/licenses/lgpl-3.0.html
babel.compliance = "Translations are Powered by Yandex.Translate"
local json = require("json")
local httpapi
@ -117,8 +119,8 @@ end
function babel.translate(self, phrase, lang, handler)
local transurl = serviceurl ..
"key="..babel.key.."&"..
"text="..phrase:gsub(" ","+").."&"..
"lang="..lang
"text="..babel.sanitize(phrase).."&"..
"lang="..babel.sanitize(lang)
httpapi.fetch({url = transurl}, function(htresponse)
if htresponse.succeeded then
@ -130,5 +132,3 @@ function babel.translate(self, phrase, lang, handler)
end)
end
babel.compliance = "Translations are Powered by Yandex.Translate"
dofile(minetest.get_modpath("babelfish").."/compliance.lua")