summaryrefslogtreecommitdiff
path: root/src/common/locale.js
blob: 60498a2c60f90d0b15c295a3e22673d83c80c624 (plain)
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
'use strict';
var _ = browser.i18n.getMessage;
var i18n = (() => {
  var i18n = {
    // derived from  http://github.com/piroor/webextensions-lib-l10n

  	updateString(aString) {
  		return aString.replace(/__MSG_(.+?)__/g, function(aMatched) {
  			var key = aMatched.slice(6, -2);
  			return _(key);
  		});
  	},
  	updateDOM(rootNode = document) {
  		var texts = document.evaluate(
  				'descendant::text()[contains(self::text(), "__MSG_")]',
  				rootNode,
  				null,
  				XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  				null
  			);
  		for (let i = 0, maxi = texts.snapshotLength; i < maxi; i++)
  		{
  			let text = texts.snapshotItem(i);
  			text.nodeValue = this.updateString(text.nodeValue);
  		}

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

  document.addEventListener('DOMContentLoaded', e => i18n.updateDOM());
  return i18n;
})()