[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: String access & metamethods
- From: "Rici Lake" <lua@...>
- Date: Thu, 13 Dec 2007 17:10:44 -0000 (GMT)
Eric Tetz wrote:
> The real KISS approach is just to load a file like:
>
> -- localizations.lua
> if language == "German" then
> DidNotWork = "Operasie het nie geslaag nie"
> NoAnswer = "Antwoord nie gefind nie"
> elseif language == "Martian" then
> DidNotWork = "Pqfsbtjf!ifu!ojf!hftmbbh!ojf"
> NoAnswer = "Bouxppse!ojf!hfgjoe!ojf"
> else -- default to English
> DidNotWork = "Operation did not succeed"
> NoAnswer = "No answer found"
> end
A possibly simpler approach, which avoids compiling a file with all
localized strings, is:
-- l12n/german.lua
DidNotWork = "Operasie het nie geslaag nie"
NoAnswer = "Antwoord nie gefind nie"
-- l12n/martian.lua
DidNotWork = "Pqfsbtjf!ifu!ojf!hftmbbh!ojf"
NoAnswer = "Bouxppse!ojf!hfgjoe!ojf"
-- l12n/english.lua
DidNotWork = "Operation did not succeed"
NoAnswer = "No answer found"
-- yourapp.lua
pcall(require, "l12n/german") or require"l12n/english"
You could use a fallback loader to avoid the pcall.
I don't see the advantage of using globals, though. Stashing the
localizations in a table called L (with a __call metamethod) does not
use more space, and avoids namespace pollution.