瀏覽代碼

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

Brendan Abolivier 7 年之前
父節點
當前提交
08c6b8b147
簽署人: Brendan Abolivier <contact@brendanabolivier.com> GPG 金鑰 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
+});