diff --git a/docs/dev/web/i18n.rst b/docs/dev/web/i18n.rst index 1aefb2436740efc5876abbb1a201097484fda211..e93446b37ea3e1380757c37d108970c76fc2f2c2 100644 --- a/docs/dev/web/i18n.rst +++ b/docs/dev/web/i18n.rst @@ -17,10 +17,11 @@ i18n support in Gluon --------------------- Internationalization support is available in all components (models, view and -contrllers) of *gluon-web*-based packages. Strings are translated using the *translate* -and *translatef* functions (*translate* for static strings, *translatef* -for printf-like formatted string); in views, the special tags ``<%:...%>`` can -be used to translate the contained string. +contrllers) of *gluon-web*-based packages. Strings are translated using the *translate*, +*_translate* and *translatef* functions (*translate* for static strings, *translatef* +for printf-like formatted string; *_translate* works the same as *translate*, but +will return *nil* instead of the original string when no translation is available) +. In views, the special tags ``<%:...%>`` can be used to translate the contained string. Example from the *gluon-config-mode-geo-location* package: diff --git a/docs/dev/web/view.rst b/docs/dev/web/view.rst index ae25f3c1f7bca5c6751b9e3c805402c2f3e34c4b..71c7416c86d26d3568bcf7a61cc07615215d1011 100644 --- a/docs/dev/web/view.rst +++ b/docs/dev/web/view.rst @@ -52,4 +52,4 @@ variables and functions should always be available for the embedded Lua code: Use ``node(unpack(request))`` to get the node for the current page. - *pcdata* (*str*): Escapes HTML entities in the passed string. - *urlencode* (*str*): Escapes the passed string for use in an URL. - - *translate* and *translatef*: see :doc:`i18n` + - *translate*, *_translate* and *translatef*: see :doc:`i18n` diff --git a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua index 55b49ca98928ba04d8f1d42fe2c8ef8addc138d3..1a92e5767c1eb72db49418fd717d57462aa63485 100644 --- a/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua +++ b/package/gluon-web/luasrc/usr/lib/lua/gluon/web/template.lua @@ -25,6 +25,7 @@ function renderer(env) renderer = ctx, translate = ctx.translate, translatef = ctx.translatef, + _translate = ctx._translate, include = function(name) ctx.render(name, scope) end, @@ -79,13 +80,19 @@ function renderer(env) return tparser.load_catalog(lang, i18ndir) end + -- Returns a translated string, or nil if none is found + function ctx._translate(key) + return (tparser.translate(key)) + end + + -- Returns a translated string, or the original string if none is found function ctx.translate(key) return tparser.translate(key) or key end function ctx.translatef(key, ...) local t = ctx.translate(key) - return t and t:format(...) + return t:format(...) end return ctx