lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

You can also set the translators table to auto-populate by giving it an appropriate __index metamethod.



module("translators", package.seeall)

local translators = {}
setmetatable(translators, {__mode = "v", __index = function( t, k ) local v = { }; t[ k ] = v; return v; end })
local mt = {}

-- If there is not a suitable translator (for the given key and language),
-- create a new one and store it in a weak-valued table so it does not
-- linger there for ever.
mt.__index = function(tab, key)
local user = inconcert.authUser or {languageId=14346, codepage="UTF-8"}
  local translator = translators[key][user.languageId]
  if not translator then
translator = i18nlib.Translator{domain=key, language = user.languageId, codepage = user.codepage, transliterate=false}
      translators[key][user.languageId] = translator
  return translator

setmetatable(_M, mt)