libs/web: if current language is a regional dialect, fall back to generic language, then english (e.g. pt_BR -> pt -> en)

This commit is contained in:
Jo-Philipp Wich 2009-05-28 00:24:04 +00:00
parent f20034cd82
commit 2bd86ec208

View file

@ -71,6 +71,7 @@ end
-- @param force Force reload even if already loaded (optional) -- @param force Force reload even if already loaded (optional)
function loadc(file, force) function loadc(file, force)
load(file, default, force) load(file, default, force)
if context.parent then load(file, context.parent, force) end
return load(file, context.lang, force) return load(file, context.lang, force)
end end
@ -78,6 +79,7 @@ end
-- @param lang Two-letter language code -- @param lang Two-letter language code
function setlanguage(lang) function setlanguage(lang)
context.lang = lang:gsub("_", "-") context.lang = lang:gsub("_", "-")
context.parent = (context.lang:match("^([a-z][a-z])_"))
end end
--- Return the translated value for a specific translation key. --- Return the translated value for a specific translation key.
@ -86,6 +88,7 @@ end
-- @return Translated string -- @return Translated string
function translate(key, def) function translate(key, def)
return (table[context.lang] and table[context.lang][key]) return (table[context.lang] and table[context.lang][key])
or (table[context.parent] and table[context.parent][key])
or (table[default] and table[default][key]) or (table[default] and table[default][key])
or def or def
end end