lua-users home
lua-l archive

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


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