Skip to content

Instantly share code, notes, and snippets.

@hugohil
Created November 20, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugohil/b4665fc8d9bce2d867df to your computer and use it in GitHub Desktop.
Save hugohil/b4665fc8d9bce2d867df to your computer and use it in GitHub Desktop.
i18n.init({ lng: 'en-EN', load: 'current', fallbackLng: false }, function(t) {
var keys = document.querySelectorAll('[data-i18n]');
for (var i = 0; i < keys.length; i++) {
var key = keys[i].dataset.i18n;
var attribute = null;
var attributeRegEx = new RegExp(/\[(\S*)\]/);
if(key.match(attributeRegEx)){
attribute = key.match(attributeRegEx)[1];
key = key.replace(attributeRegEx, '');
}
var translation = i18n.t(key);
if(translation){
if(!attribute){
keys[i].innerHTML = translation;
} else {
keys[i].setAttribute(attribute, translation);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment