Translation for javascript
How to use:
var translation = translate(LOCALIZATION.WARNING, "Zauberer");
console.log(translation);
Setup
<script src="localization.de.js" type="text/javascript" charset="utf-8"></script>
<script src="translate.js" type="text/javascript" charset="utf-8"></script>
localization.de.js
var LOCALIZATION = {
LANGUAGE: 'GERMAN',
WARNING: 'Achtung vor dem %s, er ist gefährlich !',
LOADING: 'Bitte warten, Daten werden geladen',
WELCOME: 'Hallo %s, wie geht's dir ?'
};
translate.js
(function () {
if (!window.translate) {
/**
* this global function is for string substitution
* @property {string} string to translate.
* @property {string} as much
* return STRING
*/
window.translate = function(){
var html = [ ];
var arguments = arguments;
var format = arguments[0];
var objIndex = 0;
var reg = /\%s/;
var parts = [ ];
/**
* analyze the string, extract the parts with the %s identifier.
*/
for ( var m = reg.exec(format); m; m = reg.exec(format) ) {
parts.push(format.substr(0, m[0][0] == "%" ? m.index : m.index+1));
parts.push("%s");
format = format.substr(m.index+m[0].length);
}
parts.push(format);
/**
* analyze the parts, replace the %s with the given arguments.
* beware of undefined!
*/
for (var i = 0; i < parts.length; ++i){
var part = parts[i];
if (part && part === "%s"){
var object = arguments[++objIndex];
if (object === undefined) {
html.push("%s");
}else{
html.push(object);
};
}
else{
html.push(part);
}
}
/**
* Join the array and return as string.
*/
return html.join('');
}
};
})();