openvk/Web/static/js/l10n.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

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