Koha/koha-tmpl/opac-tmpl/bootstrap/js/i18n.js
Julian Maurice 9d6ec5c64b
Bug 21156: Add plural translation capabilities to JS files
It adds Javascript equivalent of Koha::I18N's exported subroutines, and
they are used the same way.

String extraction is done only on *.js files and require gettext 0.19
(available in Debian jessie, and also in wheezy-backports)

It adds Javascript library Gettext.js for handling translation and a
Perl script po2json to transform PO file into JSON.

Gettext.js and po2json both come from Locale::Simple.
There are several tools named po2json. It's simpler to integrate this
one into Koha than to check if the good one is installed on the system.
Locale::Simple is not needed.

To avoid polluting the global namespace too much, this patch also
introduce a global JS object named Koha and add some stuff in Koha.i18n

Test plan:
1. Add a translatable string in a JS file. For example, add this:
     alert(__nx("There is one item", "There are {count} items", 3,
     {count: 3}));
   to staff-global.js
2. cd misc/translator && ./translate update fr-FR
3. Open misc/translator/po/fr-FR-messages-js.po, verify that your
   string is present, and translate it
4. cd misc/translator && ./translate install fr-FR
5. (Optional) Verify that
   koha-tmpl/intranet-tmpl/prog/fr-FR/js/locale_data.js exists and
   contains your translation
6. Open your browser on the staff main page, change language and verify
   that the message is translated
7. Repeat 1-6 on OPAC side

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Works well, translation is OK and test message is displayed correctly.
Current qa-tool error is a false positive.
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
2020-02-10 10:14:46 +00:00

51 lines
1.2 KiB
JavaScript

(function() {
var params = {
"domain": "Koha"
};
if (typeof json_locale_data !== 'undefined') {
params.locale_data = json_locale_data;
}
Koha.i18n = {
gt: new Gettext(params),
expand: function(text, vars) {
var replace_callback = function(match, name) {
return name in vars ? vars[name] : match;
};
return text.replace(/\{(.*?)\}/g, replace_callback);
}
};
})();
function __(msgid) {
return Koha.i18n.gt.gettext(msgid);
}
function __x(msgid, vars) {
return Koha.i18n.expand(__(msgid), vars);
}
function __n(msgid, msgid_plural, count) {
return Koha.i18n.gt.ngettext(msgid, msgid_plural, count);
}
function __nx(msgid, msgid_plural, count, vars) {
return Koha.i18n.expand(__n(msgid, msgid_plural, count), vars);
}
function __p(msgctxt, msgid) {
return Koha.i18n.gt.pgettext(msgctxt, msgid);
}
function __px(msgctxt, msgid, vars) {
return Koha.i18n.expand(__p(msgctxt, msgid), vars);
}
function __np(msgctxt, msgid, msgid_plural, count) {
return Koha.i18n.gt.npgettext(msgctxt, msgid, msgid_plural, count);
}
function __npx(msgctxt, msgid, msgid_plural, count, vars) {
return Koha.i18n.expand(__np(msgctxt, msgid, msgid_plural, count), vars);
}