-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_engine.lua
52 lines (43 loc) · 1.46 KB
/
example_engine.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- 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"
-- Declare the valid language codes
-- This can be hardcoded or dynamically built
babel.langcodes = {
en = "English",
gd = "Scottish",
}
-- 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(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="..babel.sanitize(phrase).."&"..
"lang="..babel.sanitize(lang)
-- make the request
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)
end