Mailing List Archive


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

Re: [tlug] Language localization in Javacript



Martin G writes:

 > > Surely your Javascript is hosted on something resembling a
 > > server with something sorta like a modern language being used to
 > > generate something more or less substitutable for well-styled HTML as
 > > well as serving the Javascript and other content, right?
 > 
 > Nope.
 > 
 > It's a self contained application that one downloads, and it only ever
 > communicates with a server by getting some JSON data now and again.

So what?  These are hard-coded strings either way.  If you generate
the appropriate Javascript on the fly, what will happen is that the
user will download a *localized* self-contained application, which
will then proceed to get some JSON data now and again.  All that
happens with this approach is that people don't download strings they
don't need.

OK, so if you've got somebody using an iPad that they share with
somebody who prefers a different language, they might prefer an
*internationalized* self-contained application with downloadable
language packs.  Even a three-toed treefrog has enough fingers on one
hand to count the number of people who share their PIM with somebody
who prefers a different language (at least in this country; in theory
people might share PIMs in rural Bangladesh, I suppose).

And if you *really do* need the language packs ....  Eg, if the
application is actually a dictionary or translation tool, you can
still use this approach with gettext by switching languages in the
generating process at appropriate times.  You might end up by
generating the switch statements:

languages = ['en', 'de', 'fr', 'es', 'pt', 'ru', 'ja']
def generate_javascript():
    print 'switch (navigator.language.substr(0, 2)) {'
    for lang in languages:
        setlocale(lang)        # Here's what tells Python which
                               # translation you want.
        print '  case "%s" :' % lang
        print '    document.writeln ("%s");' % _('Hello.')
        print '    break;'
    print '}'

Can we spell "scalability," children?  Excellent!  I knew you could!

You can also, for example, allow the user to select only the languages
they want by treating 'languages' as a user variable rather than a
constant.  (I can't think of a real application for that off hand in
this day and age of 32GB memory sticks, but hey maybe you're
distributing the Encyclopedia Takokusekika ....)


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links