Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 if (typeof browser == "undefined") | 20 (function() |
Sebastian Noack
2017/10/10 23:23:08
This check seems unnecessary. This script is only
Manish Jethani
2017/10/11 11:29:06
But if the developer is testing on Chrome, the bro
Sebastian Noack
2017/10/13 02:48:17
The browser object will never be available here, a
Manish Jethani
2017/10/13 05:00:34
Ah, you're right.
Done.
| |
21 { | |
21 window.browser = {}; | 22 window.browser = {}; |
23 | |
24 /* I18n */ | |
25 | |
26 let getLocaleCandidates = function(selectedLocale) | |
27 { | |
28 let candidates = []; | |
29 let defaultLocale = "en_US"; | |
30 | |
31 // e.g. "ja-jp-mac" -> "ja_JP", note that the part after the second | |
32 // dash is dropped, since we only support language and region | |
33 let parts = selectedLocale.split("-"); | |
34 let language = parts[0]; | |
35 let region = (parts[1] || "").toUpperCase(); | |
36 | |
37 if (region) | |
38 candidates.push(language + "_" + region); | |
39 | |
40 candidates.push(language); | |
41 | |
42 if (candidates.indexOf(defaultLocale) == -1) | |
43 candidates.push(defaultLocale); | |
44 | |
45 return candidates; | |
46 }; | |
47 | |
48 let selectedLocale = window.navigator.language; | |
49 let match = /[?&]locale=([\w-]+)/.exec(window.location.search); | |
50 if (match) | |
51 selectedLocale = match[1]; | |
52 | |
53 let locales = getLocaleCandidates(selectedLocale); | |
54 let catalog = Object.create(null); | |
55 let catalogFile = window.location.pathname.replace(/.*\//, "") | |
56 .replace(/\..*/, "") + ".json"; | |
57 | |
58 let replacePlaceholder = function(text, placeholder, content) | |
59 { | |
60 return text.split("$" + placeholder + "$").join(content || ""); | |
61 }; | |
62 | |
63 let parseMessage = function(rawMessage) | |
64 { | |
65 let text = rawMessage.message; | |
66 let placeholders = []; | |
67 | |
68 for (let placeholder in rawMessage.placeholders) | |
69 { | |
70 let {content} = rawMessage.placeholders[placeholder]; | |
71 | |
72 if (/^\$\d+$/.test(content)) | |
73 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder; | |
74 else | |
75 text = replacePlaceholder(text, placeholder, content); | |
76 } | |
77 | |
78 return [text, placeholders]; | |
79 }; | |
80 | |
81 let readCatalog = function(locale, file) | |
82 { | |
83 let xhr = new XMLHttpRequest(); | |
84 xhr.open("GET", "locale/" + locale + "/" + file, false); | |
85 xhr.overrideMimeType("text/plain"); | |
86 | |
87 try | |
88 { | |
89 xhr.send(); | |
90 } | |
91 catch (e) | |
92 { | |
93 return; | |
94 } | |
95 | |
96 if (xhr.status != 200 && xhr.status != 0) | |
97 return; | |
98 | |
99 let rawCatalog = JSON.parse(xhr.responseText); | |
100 for (let msgId in rawCatalog) | |
101 { | |
102 if (!(msgId in catalog)) | |
103 catalog[msgId] = parseMessage(rawCatalog[msgId]); | |
104 } | |
105 }; | |
106 | |
107 browser.i18n = { | |
108 getUILanguage() | |
109 { | |
110 return locales[0].replace(/_/g, "-"); | |
111 }, | |
112 getMessage(msgId, substitutions) | |
113 { | |
114 while (true) | |
115 { | |
116 let message = catalog[msgId]; | |
117 if (message) | |
118 { | |
119 let text = message[0]; | |
120 let placeholders = message[1]; | |
121 | |
122 if (!(substitutions instanceof Array)) | |
123 substitutions = [substitutions]; | |
124 | |
125 for (let i = 0; i < placeholders.length; i++) | |
126 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | |
127 | |
128 return text; | |
129 } | |
130 | |
131 if (locales.length == 0) | |
132 return ""; | |
133 | |
134 let locale = locales.shift(); | |
135 readCatalog(locale, "common.json"); | |
136 readCatalog(locale, catalogFile); | |
137 } | |
138 } | |
139 }; | |
140 }()); | |
LEFT | RIGHT |