bungle / lua-resty-template

Templating Engine (HTML) for Lua and OpenResty.
BSD 3-Clause "New" or "Revised" License
909 stars 204 forks source link

i18 / Localization #39

Open markschmid opened 4 years ago

markschmid commented 4 years ago

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 commented 4 years ago

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

bungle commented 4 years ago

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

bungle commented 4 years ago

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

markschmid commented 3 years ago

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 commented 3 years ago

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.

craveica commented 3 years ago

@markschmid try this:

template.render("template.html", {
  content = "my content",
  i18n = i18n,
}
markschmid commented 3 years ago

@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.

craveica commented 3 years ago

I see, what about this:

{% local translated = i18n.translate('my.translated.text') %}{{translated}}
craveica commented 3 years ago

or you ca do:

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

markschmid commented 3 years ago

@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 commented 3 years ago

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!