JS ngettext: fix sprintf() like string format

This commit is contained in:
Benjamin Renard 2025-04-09 15:38:23 +02:00
parent 882ba3b672
commit 71eaf6367c
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -4,13 +4,18 @@
var translations;
var translations_data;
function _(string, ...extra_args) {
function ___(string) {
return string;
}
function gettext(string, ...extra_args) {
var translated = translations ? translations.gettext(string) : string;
translated = translated !== "" ? translated : string;
if (extra_args)
translated = translated.replace(/%[sd]/g, () => extra_args.shift());
return translated;
}
_ = gettext;
function ngettext(singular, plural, n, ...extra_args) {
var translated = translations
@ -24,8 +29,11 @@ function ngettext(singular, plural, n, ...extra_args) {
}
$(document).ready(function () {
// Load Gettext translations
translations = babel.Translations.load(
translations_data ? translations_data : {}
).install();
/**
* Load Gettext translations
*
* Note: do not use babel.Translations.install() method to allow customization of gettext's
* methods and support of sprintf() like string format.
*/
translations = babel.Translations.load(translations_data ?? {});
});