JS i18n (locale) libary

This commit is contained in:
ElPumpo
2018-09-30 17:51:37 +02:00
parent 8c596d4c1c
commit 4ef2deaa29
3 changed files with 76 additions and 10 deletions
+1
View File
@@ -62,6 +62,7 @@ ui_page {
}
files {
'locale.js',
'html/ui.html',
'html/css/app.css',
+65
View File
@@ -0,0 +1,65 @@
var locale = [];
/**
* Similar to the concept of gettext, this function returns the
* localized string for the parsed string. The function supports
* wrapping.
*
* @param {string} args The string we want localized
* @return {string} Returns the localized string
*
*/
function _U() {
var args = arguments;
var string = args[0];
// Was a string specified?
if (!string) {
console.log('locale.js: no string was parsed');
return 'locale.js: no string was parsed';
}
// Has the locale file been set?
if (locale.length == 0) {
console.log('locale.js: no locale has been set');
return 'locale.js: no locale has been set';
}
// Does the translation exist?
if (!locale[string]) {
console.log('locale.js: translation [{0}] does not exist'.format(string));
return 'locale.js: translation [{0}] does not exist'.format(string);
}
// Do we need to format the string?
if (args.length == 1) {
return capitalize(locale[string]);
} else {
return formatString(args);
}
}
function formatString(args) {
var string = capitalize(locale[args[0]]);
for (var i = 1; i < args.length; i++) {
string = string.replace(/%s/, args[i]);
}
return string;
}
function capitalize(string) {
return string[0].toUpperCase() + string.slice(1);
}
// https://stackoverflow.com/a/35359503
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
+10 -10
View File
@@ -2,20 +2,20 @@ Locales = {}
function _(str, ...) -- Translate string
if Locales[Config.Locale] ~= nil then
if Locales[Config.Locale] ~= nil then
if Locales[Config.Locale][str] ~= nil then
return string.format(Locales[Config.Locale][str], ...)
else
return 'Translation [' .. Config.Locale .. '][' .. str .. '] does not exist'
end
if Locales[Config.Locale][str] ~= nil then
return string.format(Locales[Config.Locale][str], ...)
else
return 'Translation [' .. Config.Locale .. '][' .. str .. '] does not exist'
end
else
return 'Locale [' .. Config.Locale .. '] does not exist'
end
else
return 'Locale [' .. Config.Locale .. '] does not exist'
end
end
function _U(str, ...) -- Translate string first char uppercase
return tostring(_(str, ...):gsub("^%l", string.upper))
return tostring(_(str, ...):gsub("^%l", string.upper))
end