Caret/js/util/i18n.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-06-30 01:16:50 +00:00
define(["util/dom2"], function() {
var translationCache = {};
2014-06-30 01:16:50 +00:00
return {
//process a chunk of template HTML for i18n strings
process: function(html) {
return html.replace(/__MSG_(\w+)__/g, function(match, tag) {
return chrome.i18n.getMessage(tag);
});
},
//process the page for inline strings, marked with .i18n
page: function() {
document.findAll(".i18n").forEach(function(element) {
var original = element.innerHTML;
var translated = chrome.i18n.getMessage(original);
if (translated) element.innerHTML = translated;
});
},
//get a message, or return the untranslated text
//caches results for speed
get: function(message) {
var translated;
if (translationCache[message]) {
translated = translationCache[message];
} else {
translated = chrome.i18n.getMessage(message) || message;
translationCache[message] = translated;
}
for (var i = 1; i < arguments.length; i++) {
translated = translated.replace(new RegExp("\$" + i, "g"), arguments[i]);
}
return translated;
2014-06-30 01:16:50 +00:00
}
}
});