File: l10n.js

package info (click to toggle)
xnote 4.1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 788 kB
  • sloc: javascript: 5,779; makefile: 22
file content (55 lines) | stat: -rw-r--r-- 1,580 bytes parent folder | download | duplicates (2)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 license: The MIT License, Copyright (c) 2016-2018 YUKI "Piro" Hiroshi
 original:
   http://github.com/piroor/webextensions-lib-l10n
*/

var l10n = {
  updateString(aString) {
    return aString.replace(/__MSG_(.+?)__/g, (aMatched) => {
      const key = aMatched.slice(6, -2);
      return chrome.i18n.getMessage(key) || aMatched;
    });
  },

  $log(aMessage, ...aArgs) {
    aMessage = `l10s: ${aMessage}`;
    if (typeof window.log === 'function')
      log(aMessage, ...aArgs);
    else ;
//      console.log(aMessage, ...aArgs);
  },

  updateDocument() {
    ~ dump ("\nRunning l10n.updateDocument()");
    const texts = document.evaluate(
      'descendant::text()[contains(self::text(), "__MSG_")]',
      document,
      null,
      XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
      null
    );
    for (let i = 0, maxi = texts.snapshotLength; i < maxi; i++) {
      const text = texts.snapshotItem(i);
      text.nodeValue = this.updateString(text.nodeValue);
    }

    const attributes = document.evaluate(
      'descendant::*/attribute::*[contains(., "__MSG_")]',
      document,
      null,
      XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
      null
    );
    for (let i = 0, maxi = attributes.snapshotLength; i < maxi; i++) {
      const attribute = attributes.snapshotItem(i);
      this.$log('apply', attribute);
      attribute.value = this.updateString(attribute.value);
    }
  }
};

document.addEventListener('DOMContentLoaded', () => {
  l10n.updateDocument();
}, { once: true });
export default l10n;