Przeglądaj źródła

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

Brendan Abolivier 7 lat temu
rodzic
commit
17bef302ca
Podpisane przez: Brendan Abolivier <contact@brendanabolivier.com> ID klucza GPG: 8EF1500759F70623
2 zmienionych plików z 83 dodań i 27 usunięć
  1. 27
    27
      index.html
  2. 56
    0
      libs/l10n.js

+ 27
- 27
index.html Wyświetl plik

@@ -1,30 +1,30 @@
1 1
 <!doctype html>
2 2
 <html>
3
-  <head>
4
-    <title>Collateral Freedom</title>
5
-    <style>
6
-      body {
7
-        font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
8
-        font-size: 100%;
9
-        text-overflow: ellipsis;
10
-        overflow: hidden;
11
-        width: 400px;
12
-        text-align:center;
13
-      }
14
-      button {
15
-        width: 100%;
16
-        height: 50px;
17
-        margin-top: 10px;
18
-      }
19
-    </style>
20
-    <script src="jquery.js"></script>
21
-    <script src="index.js"></script>
22
-  </head>
23
-  <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>
26
-    </div>
27
-    <div id="nomirror">This website has no mirror provided by RWB</div>
28
-  </body>
3
+	<head>
4
+		<title>Collateral Freedom</title>
5
+		<style>
6
+			body {
7
+				font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
8
+				font-size: 100%;
9
+				text-overflow: ellipsis;
10
+				overflow: hidden;
11
+				width: 400px;
12
+				text-align:center;
13
+			}
14
+			button {
15
+				width: 100%;
16
+				height: 50px;
17
+				margin-top: 10px;
18
+			}
19
+		</style>
20
+		<script src="libs/jquery.js"></script>
21
+		<script src="libs/l10n.js"></script>
22
+		<script src="index.js"></script>
23
+	</head>
24
+	<body>
25
+		<div id="mirror" style="display:none">__MSG_mirror__<br />
26
+				<button>__MSG_mirror.button__</button>
27
+		</div>
28
+		<div id="nomirror">__MSG_nomirror__</div>
29
+	</body>
29 30
 </html>
30
-

+ 56
- 0
libs/l10n.js Wyświetl plik

@@ -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
+});