Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 "use strict"; | |
19 | |
20 (function() | |
21 { | |
22 window.browser = {}; | |
Sebastian Noack
2017/10/13 21:39:23
Nit: Perhaps we should just define the "browser" o
Manish Jethani
2017/10/13 22:30:24
There will be more polyfills in this file (see htt
| |
23 | |
24 /* I18n */ | |
25 | |
26 let getLocaleCandidates = function(selectedLocale) | |
27 { | |
28 let candidates = []; | |
29 let defaultLocale = "en-US"; | |
saroyanm
2017/10/16 19:24:20
Heads-up for the rebase: There were couple of chan
Manish Jethani
2017/10/16 21:35:21
Done.
| |
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 }()); | |
OLD | NEW |