about summary refs log tree commit diff stats
path: root/src/assets/javascripts/localise.js
blob: 7c24d6c79a22ea0858b0120268e183b36f575386 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
window.browser = window.browser || window.chrome;

function getMessage(tag) {
  return tag.replace(/__MSG_(\w+)__/g, function (_match, v1) {
    return v1 ? browser.i18n.getMessage(v1) : null;
  });
}

function localisePage() {
  let elements = document.querySelectorAll("[data-localise]");

  for (let i in elements)
    if (elements.hasOwnProperty(i)) {
      let obj = elements[i];
      let tag = obj.getAttribute("data-localise").toString();

      let msg = getMessage(tag);

      if (msg && msg !== tag) obj.textContent = msg;
    }

  let placeholders = document.querySelectorAll("[data-localise-placeholder]");

  for (let i in placeholders)
    if (placeholders.hasOwnProperty(i)) {
      let obj = placeholders[i];
      let tag = obj.getAttribute("data-localise-placeholder").toString();

      let msg = getMessage(tag);

      if (msg && msg !== tag) obj.placeholder = msg;
    }
}

localisePage();

export default {
  localisePage
}