[enh] Implements inline HTML translation.

This commit is contained in:
opi 2014-10-03 12:09:54 +02:00
parent d5a47711ab
commit 6152f6da21
2 changed files with 41 additions and 20 deletions

View file

@ -1536,32 +1536,32 @@ app = Sammy('#main', function (sam) {
});
/**
* Translations
*/
$.getJSON('locales/en.json', function(data){
y18n.translations['en'] = data;
y18n.translateInlineHTML();
});
// User defined language
if (window.navigator && window.navigator.language) {
y18n.locale = window.navigator.language.substr(0, 2);
if (y18n.locale !== 'en') {
$.getJSON('locales/'+ y18n.locale +'.json', function(data){
y18n.translations[y18n.locale] = data;
y18n.translateInlineHTML();
});
}
}
/**
* Run the app
*
*/
$(document).ready(function () {
/**
* Translations
*/
// Default language
$.getJSON('locales/en.json', function(data){
y18n.translations['en'] = data;
});
// User language
if (window.navigator && window.navigator.language) {
y18n.locale = window.navigator.language.substr(0, 2);
if (y18n.locale !== 'en') {
$.getJSON('locales/'+ y18n.locale +'.json', function(data){
y18n.translations[y18n.locale] = data;
});
}
}
/**
* Application
*/

View file

@ -21,6 +21,27 @@
y18n.init();
/**
* HTML Inline translation
*/
y18n.translateInlineHTML = function(){
// Inner HTML
[].forEach.call(
document.querySelectorAll('[data-y18n]'),
function(el){
el.innerText = y18n.translate(el.getAttribute('data-y18n'))
}
);
// Attributes
[].forEach.call(
document.querySelectorAll('[data-y18n-title]'),
function(el){
el.title = y18n.translate(el.getAttribute('data-y18n-title'))
}
);
}
/**
* Translation
*/