[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: String access & metamethods
- From: "Ken Smith" <kgsmith@...>
- Date: Wed, 12 Dec 2007 15:27:10 -0800
Here is an alternative to the excellent metamethod technique. If you
output text via other means than print, this may incur more work since
you'll have to rewrite each of the output methods separately.
#!/usr/bin/env lua
original = {}
original.print = print
translations = {
french =
{
["english phrase 1"] = "french phrase 1",
["english phrase 2"] = "french phrase 2",
},
portuguese =
{
["english phrase 1"] = "portuguese phrase 1",
["english phrase 2"] = "portuguese phrase 2",
},
spanish =
{
["english phrase 1"] = "spanish phrase 1",
["english phrase 2"] = "spanish phrase 2",
},
}
language = "portuguese"
print = function(...)
local b = {insert = table.insert}
for i,a in ipairs(arg) do
if not translations[language] then
-- unsupported language, fail gracefully
translations[language] = {}
end
b:insert(translations[language][a] or a)
end
original.print(unpack(b))
end
print("english phrase 1", "hi", "english phrase 2")
Ken Smith