From 715ac337159da1e2dca59363f612f7e1ba6654e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Mart=C3=ADnez?= Date: Thu, 6 Oct 2016 05:24:15 -0300 Subject: [PATCH] Add support for language variants/dialects. Assuming the detected language is `ll_CC`, the mod tries the following files: - `locale/ll_CC.txt` - `locale/ll.txt` - `locale/en.txt` (assuming it has not been processed already). --- init.lua | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/init.lua b/init.lua index 21d4e02..79d4c31 100644 --- a/init.lua +++ b/init.lua @@ -16,7 +16,6 @@ dofile(MP.."/lib.lua") local LANG = minetest.setting_get("language") if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end if not (LANG and (LANG ~= "")) then LANG = "en" end -LANG = LANG:sub(1, 2) local INS_CHAR = intllib.INSERTION_CHAR @@ -61,14 +60,31 @@ function intllib.Getter(modname) end -function intllib.get_strings(modname) +local function get_locales(code) + local ll, cc = code:match("^(..)_(..)") + if ll then + return { ll.."_"..cc, ll, ll~="en" and "en" or nil } + else + return { code, code~="en" and "en" or nil } + end +end + + +function intllib.get_strings(modname, langcode) + langcode = langcode or LANG modname = modname or minetest.get_current_modname() local msgstr = intllib.strings[modname] if not msgstr then local modpath = minetest.get_modpath(modname) - msgstr = intllib.load_strings(modpath.."/locale/"..LANG..".txt") + msgstr = { } + for _, l in ipairs(get_locales(langcode)) do + local t = intllib.load_strings(modpath.."/locale/"..l..".txt") or { } + for k, v in pairs(t) do + msgstr[k] = msgstr[k] or v + end + end intllib.strings[modname] = msgstr end - return msgstr or nil + return msgstr end