This is a javascript implementation of GNU Gettext, providing internationalization support for javascript. It differs from existing javascript implementations in that it will support all current Gettext features (ex. plural and context support), and will also support loading language catalogs from .mo, .po, or preprocessed json files (converter included).
The locale initialization differs from that of GNU Gettext / POSIX. Rather than setting the category, domain, and paths, and letting the libs find the right file, you must explicitly load the file at some point. The "domain" will still be honored. Future versions may be expanded to include support for set_locale like features.
=head1 INSTALL
To install this module, simply copy the file lib/Gettext.js to a web accessable location, and reference it from your application.
=head1 CONFIGURATION
Configure in one of two ways:
=over
=item 1. Optimal. Load language definition from statically defined json data.
Several methods of loading locale data are included. You may specify a plugin or alternative method of loading data by passing the data in as the "locale_data" option. For example:
var get_locale_data = function () {
// plugin does whatever to populate locale_data
return locale_data;
};
var gt = new Gettext( 'domain' : 'messages',
'locale_data' : get_locale_data() );
The above can also be used if locale data is specified in a statically included <SCRIPT> tag. Just specify the variable name in the call to new. Ex:
var gt = new Gettext( 'domain' : 'messages',
'locale_data' : json_locale_data_variable );
Finally, you may load the locale data by referencing it in a <LINK> tag. Simply exclude the 'locale_data' option, and all <LINK rel="gettext" ...> items will be tried. The <LINK> should be specified as:
Loaded locale data is currently cached class-wide. This means that if two scripts are both using Gettext.js, and both share the same gettext domain, that domain will only be loaded once. This will allow you to grab a new object many times from different places, utilize the same domain, and share a single translation file. The downside is that a domain won't be RE-loaded if a new object is instantiated on a domain that had already been instantiated.
=back
=head1 BUGS / TODO
=over
=item error handling
Currently, there are several places that throw errors. In GNU Gettext, there are no fatal errors, which allows text to still be displayed regardless of how broken the environment becomes. We should evaluate and determine where we want to stand on that issue.
=item syncronous only support (no ajax support)
Currently, fetching language data is done purely syncronous, which means the page will halt while those files are fetched/loaded.
This is often what you want, as then following translation requests will actually be translated. However, if all your calls are done dynamically (ie. error handling only or something), loading in the background may be more adventagous.
It's still recommended to use the statically defined <script ...> method, which should have the same delay, but it will cache the result.
=item domain support
domain support while using shortcut methods like C<_('string')> or C<i18n('string')>.
Under normal apps, the domain is usually set globally to the app, and a single language file is used. Under javascript, you may have multiple libraries or applications needing translation support, but the namespace is essentially global.
It's recommended that your app initialize it's own shortcut with it's own domain. (See examples/wrapper/i18n.js for an example.)
Basically, you'll want to accomplish something like this:
// in some other .js file that needs i18n
this.i18nObj = new i18n;
this.i18n = this.i18nObj.init('domain');
// do translation
alert( this.i18n("string") );
If you use this raw Gettext object, then this is all handled for you, as you have your own object then, and will be calling C<myGettextObject.gettext('string')> and such.
=item encoding
May want to add encoding/reencoding stuff. See GNU iconv, or the perl module Locale::Recode from libintl-perl.
=back
=head1 COMPATABILITY
This has been tested on the following browsers. It may work on others, but these are all those to which I have access.