diff options
author | Ekdohibs <nathanael.courant@laposte.net> | 2017-01-31 18:05:03 +0100 |
---|---|---|
committer | Ekdohibs <nathanael.courant@laposte.net> | 2017-08-24 17:54:10 +0200 |
commit | b24e6433df3c3b2926568aff9c0173459e3e8eab (patch) | |
tree | eec6a9f05e78e3de7b08c805685cd54dcc5e43de /builtin/common | |
parent | b28af0ed0777f66122ecaf0d0e302fe24c88d552 (diff) | |
download | minetest-b24e6433df3c3b2926568aff9c0173459e3e8eab.tar.gz minetest-b24e6433df3c3b2926568aff9c0173459e3e8eab.tar.bz2 minetest-b24e6433df3c3b2926568aff9c0173459e3e8eab.zip |
Add clientside translations.
Diffstat (limited to 'builtin/common')
-rw-r--r-- | builtin/common/misc_helpers.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index 776be29d8..67eda11f3 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -680,6 +680,44 @@ function core.strip_colors(str) return (str:gsub(ESCAPE_CHAR .. "%([bc]@[^)]+%)", "")) end +function core.translate(textdomain, str, ...) + local start_seq + if textdomain == "" then + start_seq = ESCAPE_CHAR .. "T" + else + start_seq = ESCAPE_CHAR .. "(T@" .. textdomain .. ")" + end + local arg = {n=select('#', ...), ...} + local end_seq = ESCAPE_CHAR .. "E" + local arg_index = 1 + local translated = str:gsub("@(.)", function(matched) + local c = string.byte(matched) + if string.byte("1") <= c and c <= string.byte("9") then + local a = c - string.byte("0") + if a ~= arg_index then + error("Escape sequences in string given to core.translate " .. + "are not in the correct order: got @" .. matched .. + "but expected @" .. tostring(arg_index)) + end + if a > arg.n then + error("Not enough arguments provided to core.translate") + end + arg_index = arg_index + 1 + return ESCAPE_CHAR .. "F" .. arg[a] .. ESCAPE_CHAR .. "E" + else + return matched + end + end) + if arg_index < arg.n + 1 then + error("Too many arguments provided to core.translate") + end + return start_seq .. translated .. end_seq +end + +function core.get_translator(textdomain) + return function(str, ...) return core.translate(textdomain or "", str, ...) end +end + -------------------------------------------------------------------------------- -- Returns the exact coordinate of a pointed surface -------------------------------------------------------------------------------- |