nodecore-cd2025/mods/nc_api/util_translate.lua
Aaron Suen 28108e1e40 Add translation support, esp. for TouchTips.
Translations are currently by holistic strings, i.e.
no parameterization.  This may create some busy
work for translators, but gives them more freedom
to account for differences between languages.

A translation template file is written out to the world
path on game start, so translators have a seed to
work from.
2019-08-23 20:40:33 -04:00

46 lines
1.2 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local ipairs, minetest, nodecore, pairs, table
= ipairs, minetest, nodecore, pairs, table
local table_sort
= table.sort
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local strings = {}
local strings_dirty
local token = "NodeCore"
local prefix = minetest.translate(modname, token)
prefix = prefix:sub(1, prefix:find(token) - 1)
function nodecore.translate(str, ...)
if (not str) or (type(str) ~= "string") or (#str < 1)
or (str:sub(1, #prefix) == prefix) then return str end
if not strings[str] then
strings[str] = true
strings_dirty = true
end
return minetest.translate(modname, str, ...)
end
minetest.register_globalstep(function()
if not strings_dirty then return end
strings_dirty = nil
local keys = {}
for k, v in pairs(strings) do keys[#keys + 1] = k end
table_sort(keys)
data = "# textdomain: " .. modname .. "\n"
for _, k in ipairs(keys) do
k = k:gsub("@", "@@"):gsub("=", "@="):gsub("\n", "@n")
data = data .. k .. "=" .. "\n"
end
local p = minetest.get_worldpath() .. "/" .. modname .. ".template.tr"
return minetest.safe_file_write(p, data)
end)