From 0e6f63951adfa49aaeb07f3e45a1cb35e6dabda1 Mon Sep 17 00:00:00 2001 From: tophf Date: Fri, 27 Mar 2015 13:30:07 +0300 Subject: [PATCH] Localization: embed in html, autorun, apply earlier --- edit.html | 26 ++++++++++++------------ edit.js | 53 +++++++++++++++++++++++++++++++----------------- localization.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ manage.html | 27 ++++++++++++------------- manage.js | 31 ++++++++++++++-------------- popup.html | 14 ++++++++----- popup.js | 25 +++++++++++++++-------- 7 files changed, 155 insertions(+), 75 deletions(-) diff --git a/edit.html b/edit.html index a5b3e7b9..60f6100f 100644 --- a/edit.html +++ b/edit.html @@ -285,47 +285,47 @@
-

+

diff --git a/edit.js b/edit.js index 62ea7f2a..55aa4b8b 100644 --- a/edit.js +++ b/edit.js @@ -11,16 +11,41 @@ var propertyToCss = {urls: "url", urlPrefixes: "url-prefix", domains: "domain", var CssToProperty = {"url": "urls", "url-prefix": "urlPrefixes", "domain": "domains", "regexp": "regexps"}; // templates -var appliesToTemplate = document.createElement("li"); -appliesToTemplate.innerHTML = ''; +var appliesToTemplate = tHTML('\ +
  • \ + \ + \ + \ + \ +
  • \ +'); -var appliesToEverythingTemplate = document.createElement("li"); -appliesToEverythingTemplate.className = "applies-to-everything"; -appliesToEverythingTemplate.innerHTML = t("appliesToEverything") + ' '; - -var sectionTemplate = document.createElement("div"); -sectionTemplate.innerHTML = '
    '; +var appliesToEverythingTemplate = tHTML('\ +
  • \ + \ +
  • \ +'); +var sectionTemplate = tHTML('\ +
    \ + \ + \ +
    \ +
    \ + \ +
      \ +
      \ + \ + \ +
      \ +'); // make querySelectorAll enumeration code readable ["forEach", "some", "indexOf"].forEach(function(method) { @@ -153,7 +178,6 @@ function initCodeMirror() { controlOptions = ["smartIndent", "indentWithTabs", "tabSize", "keyMap", "lineWrapping"]; controlOptions.forEach(function(option) { controlPrefs["editor." + option] = CM.defaults[option]; - tE(option + "-label", "cm_" + option); }); loadPrefs(controlPrefs); @@ -554,7 +578,6 @@ function nextPrevBuffer(cm, direction) { window.addEventListener("load", init, false); function init() { - tE("sections-help", "helpAlt", "alt"); var params = getParams(); if (!params.id) { // match should be 2 - one for the whole thing, one for the parentheses // This is an add @@ -582,7 +605,7 @@ function init() { function initWithStyle(style) { document.getElementById("name").value = style.name; document.getElementById("enabled").checked = style.enabled == "true"; - document.getElementById("heading").innerHTML = t("editStyleHeading"); + tE("heading", "editStyleHeading", null, false); // if this was done in response to an update, we need to clear existing sections document.querySelectorAll("#sections > div").forEach(function(div) { div.parentNode.removeChild(div); @@ -781,14 +804,6 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { } }); -tE("name", "styleMissingName", "placeholder"); -tE("enabled-label", "styleEnabledLabel"); -tE("to-mozilla", "styleToMozillaFormat"); -tE("save-button", "styleSaveLabel"); -tE("cancel-button", "styleCancelEditLabel"); -tE("sections-heading", "styleSectionsTitle"); -tE("options-heading", "optionsHeading"); - document.getElementById("to-mozilla").addEventListener("click", showMozillaFormat, false); document.getElementById("to-mozilla-help").addEventListener("click", showToMozillaHelp, false); document.getElementById("save-button").addEventListener("click", save, false); diff --git a/localization.js b/localization.js index b9ebf07a..18fd9059 100644 --- a/localization.js +++ b/localization.js @@ -1,3 +1,5 @@ +tDocLoader(); + function t(key, params) { var s = chrome.i18n.getMessage(key, params) if (s == "") { @@ -17,3 +19,55 @@ function tE(id, key, attr, esc) { document.getElementById(id).innerHTML = t(key); } } + +function tHTML(html) { + var node = document.createElement("div"); + node.innerHTML = html.replace(/>\s+<'); // spaces are removed; use   for an explicit space + tNodeList(node.querySelectorAll("*")); + var child = node.removeChild(node.firstElementChild); + node.remove(); + return child; +} + +function tNodeList(nodes) { + for (var n = 0; n < nodes.length; n++) { + var node = nodes[n]; + if (node.nodeType != 1) { // not an ELEMENT_NODE + continue; + } + for (var a = 0; a < node.attributes.length; a++) { + var name = node.attributes[a].nodeName; + if (name.indexOf("i18n-") != 0) { + continue; + } + name = name.substr(5); // "i18n-".length + var value = t(node.attributes[a].nodeValue); + switch (name) { + case "text": + node.insertBefore(document.createTextNode(value), node.firstChild); + break; + case "html": + node.insertAdjacentHTML("afterbegin", value); + break; + default: + node.setAttribute(name, value); + } + } + } +} + +function tDocLoader() { + // localize HEAD + tNodeList(document.querySelectorAll("*")); + + // localize BODY + var observer = new MutationObserver(function(mutations) { + for (var m = 0; m < mutations.length; m++) { + tNodeList(mutations[m].addedNodes); + } + }); + observer.observe(document, {subtree: true, childList: true}); + document.addEventListener("DOMContentLoaded", function() { + observer.disconnect(); + }); +} diff --git a/manage.html b/manage.html index 06496791..b4b0c34f 100644 --- a/manage.html +++ b/manage.html @@ -1,6 +1,6 @@ - +