Mailing List Archive
tlug.jp Mailing List tlug archive tlug Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]Re: [tlug] Language localization in Javacript
- Date: Fri, 30 Sep 2011 15:41:38 +0900
- From: Darren Cook <darren@example.com>
- Subject: Re: [tlug] Language localization in Javacript
- References: <CA+kCxRb8df237PV1ZdvOp5Emuqgz_=RdVzpa_fPMJEbQZRX3sA@example.com> <4E81AF97.2060001@example.com> <4E842467.1010902@example.com>
- User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.21) Gecko/20110831 Thunderbird/3.1.13
> ...Don't use indirect variable references. Let the data structure do the work: > > strings = { > english: { hi: "Hello world", bye: "Goodbye" }, > japanese: { hi: "こんにちは世界", bye: "さよなら" }, > french: { hi: "Bonjour tout le monde", bye: "Au revoir" }, > } > > var language = "english"; > > document.writeln( strings[language]["hi"] ); I'm glad I read all the posts before replying; Simon's answer above is what I was going to suggest. The ternary operator I used originally was a shortcut, guessing you only needed English and Japanese. However, I'd still have a global var: T=strings[language]; ... document.writeln( T["hi"] ); Why? Because the output lines are going to be used in hundreds of places, and typing strings[language] everywhere is tedious and distracting (and also uses more CPU cycles - though no-one would notice). Darren P.S. You don't have to put all the strings in one huge object, as in the above example. Going back to your original code: var english = new Object (); english.l00001 = "hello"; english.l00002 = "goodbye"; var japanese = new Object () japanese.l00001 = "今日は"; japanese.l00002 = "サヨナラ"; You then create the strings object (*) like this: var strings={ english:english, japanese:japanese, }; *: BTW, a Javascript object is the equivalent of PHP's associative array. P.P.S. As others have said, this is all assuming you have a good reason to have all languages statically embedded in an html page. Which it sounds like you do. -- Darren Cook, Software Researcher/Developer http://dcook.org/work/ (About me and my work) http://dcook.org/blogs.html (My blogs and articles)
- Follow-Ups:
- Re: [tlug] Language localization in Javacript
- From: Stephen J. Turnbull
- References:
- [tlug] Language localization in Javacript
- From: Martin G
- Re: [tlug] Language localization in Javacript
- From: Darren Cook
- Re: [tlug] Language localization in Javacript
- From: Simon Cozens
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] Secure messaging solutions for Linux users and others.
- Next by Date: Re: [tlug] Language localization in Javacript
- Previous by thread: Re: [tlug] Language localization in Javacript
- Next by thread: Re: [tlug] Language localization in Javacript
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links