eIt may sound pretty ridiculous that we're doing l10n in Javascript, but it's unavoidably required. Since Javascript plays more important role in View code in virtually all web frameworks. Rails has RJS, which basically require Javascript to perform in-place page-updating job. Jifty also use Javascript to update page fragments. Jemplate is a full javascript template library. Extjs is a full-js UI builder. In these libraries, more or less, we need to put string literals in or Javascript code.
So we've done our way localization strings in Javascript: Asynapse.Localization. It's now put in our Asynapse project Subversion repository. After it's initialized, you call this function: _, yes, a function named just an underscore character, which is basically the standard name of the function that accomplished this purpose in lightweight languages. Therefore, we think it's totally OK to named it just underscore. Besides, it doesn't conflict with prototype.js, so that won't make people sad.
It's almost as simple as this:
# Initialize
Asynapse.Localization.init({
"lang": "zh_TW",
"dict_path": "/javascripts/loc"
})
# use it
alert( _("Nihao") )
We use JSON as our dictionary format. It looks like this:
{
"Nihao": "你好",
"Good bye": "再見"
}
But wait, that doesn't feel any easier. Writing JSON by hand is actually worse then editing anything else, isn't it ? Besides, it doesn't quite fit our Asynapse concept.
As we allAsian people know, translation strings in code is something we never want to repeatedly do. So we figured out a way to make it less repeating: We choose to cover .po files to JSON.
If you're already developing some projects that has l10n support, you probably use gettext to the real l10n task. Then you definitely know what .po files are. They are the dictionaries in the format defined in gettext standard. po2json.pl is the script that convert from po dictionaries into JSON files which can be loaded by Asynapse.Localization library. That means, you're simply reusing dictionaries that you already had. If you have new strings from Javascript code, just add them manually into your po file, and they'll also be reused in server side code too.
That now feel more Asynapse, we wins at both client and server side coding.
So far this library is lying in the Subversion repository, stay tuned, we'll soon release it to several major code archiving sites once it's OK enough to be released. For developers who can't wait, just checkout our code from google svn code. Definitely poke us, give us some feed back if you like it (or hate it)
1 意見:
www.ajaxjs.com EXT中文站
jstang.5d6d.com "JS堂"Ajax论坛
張貼意見