JS translations: allow simple string formatting

This commit is contained in:
Benjamin Renard 2024-09-21 18:31:42 +02:00
parent da317d85d0
commit 6a0bb16517
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -4,17 +4,23 @@
var translations; var translations;
var translations_data; var translations_data;
function _(string) { function _(string, ...extra_args) {
var translated = translations ? translations.gettext(string) : string; var translated = translations ? translations.gettext(string) : string;
return translated !== "" ? translated : string; translated = translated !== "" ? translated : string;
if (extra_args)
translated = translated.replace(/%[sd]/g, () => extra_args.shift());
return translated;
} }
function ngettext(singular, plural, n) { function ngettext(singular, plural, n, ...extra_args) {
return translations var translated = translations
? translations.ngettext(singular, plural, n) ? translations.ngettext(singular, plural, n)
: n > 1 : n > 1
? plural ? plural
: singular; : singular;
if (extra_args)
translated = translated.replace(/%[sd]/g, () => extra_args.shift());
return translated;
} }
$(document).ready(function () { $(document).ready(function () {