yunohost-admin/src/js/yunohost/y18n.js

115 lines
3 KiB
JavaScript
Raw Normal View History

2014-05-12 18:28:42 +02:00
;(function(y18n){
"use strict";
// Default options
var defaultOptions = {
defaultLocale: "en",
locale: "en",
placeholder: /(?:\{\{|%\{)(.*?)(?:\}\}?)/gm,
translations: {}
2015-08-29 15:45:00 +02:00
};
2014-05-12 18:28:42 +02:00
/**
* Initialization
*/
y18n.init = function() {
// Merge options with defaults.
for (var key in defaultOptions) {
y18n[key] = (typeof y18n[key] !== 'undefined') ? y18n[key] : defaultOptions[key];
}
2015-08-29 15:45:00 +02:00
};
2014-05-12 18:28:42 +02:00
y18n.init();
/**
* HTML Inline translation
*/
y18n.translateInlineHTML = function(){
// Inner HTML
[].forEach.call(
document.querySelectorAll('[data-y18n]'),
function(el){
el.innerHTML = y18n.translate(el.getAttribute('data-y18n'));
}
);
// Attributes
[].forEach.call(
document.querySelectorAll('[data-y18n-title]'),
function(el){
2015-08-29 15:45:00 +02:00
el.title = y18n.translate(el.getAttribute('data-y18n-title'));
}
);
2015-08-29 15:45:00 +02:00
};
2014-05-12 18:28:42 +02:00
/**
* Translation
*/
y18n.translate = function(key, options) {
2014-07-22 14:44:13 +02:00
options = options || {'locale' : y18n.locale};
options.locale = options.locale || y18n.locale;
2014-05-12 18:28:42 +02:00
// Get translation
var translation = this.lookup(key, options);
// Translation fallback
2014-05-13 00:32:15 +02:00
if ((typeof translation === 'undefined' || translation === key) && options.locale !== y18n.defaultLocale) {
2014-05-12 18:28:42 +02:00
options.locale = y18n.defaultLocale;
return this.translate(key, options);
}
// Variables remplacement
return (translation) ? translation.printf(options) : key;
2015-08-29 15:45:00 +02:00
};
2014-05-12 18:28:42 +02:00
2014-07-22 14:44:13 +02:00
y18n.lookup = function(key, options) {
// Default locale
2014-05-12 18:28:42 +02:00
if (typeof options.locale === 'undefined') {
options.locale = y18n.locale;
}
// Get translation string
if (typeof y18n.translations[options.locale] !== 'undefined') {
if (typeof y18n.translations[options.locale][key] !== 'undefined') {
return y18n.translations[options.locale][key];
}
}
2015-08-29 15:45:00 +02:00
};
2014-05-12 18:28:42 +02:00
// Save some typing
y18n.t = y18n.translate;
})(typeof(exports) === 'undefined' ? (this.y18n || (this.y18n = {})) : exports);
// http://monocleglobe.wordpress.com/2010/01/12/everybody-needs-a-little-printf-in-their-javascript/
String.prototype.printf = function (obj) {
var useArguments = false;
var _arguments = arguments;
var i = -1;
if (typeof _arguments[0] == "string") {
useArguments = true;
}
if (obj instanceof Array || useArguments) {
return this.replace(/\%s/g,
function (a, b) {
i++;
if (useArguments) {
if (typeof _arguments[i] == 'string') {
return _arguments[i];
}
else {
throw new Error("Arguments element is an invalid type");
}
}
return obj[i];
});
}
else {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = obj[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
}
};