diff --git a/example_engine.lua b/example_engine.lua index 75c9bed..cc55b31 100644 --- a/example_engine.lua +++ b/example_engine.lua @@ -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" diff --git a/init.lua b/init.lua index 73aa877..edc39e1 100644 --- a/init.lua +++ b/init.lua @@ -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 }) diff --git a/utilities.lua b/utilities.lua new file mode 100644 index 0000000..643c7d8 --- /dev/null +++ b/utilities.lua @@ -0,0 +1,6 @@ +babel.sanitize = function(stringdata) + stringdata = stringdata:gsub(" ","+") + stringdata = stringdata:gsub("&", "%26") + + return stringdata +end diff --git a/yandex_engine.lua b/yandex_engine.lua index a29ca1a..698ea5c 100644 --- a/yandex_engine.lua +++ b/yandex_engine.lua @@ -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")