add string sanitization, update example
This commit is contained in:
parent
ac3d6e63d3
commit
c705dc3499
@ -1,6 +1,11 @@
|
|||||||
-- Example Engine File
|
-- Example Engine File
|
||||||
-- You can use this file as an example to base your own engine off of
|
-- 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
|
-- Declare the engine name
|
||||||
-- This is used in displaying messages by the main library
|
-- This is used in displaying messages by the main library
|
||||||
babel.engine = "EXAMPLE"
|
babel.engine = "EXAMPLE"
|
||||||
@ -15,23 +20,36 @@ babel.langcodes = {
|
|||||||
-- The API URL for the service
|
-- The API URL for the service
|
||||||
local serviceurl = "https://translations.example.com/api/v42/json?"
|
local serviceurl = "https://translations.example.com/api/v42/json?"
|
||||||
|
|
||||||
|
local httpapi
|
||||||
|
|
||||||
|
function babel.register_http(hat)
|
||||||
|
httpapi = hat
|
||||||
|
end
|
||||||
|
|
||||||
-- The public-facing translation function
|
-- 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
|
-- Construct the request URL
|
||||||
|
-- We sanitize both lang and phrase as they can be player-entered data
|
||||||
|
-- For example:
|
||||||
local transurl = serviceurl ..
|
local transurl = serviceurl ..
|
||||||
"key="..babel.key.."&"..
|
"key="..babel.key.."&"..
|
||||||
"text="..phrase:gsub(" ","+").."&"..
|
"text="..babel.sanitize(phrase).."&"..
|
||||||
"lang="..lang
|
"lang="..babel.sanitize(lang)
|
||||||
|
|
||||||
-- make the request
|
-- 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
|
-- do further processing on response, or just return it as-is
|
||||||
return response
|
return response
|
||||||
end
|
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"
|
|
||||||
|
10
init.lua
10
init.lua
@ -9,6 +9,8 @@ babel = {}
|
|||||||
|
|
||||||
local modpath = minetest.get_modpath("babelfish")
|
local modpath = minetest.get_modpath("babelfish")
|
||||||
dofile(modpath.."/chat.lua" )
|
dofile(modpath.."/chat.lua" )
|
||||||
|
dofile(modpath.."/utilities.lua" )
|
||||||
|
|
||||||
local langprefs = minetest.get_worldpath().."/babel_langprefs"
|
local langprefs = minetest.get_worldpath().."/babel_langprefs"
|
||||||
local engine = minetest.setting_get("babelfish.engine") or "yandex"
|
local engine = minetest.setting_get("babelfish.engine") or "yandex"
|
||||||
babel.key = minetest.setting_get("babelfish.key")
|
babel.key = minetest.setting_get("babelfish.key")
|
||||||
@ -141,7 +143,7 @@ end)
|
|||||||
local function f_babel(player, argstring)
|
local function f_babel(player, argstring)
|
||||||
local targetplayer = argstring
|
local targetplayer = argstring
|
||||||
if not player_pref_language[player] then
|
if not player_pref_language[player] then
|
||||||
player_pref_language[player] = "en"
|
player_pref_language[player] = babel.defaultlang
|
||||||
end
|
end
|
||||||
|
|
||||||
local targetlang = player_pref_language[player]
|
local targetlang = player_pref_language[player]
|
||||||
@ -163,6 +165,7 @@ local function f_babel(player, argstring)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function f_babelshout(player, argstring)
|
local function f_babelshout(player, argstring)
|
||||||
|
-- babel equivalent of shout - broadcast translated message
|
||||||
local targetlang, targetphrase = components(argstring)
|
local targetlang, targetphrase = components(argstring)
|
||||||
|
|
||||||
local validation = babel:validate_lang(targetlang)
|
local validation = babel:validate_lang(targetlang)
|
||||||
@ -178,6 +181,7 @@ local function f_babelshout(player, argstring)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function f_babelmsg(player, argstring)
|
local function f_babelmsg(player, argstring)
|
||||||
|
-- babel equivalent of private message
|
||||||
local targetplayer, targetphrase = components(argstring)
|
local targetplayer, targetphrase = components(argstring)
|
||||||
local targetlang = player_pref_language[targetplayer]
|
local targetlang = player_pref_language[targetplayer]
|
||||||
|
|
||||||
@ -201,6 +205,7 @@ end
|
|||||||
local function setplayerlanguage(tplayer, langcode)
|
local function setplayerlanguage(tplayer, langcode)
|
||||||
if minetest.get_player_by_name(tplayer) then
|
if minetest.get_player_by_name(tplayer) then
|
||||||
player_pref_language[tplayer] = langcode
|
player_pref_language[tplayer] = langcode
|
||||||
|
prefsave()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -216,9 +221,8 @@ minetest.register_chatcommand("bblang", {
|
|||||||
babel.chat_send_player(player, validation)
|
babel.chat_send_player(player, validation)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
player_pref_language[player] = args
|
setplayerlanguage(player, args) -- FIXME this should use the above pref functions
|
||||||
babel.chat_send_player(player, args.." : OK" )
|
babel.chat_send_player(player, args.." : OK" )
|
||||||
prefsave()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
6
utilities.lua
Normal file
6
utilities.lua
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
babel.sanitize = function(stringdata)
|
||||||
|
stringdata = stringdata:gsub(" ","+")
|
||||||
|
stringdata = stringdata:gsub("&", "%26")
|
||||||
|
|
||||||
|
return stringdata
|
||||||
|
end
|
@ -5,6 +5,8 @@
|
|||||||
-- in a LICENSE.txt file
|
-- in a LICENSE.txt file
|
||||||
-- If not, please see https://www.gnu.org/licenses/lgpl-3.0.html
|
-- 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 json = require("json")
|
||||||
|
|
||||||
local httpapi
|
local httpapi
|
||||||
@ -117,8 +119,8 @@ end
|
|||||||
function babel.translate(self, phrase, lang, handler)
|
function babel.translate(self, phrase, lang, handler)
|
||||||
local transurl = serviceurl ..
|
local transurl = serviceurl ..
|
||||||
"key="..babel.key.."&"..
|
"key="..babel.key.."&"..
|
||||||
"text="..phrase:gsub(" ","+").."&"..
|
"text="..babel.sanitize(phrase).."&"..
|
||||||
"lang="..lang
|
"lang="..babel.sanitize(lang)
|
||||||
|
|
||||||
httpapi.fetch({url = transurl}, function(htresponse)
|
httpapi.fetch({url = transurl}, function(htresponse)
|
||||||
if htresponse.succeeded then
|
if htresponse.succeeded then
|
||||||
@ -130,5 +132,3 @@ function babel.translate(self, phrase, lang, handler)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
babel.compliance = "Translations are Powered by Yandex.Translate"
|
|
||||||
dofile(minetest.get_modpath("babelfish").."/compliance.lua")
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user