/* Pure Javascript implementation of Uniforum message translation. Copyright (C) 2008 Joshua I. Miller , all rights reserved This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. =head1 NAME Javascript Gettext - Javascript implemenation of GNU Gettext API. =head1 SYNOPSIS // ////////////////////////////////////////////////////////// // Optimum caching way var gt = new Gettext({ "domain" : "myDomain" }); // rest is the same // ////////////////////////////////////////////////////////// // The reson the shortcuts aren't exported by default is because they'd be // glued to the single domain you created. So, if you're adding i18n support // to some js library, you should use it as so: if (typeof(MyNamespace) == 'undefined') MyNamespace = {}; MyNamespace.MyClass = function () { var gtParms = { "domain" : 'MyNamespace_MyClass' }; this.gt = new Gettext(gtParams); return this; }; MyNamespace.MyClass.prototype._ = function (msgid) { return this.gt.gettext(msgid); }; MyNamespace.MyClass.prototype.something = function () { var myString = this._("this will get translated"); }; // ////////////////////////////////////////////////////////// // Adding the shortcuts to a global scope is easier. If that's // ok in your app, this is certainly easier. var myGettext = new Gettext({ 'domain' : 'myDomain' }); function _ (msgid) { return myGettext.gettext(msgid); } alert( _("text") ); // ////////////////////////////////////////////////////////// // Data structure of the json data // NOTE: if you're loading via the // in domain.json json_locale_data = { "mydomain" : { // po header fields "" : { "plural-forms" : "...", "lang" : "en", }, // all the msgid strings and translations "msgid" : [ "msgid_plural", "translation", "plural_translation" ], }, }; // please see the included bin/po2json script for the details on this format This method also allows you to use unsupported file formats, so long as you can parse them into the above format. =item 2. Use AJAX to load language file. Use XMLHttpRequest (actually, SJAX - syncronous) to load an external resource. Supported external formats are: =over =item * Javascript Object Notation (.json) (see bin/po2json) type=application/json =item * Uniforum Portable Object (.po) (see GNU Gettext's xgettext) type=application/x-po =item * Machine Object (compiled .po) (.mo) NOTE: .mo format isn't actually supported just yet, but support is planned. (see GNU Gettext's msgfmt) type=application/x-mo =back =back =head1 METHODS The following methods are implemented: new Gettext(args) textdomain (domain) gettext (msgid) dgettext (domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES) ngettext (msgid, msgid_plural, count) dngettext (domainname, msgid, msgid_plural, count) dcngettext (domainname, msgid, msgid_plural, count, LC_MESSAGES) pgettext (msgctxt, msgid) dpgettext (domainname, msgctxt, msgid) dcpgettext (domainname, msgctxt, msgid, LC_MESSAGES) npgettext (msgctxt, msgid, msgid_plural, count) dnpgettext (domainname, msgctxt, msgid, msgid_plural, count) dcnpgettext (domainname, msgctxt, msgid, msgid_plural, count, LC_MESSAGES) strargs (string, args_array) =head2 new Gettext (args) 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