leafo / lapis

A web framework for Lua and OpenResty written in MoonScript
http://leafo.net/lapis/
MIT License
3.14k stars 247 forks source link

[feature suggestion] translations #750

Closed aergo20 closed 2 years ago

aergo20 commented 2 years ago

Hi @leafo! Congrats for this great web framework. I have a feature suggestion: An i18n (translation) module for Lapis.

Example:

--app.lua
local lapis = require("lapis")

local app = lapis.Application()
app:enable("etlua")

app:enable("i18n")

app:get("/", function(self)
  return {
    render = "list",
    language = "es" -- pass true to auto detect (accept-language header).
    -- a list of languages can be used for fallbacks e.g. {"pt-br", "pt"}
   }
end)

return app

--views/list.etlua
<%=i18n("Hello World!")%>
<%=i18n("%i item", 10)%> (the i18n uses string.format if extra args)
<%=i18n("not found")%> (not found keys returns itself)
My current language is <%=i18n.language%> (<%=i18n.detected and "auto detected" or "selected by user"%>)

--translations/es.lua
return {
  ["Hello World!"] = "Hola Mundo",

  ["%i item"] = {"%i item", "%i items"}, -- %i is 1 returns [1], else returns [2] (singular/plural simple)
  ["%i item"] = {"%i item", "%i item", "%i items"}, -- %i is 0 returns [1], %i is 1 returns [2], else returns [3] (zero/singular/plural)

-- see https://cldr.unicode.org/index/cldr-spec/plural-rules 
-- see https://github.com/kikito/i18n.lua#pluralization

}
aergo20 commented 2 years ago

Moved to discussion/ideas