2008-03-02 21:52:58 +00:00
|
|
|
--[[
|
2008-05-25 17:00:30 +00:00
|
|
|
LuCI - Internationalisation
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
Description:
|
|
|
|
A very minimalistic but yet effective internationalisation module
|
|
|
|
|
|
|
|
FileId:
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
License:
|
|
|
|
Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
2008-10-05 16:08:33 +00:00
|
|
|
You may obtain a copy of the License at
|
2008-03-02 21:52:58 +00:00
|
|
|
|
2008-10-05 16:08:33 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- LuCI translation library.
|
2008-05-25 17:00:30 +00:00
|
|
|
module("luci.i18n", package.seeall)
|
2008-08-06 20:20:40 +00:00
|
|
|
require("luci.util")
|
2012-11-25 19:17:55 +00:00
|
|
|
|
|
|
|
local tparser = require "luci.template.parser"
|
2008-03-02 21:52:58 +00:00
|
|
|
|
|
|
|
table = {}
|
2008-08-06 20:20:40 +00:00
|
|
|
i18ndir = luci.util.libpath() .. "/i18n/"
|
2008-05-31 13:57:30 +00:00
|
|
|
loaded = {}
|
2008-06-14 14:12:12 +00:00
|
|
|
context = luci.util.threadlocal()
|
|
|
|
default = "en"
|
2008-03-02 21:52:58 +00:00
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Clear the translation table.
|
2008-03-02 21:52:58 +00:00
|
|
|
function clear()
|
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Load a translation and copy its data into the translation table.
|
|
|
|
-- @param file Language file
|
|
|
|
-- @param lang Two-letter language code
|
|
|
|
-- @param force Force reload even if already loaded (optional)
|
|
|
|
-- @return Success status
|
2008-06-14 14:12:12 +00:00
|
|
|
function load(file, lang, force)
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Load a translation file using the default translation language.
|
|
|
|
-- Alternatively load the translation of the fallback language.
|
|
|
|
-- @param file Language file
|
|
|
|
-- @param force Force reload even if already loaded (optional)
|
2008-06-01 12:12:18 +00:00
|
|
|
function loadc(file, force)
|
2008-06-14 14:12:12 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Set the context default translation language.
|
|
|
|
-- @param lang Two-letter language code
|
2008-06-14 14:12:12 +00:00
|
|
|
function setlanguage(lang)
|
2009-05-28 00:24:04 +00:00
|
|
|
context.lang = lang:gsub("_", "-")
|
|
|
|
context.parent = (context.lang:match("^([a-z][a-z])_"))
|
2012-11-25 19:17:55 +00:00
|
|
|
if not tparser.load_catalog(context.lang, i18ndir) then
|
|
|
|
if context.parent then
|
|
|
|
tparser.load_catalog(context.parent, i18ndir)
|
2012-12-02 13:30:46 +00:00
|
|
|
return context.parent
|
2012-11-25 19:17:55 +00:00
|
|
|
end
|
|
|
|
end
|
2012-12-02 13:30:46 +00:00
|
|
|
return context.lang
|
2008-03-02 21:52:58 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Return the translated value for a specific translation key.
|
2009-10-31 15:42:07 +00:00
|
|
|
-- @param key Default translation text
|
2008-07-29 20:32:02 +00:00
|
|
|
-- @return Translated string
|
2009-10-31 15:42:07 +00:00
|
|
|
function translate(key)
|
2012-11-25 19:17:55 +00:00
|
|
|
return tparser.translate(key) or key
|
2008-05-25 15:55:39 +00:00
|
|
|
end
|
|
|
|
|
2008-07-29 20:32:02 +00:00
|
|
|
--- Return the translated value for a specific translation key and use it as sprintf pattern.
|
2009-10-31 15:42:07 +00:00
|
|
|
-- @param key Default translation text
|
2008-07-29 20:32:02 +00:00
|
|
|
-- @param ... Format parameters
|
|
|
|
-- @return Translated and formatted string
|
2009-10-31 15:42:07 +00:00
|
|
|
function translatef(key, ...)
|
|
|
|
return tostring(translate(key)):format(...)
|
2008-10-05 16:08:33 +00:00
|
|
|
end
|
2009-07-26 22:48:09 +00:00
|
|
|
|
|
|
|
--- 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 <code>tostring(translate(...))</code>
|
2009-10-31 15:42:07 +00:00
|
|
|
-- @param key Default translation text
|
2009-07-26 22:48:09 +00:00
|
|
|
-- @return Translated string
|
2009-10-31 15:42:07 +00:00
|
|
|
function string(key)
|
|
|
|
return tostring(translate(key))
|
2009-07-26 22:48:09 +00:00
|
|
|
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 <code>tostring(translatef(...))</code>
|
2009-10-31 15:42:07 +00:00
|
|
|
-- @param key Default translation text
|
2009-07-26 22:48:09 +00:00
|
|
|
-- @param ... Format parameters
|
|
|
|
-- @return Translated and formatted string
|
2009-10-31 15:42:07 +00:00
|
|
|
function stringf(key, ...)
|
|
|
|
return tostring(translate(key)):format(...)
|
2009-07-26 22:48:09 +00:00
|
|
|
end
|