lua-resty-template icon indicating copy to clipboard operation
lua-resty-template copied to clipboard

i18 / Localization

Open markschmid opened this issue 5 years ago • 11 comments

I've started using this library and I like it. I need a mechanism to support multiple languages (as in i18n/localization). I've done some initial googling but have not found pointers to how to do that so far. Neither with this library nor e.g. in the awesome resty library collection. Any ideas/pointers would be highly appreciated.

markschmid avatar Jul 14 '20 19:07 markschmid

OK did some more searching. It's probably easiest to do it "externally" using something like https://github.com/kikito/i18n.lua

markschmid avatar Jul 19 '20 12:07 markschmid

you could also use something like gettext, and perhaps generate language templates with it. Then on runtime load language specific template.

bungle avatar Jul 19 '20 19:07 bungle

I have even created LuaJIT FFI bindings to it: https://github.com/bungle/lua-resty-gettext/blob/master/lib/resty/gettext.lua

bungle avatar Jul 19 '20 19:07 bungle

I'm trying to make use https://github.com/kikito/i18n.lua from lua-resty-template. Here's what I'd like to achieve:

local template = require("resty.template")
local i18n = require("resty.i18n")
--[ load strings, set language etc. in i18n]
template.render("template.html", {
  content = "my content"
}

And in template.html:

{% i18n.translate('my.translated.text') %}

I've tried a few things but they didn't work so far. Checked the docs but did not find a solution so far. Any suggestions appreciated, thanks.

markschmid avatar Nov 14 '20 12:11 markschmid

Addition: I think on a more general level it boils down to the question of how I can make use of an external library from within a lua-resty-template template.

markschmid avatar Nov 14 '20 12:11 markschmid

@markschmid try this:

template.render("template.html", {
  content = "my content",
  i18n = i18n,
}

craveica avatar Nov 14 '20 13:11 craveica

@craveica Thanks for your reponse. I've actually tried it, but it does not work. It might be because only strings and table datatypes are supported. I've also tried the example with the underscore _ from the docs, but to no avail so far.

markschmid avatar Nov 14 '20 13:11 markschmid

I see, what about this:

{% local translated = i18n.translate('my.translated.text') %}{{translated}}

craveica avatar Nov 14 '20 14:11 craveica

or you ca do:

my_translated_text = i18n.translate('my.translated.text') and use {{my_translated_text}} in template

craveica avatar Nov 14 '20 14:11 craveica

@craveica your first recommendation actually works! i just have to make i18n a non-global variable. thanks! i'll start from this for the moment and maybe come time I find a shorter solution.

Your second solution has not been an option for me in the first place because that way i'd need to manage every piece of text in 2 places (in the code and the template) which would be very cumbersome.

markschmid avatar Nov 14 '20 15:11 markschmid

OK, so for reference and if it helps anyone, here's the gist:

thecode.lua:

local template = require("resty.template")
i18n = require("resty.i18n") -- needs to be non-local
--[ load strings, set language etc. in i18n]
template.render("template.html", {
  other = "my other stuff that is not localized"
}

and in template.html:

{% echo(i18n.translate('my.text')) %}

Thanks for the help provided!

markschmid avatar Nov 14 '20 15:11 markschmid