lua-users home
lua-l archive

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


Brett Kugler wrote:
> So here's the skinny.  I would like every literal string reference
> the VM encounters to be compared to a table of localized strings and
> return either the localized version, or the original string if it's
> not in the table.  Here's a quick code snippet (Lua 5.1):   

You can't do that in pure Lua, because in some cases no metamethod is
involved (see below).

> t={}
> t["english1"]={"english1","spanish1","french1"}
> t["english2"]={"english2","spanish2","french2"}
> t["english3"]={"english3","spanish3","french3"}
> 
> language=2 -- Spanish
> 
> print("english1")    <- spanish1
> a_word="english2"  <-  a_word="spanish2"

Here you have a simple assignation to a variable. If that variable is
local, no metamethod is triggered. If it's global, it's highly dependent
on how the environment is set up.

> I realize I could simplify my life with a little extra notation, but
> the hope was to really not change my existing code at all and have
> the string literals be reinterpreted based on a new metamethod
> instead.  Am I grasping at straws?   

The extra notation can be as small as a single character per string.
Here is an example:

t={}
t["english1"]={"english1","spanish1","french1"}
t["english2"]={"english2","spanish2","french2"}
t["english3"]={"english3","spanish3","french3"}
language = 2

L = function(english)
   return t[english][language]
end

print(L"english1")   --> spanish1, note the L prefix
a_word = L"english2" --> a_word == "spanish2"
print(a_word)        --> spanish2