mirror of
https://github.com/openvk/openvk
synced 2024-12-23 00:51:03 +03:00
L10n: Improve code in JavaScript tr function
This commit is contained in:
parent
3d44361a76
commit
21b03d3e71
1 changed files with 24 additions and 23 deletions
|
@ -1,36 +1,37 @@
|
|||
function tr(string, ...arg) {
|
||||
function tr(string, ...args) {
|
||||
let output = window.lang[string];
|
||||
if(arg.length > 0) {
|
||||
if(typeof arg[0] == 'number') {
|
||||
let numberedStringId;
|
||||
let cardinal = arg[0];
|
||||
if(args.length > 0) {
|
||||
if(typeof args[0] === "number") {
|
||||
const cardinal = args[0];
|
||||
let numberedString;
|
||||
|
||||
switch(cardinal) {
|
||||
case 0:
|
||||
numberedString = string + '_zero';
|
||||
numberedString = string + "_zero";
|
||||
break;
|
||||
case 1:
|
||||
numberedString = string + '_one';
|
||||
numberedString = string + "_one";
|
||||
break;
|
||||
default:
|
||||
numberedString = string + (cardinal < 5 ? '_few' : '_other');
|
||||
numberedString = string + (cardinal < 5 ? "_few" : "_other");
|
||||
}
|
||||
|
||||
let newoutput = window.lang[numberedString];
|
||||
if(newoutput === null) {
|
||||
newoutput = window.lang[string + '_other'];
|
||||
if(newoutput === null) {
|
||||
newoutput = output;
|
||||
}
|
||||
}
|
||||
let newOutput = window.lang[numberedString];
|
||||
if(newOutput === null)
|
||||
newOutput = window.lang[string + "_other"];
|
||||
|
||||
output = newoutput;
|
||||
if(newOutput === null)
|
||||
newOutput = output;
|
||||
|
||||
output = newOutput;
|
||||
}
|
||||
}
|
||||
|
||||
let i = 1;
|
||||
arg.forEach(element => {
|
||||
output = output.replace(RegExp('(\\$' + i + ')'), element);
|
||||
i++;
|
||||
});
|
||||
|
||||
if(output == null)
|
||||
return "@" + string;
|
||||
|
||||
for(const [ i, element ] of Object.entries(args))
|
||||
output = output.replace(RegExp("(\\$" + (Number(i) + 1) + ")"), element);
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue