X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fi18n.lua;h=816d90310af692298b830b6cbbd8be47ebc1eaae;hp=091542f8605908b32f0582c51cfdaad6bb03dbed;hb=f68906c20fa82065196d37e243c5819f7ff18ea9;hpb=96bc583cfcca29e114897f51153ee82422a4a1a4 diff --git a/libs/web/luasrc/i18n.lua b/libs/web/luasrc/i18n.lua index 091542f86..816d90310 100644 --- a/libs/web/luasrc/i18n.lua +++ b/libs/web/luasrc/i18n.lua @@ -93,21 +93,38 @@ function setlanguage(lang) end --- Return the translated value for a specific translation key. --- @param key Translation key --- @param def Default translation +-- @param key Default translation text -- @return Translated string -function translate(key, def) +function translate(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 def + or key end --- Return the translated value for a specific translation key and use it as sprintf pattern. --- @param key Translation key --- @param default Default translation +-- @param key Default translation text -- @param ... Format parameters -- @return Translated and formatted string -function translatef(key, default, ...) - return tostring(translate(key, default)):format(...) +function translatef(key, ...) + return tostring(translate(key)):format(...) +end + +--- Return the translated value for a specific translation key +-- and ensure that the returned value is a Lua string value. +-- This is the same as calling tostring(translate(...)) +-- @param key Default translation text +-- @return Translated string +function string(key) + return tostring(translate(key)) +end + +--- Return the translated value for a specific translation key and use it as sprintf pattern. +-- Ensure that the returned value is a Lua string value. +-- This is the same as calling tostring(translatef(...)) +-- @param key Default translation text +-- @param ... Format parameters +-- @return Translated and formatted string +function stringf(key, ...) + return tostring(translate(key)):format(...) end