[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Is this possible? (garbage collect and weak tables)
- From: Mark Hamburg <mark@...>
- Date: Sun, 8 Nov 2009 10:31:36 -0800
You can also set the translators table to auto-populate by giving it
an appropriate __index metamethod.
Mark
----
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
end
return translator
end
setmetatable(_M, mt)