modules

ac-bundle-module-frontend-modules-translation

frontend/modules/translation.js

A module for ac-bundle-app, published by ac-bundle-module.

Available Methods: languages, elements, key, default, userLanguage, language, select, load, start

languages


elements

[object Object]
key

getter
default

English
userLanguage

getter
language

getter
select

function(language) {
  for (var key in mod.keywords) {
    var keyword = mod.keywords[key];
    var value = keyword[language];
    if (!app.has(value)) value = keyword[mod.default];
    var nodes = document.querySelectorAll("[translation=\"" + key + "\"]");
    for (var i=0; i<=nodes.length-1; i++) {
      var node = nodes[i];
      node.innerHTML = value;
    }
    var nodes = document.querySelectorAll("[placeholder=\"translation-" + key + "\"]");
    for (var i=0; i<=nodes.length-1; i++) {
      var node = nodes[i];
      node.setAttribute("placeholder", value);
    }
  }
}

load

async function() {
  var result = await fetch("/sync/airtable/translation/load");
  if (result.status === 200) {
    var json = await result.json();
    for (var key in json) {
      mod[key] = json[key];
    }
    for (var language in mod.languages) {
      (function(language) {
        var item = mod.elements.dummy.cloneNode(true);
        var selected = mod.language === language;
        item.className = item.className.split(" dummy").join("");
        item.innerHTML = language;
        mod.elements.list.appendChild(item);
        item.addEventListener("click", function() {
          if (app.has(mod.selected) && mod.selected.language === language) return false;
          if (app.has(mod.selected)) mod.selected.item.className = mod.selected.item.className.split(" selected").join("");
          mod.language = language;
          mod.selected = {language: language, item: item};
          item.className = item.className.split(" selected").join("") + " selected";
          mod.select(mod.language);
        });
        if (selected) item.dispatchEvent(new CustomEvent("click"));
      })(language);
    }
  } else {
    console.log("Could not load languages.");
  }
}

start

function() {
  mod.load();
}

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *