40 lines
891 B
Plaintext

<?
local defaultLang = "en-us"
lang = string.lower(GET("lang") or defaultLang)
-- TODO: change to something modular, to also work with other paths
local i18nPath = "/var/www/html/admin/i18n"
local defaultFile = io.open(string.format('%s/en-us.ini', i18nPath), "r")
local file = io.open(string.format('%s/%s.ini', i18nPath, lang), "r")
local function trim(s)
return s:match("^%s*(.-)%s*$")
end
local function closeFiles()
defaultFile:close()
if file then
file:close()
end
end
local translations = {}
local iniFile = file or defaultFile
for line in iniFile:lines() do
local key, value = line:match("^(.-)=(.-)$")
if key and value then
translations[trim(key)] = trim(value)
end
end
closeFiles()
function i18n(key)
return translations[key] or ("[" .. key .. "]")
end
function i18ns(key, ...)
return string.format(i18n(key), ...)
end
?>