modernize ui/component/trans

pull/8491/head
Thibault Duplessis 2021-03-27 14:04:38 +01:00
parent d0a566fc03
commit e8e4eacaa4
1 changed files with 20 additions and 22 deletions

View File

@ -1,45 +1,43 @@
const trans = i18n => {
var format = function (str, args) {
const trans = (i18n: I18nDict) => {
const format = (str: string, args: string[]) => {
if (args.length && str.includes('$s')) for (var i = 1; i < 4; i++) str = str.replace('%' + i + '$s', args[i - 1]);
args.forEach(function (arg) {
str = str.replace('%s', arg);
});
return str;
};
var list = function (str, args) {
var segments = str.split(/(%(?:\d\$)?s)/g);
for (var i = 1; i <= args.length; i++) {
var pos = segments.indexOf('%' + i + '$s');
const list = (str: string, args: string[]) => {
const segments = str.split(/(%(?:\d\$)?s)/g);
for (let i = 1; i <= args.length; i++) {
const pos = segments.indexOf('%' + i + '$s');
if (pos !== -1) segments[pos] = args[i - 1];
}
for (var i = 0; i < args.length; i++) {
var pos = segments.indexOf('%s');
for (let i = 0; i < args.length; i++) {
const pos = segments.indexOf('%s');
if (pos === -1) break;
segments[pos] = args[i];
}
return segments;
};
const trans: Trans = function (key) {
var str = i18n[key];
const trans: Trans = function (key: string) {
const str = i18n[key];
return str ? format(str, Array.prototype.slice.call(arguments, 1)) : key;
};
trans.plural = function (key, count) {
var pluralKey = key + ':' + lichess.quantity(count);
var str = i18n[pluralKey] || i18n[key];
trans.plural = function (key: string, count: number) {
const pluralKey = `${key}:${lichess.quantity(count)}`;
const str = i18n[pluralKey] || i18n[key];
return str ? format(str, Array.prototype.slice.call(arguments, 1)) : key;
};
trans.noarg = function (key) {
// optimisation for translations without arguments
return i18n[key] || key;
};
trans.vdom = function (key) {
var str = i18n[key];
// optimisation for translations without arguments
trans.noarg = (key: string) => i18n[key] || key;
trans.vdom = function (key: string) {
const str = i18n[key];
return str ? list(str, Array.prototype.slice.call(arguments, 1)) : [key];
};
trans.vdomPlural = function (key, count) {
var pluralKey = key + ':' + lichess.quantity(count);
var str = i18n[pluralKey] || i18n[key];
trans.vdomPlural = function (key: string, count: number) {
const pluralKey = `${key}:${lichess.quantity(count)}`;
const str = i18n[pluralKey] || i18n[key];
return str ? list(str, Array.prototype.slice.call(arguments, 2)) : [key];
};
return trans;