lua-users home
lua-l archive

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


One era ago I wrote an appender for lualogging for localization, with some trickery from sir Rici, which ended up being quite useful.

http://giger.servebeer.com/pics/Development/Lua/localizedAppender.zip

Sample follows:
----

   require "logging.localized"
   localizer = logging.localized( 'dictionary.swedish', logging.null() )
   print( localizedLog:info( { id='SomeIdentifier', SomeParam=1986 } ) )

and dictionary.swedish would look like this:


   module( ... )
   messages = {}
messages.WhatHappenedThen = "På våren '${SomeParam}' brann mina termobyxor upp på en åker."


Brett Kugler wrote:
I've been using Lua for a while now, but really have kept things fairly simple as far as some of the more 'interesting' Lua features.

I'm now attempting to implement localization to my Lua apps and thought this would be a good time to look into using metamethods, as the solution I've thought up seems to call for them. Unfortunately, I've never used them and what's more, I'm not sure what I'm attempting can be accomplished even with that powerful tool in my pocket.

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):

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"

function(in_string)
    if t[in_string][language] ~= nil then
        return t[in_string][language]
    else
        return in_string
end

So now the question. How do I make the string access in print("english1") trigger the metamethod of a string and which metamethod would that be? Since I also want this to be generic, it really should work across any string access at any point in the execution. What I mean by this is that the triggering of the lookup function should be tied to the string literal, not the function using it ( foo("english1") should also return the result of the lookup function. How about statements like a_word="english1"?

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?

Thanks for any advice in advance,
Brett