Преглед на файлове

Use https://github.com/piroor/webextensions-lib-l10n/

Brendan Abolivier преди 7 години
родител
ревизия
08c6b8b147
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
променени са 2 файла, в които са добавени 61 реда и са изтрити 4 реда
  1. 5
    4
      index.html
  2. 56
    0
      libs/l10n.js

+ 5
- 4
index.html Целия файл

@@ -17,13 +17,14 @@
17 17
 				margin-top: 10px;
18 18
 			}
19 19
 		</style>
20
-		<script src="jquery.js"></script>
20
+		<script src="libs/jquery.js"></script>
21
+		<script src="libs/l10n.js"></script>
21 22
 		<script src="index.js"></script>
22 23
 	</head>
23 24
 	<body>
24
-		<div id="mirror" style="display:none">This website may be blocked in your region.<br />
25
-				<button>Take me to a mirror</button>
25
+		<div id="mirror" style="display:none">__MSG_mirror__<br />
26
+				<button>__MSG_mirror.button__</button>
26 27
 		</div>
27
-		<div id="nomirror">This website has no mirror provided by RWB</div>
28
+		<div id="nomirror">__MSG_nomirror__</div>
28 29
 	</body>
29 30
 </html>

+ 56
- 0
libs/l10n.js Целия файл

@@ -0,0 +1,56 @@
1
+/*
2
+ license: The MIT License, Copyright (c) 2016-2017 YUKI "Piro" Hiroshi
3
+ original:
4
+   http://github.com/piroor/webextensions-lib-l10n
5
+*/
6
+
7
+var l10n = {
8
+	updateString(aString) {
9
+		return aString.replace(/__MSG_(.+?)__/g, function(aMatched) {
10
+			var key = aMatched.slice(6, -2);
11
+			return chrome.i18n.getMessage(key);
12
+		});
13
+	},
14
+
15
+	$log(aMessage, ...aArgs) {
16
+		aMessage = 'l10s ' + aMessage;
17
+		if (typeof log === 'function')
18
+			log(aMessage, ...aArgs);
19
+		else
20
+			console.log(aMessage, ...aArgs);
21
+	},
22
+
23
+	updateDocument() {
24
+		var texts = document.evaluate(
25
+				'descendant::text()[contains(self::text(), "__MSG_")]',
26
+				document,
27
+				null,
28
+				XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
29
+				null
30
+			);
31
+		for (let i = 0, maxi = texts.snapshotLength; i < maxi; i++)
32
+		{
33
+			let text = texts.snapshotItem(i);
34
+			text.nodeValue = this.updateString(text.nodeValue);
35
+		}
36
+
37
+		var attributes = document.evaluate(
38
+				'descendant::*/attribute::*[contains(., "__MSG_")]',
39
+				document,
40
+				null,
41
+				XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
42
+				null
43
+			);
44
+		for (let i = 0, maxi = attributes.snapshotLength; i < maxi; i++)
45
+		{
46
+			let attribute = attributes.snapshotItem(i);
47
+			this.$log('apply', attribute);
48
+			attribute.value = this.updateString(attribute.value);
49
+		}
50
+	}
51
+};
52
+
53
+document.addEventListener('DOMContentLoaded', function onReady() {
54
+	document.removeEventListener('DOMContentLoaded', onReady);
55
+	l10n.updateDocument();
56
+});