about summary refs log tree commit diff stats
path: root/src/assets/javascripts/localise.js
blob: d26d07d4ddcbb60b4d4cc1ed8da52a09e56f06d9 (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
window.browser = window.browser || window.chrome

function localisePage() {
  /**
   * @param {string} tag
   */
  function getMessage(tag) {
    return tag.replace(/__MSG_(\w+)__/g, (_match, v1) => {
      return v1 ? browser.i18n.getMessage(v1) : null
    })
  }

  const elements = document.querySelectorAll("[data-localise]")
  for (let i in elements)
    if (elements.hasOwnProperty(i)) {
      const obj = elements[i]
      const tag = obj.getAttribute("data-localise").toString()
      const msg = getMessage(tag)
      if (msg && msg !== tag) obj.textContent = msg
    }

  const placeholders = document.querySelectorAll("[data-localise-placeholder]")
  for (let i in placeholders)
    if (placeholders.hasOwnProperty(i)) {
      const obj = placeholders[i]
      const tag = obj.getAttribute("data-localise-placeholder").toString()
      const msg = getMessage(tag)
      if (msg && msg !== tag) obj.placeholder = msg
    }
}

export default {
  localisePage,
}