[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: String access & metamethods
- From: "Brett Kugler" <bkugler@...>
- Date: Wed, 12 Dec 2007 13:36:26 -0600
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
language=2 -- Spanish
print("english1") <- spanish1
a_word="english2" <- a_word="spanish2"
if t[in_string][language] ~= nil then
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,