openvk/Web/static/js/l10n.js
2021-12-24 22:58:36 +02:00

37 lines
1.1 KiB
JavaScript
Executable file

function tr(string, ...args) {
let output = window.lang[string];
if(args.length > 0) {
if(typeof args[0] === "number") {
const cardinal = args[0];
let numberedString;
switch(cardinal) {
case 0:
numberedString = string + "_zero";
break;
case 1:
numberedString = string + "_one";
break;
default:
numberedString = string + (cardinal < 5 ? "_few" : "_other");
}
let newOutput = window.lang[numberedString];
if(newOutput == null)
newOutput = window.lang[string + "_other"];
if(newOutput == null)
newOutput = output;
output = newOutput;
}
}
if(output == null)
return "@" + string;
for(const [ i, element ] of Object.entries(args))
output = output.replace(RegExp("(\\$" + (Number(i) + 1) + ")"), element);
return output;
}