Mailing List Archive


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

Re: [tlug] Language localization in Javacript



I really didn't want to get involved in this thread but yikes, guys,
there *is* a better way.

Martin G writes:

 > How would I set up a conditional for three or more possible
 > languages?

Don't.  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?  Generate the
Javascript, too!

The something-resembling-a-modern-language probably supports GNU
gettext (if it doesn't, switch to Python), which in Python 2 would be
used in a CGI script that looks like this:

# I probably wouldn't do it this way in a real app, but I don't feel
# like thinking about a real app right now.
# start writhing
def generate_javascript ():
    print "document.writeln("
    # Watch for it, here it comes!
    print _("Hello.")
    # See it?  Hint: it's just lying down; it's not dead.
    print ");"

setlocale("ja")
generate_javascript()
# done writhing

which you run through xgettext to get a file like this:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@example.com>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-09-28 18:00+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@example.com>\n"
"Language-Team: LANGUAGE <LL@example.com>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: js.py:4
msgid "Hello."
msgstr "こんにちは。"
        A
        +----------------fill in dis here blank with language du
          jour and save as "$language.mo".

There's more to it than that, of course, but basically unless you
really are writing "hello world" and no more, for two languages and no
more, the pain of maintaining translations is extremely high.  gettext
reduces it to a managable level.  It's worth learning how to use it.




Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links