OLD | NEW |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 candidates.push(language + "-" + region); | 91 candidates.push(language + "-" + region); |
92 | 92 |
93 candidates.push(language); | 93 candidates.push(language); |
94 | 94 |
95 if (candidates.indexOf(defaultLocale) == -1) | 95 if (candidates.indexOf(defaultLocale) == -1) |
96 candidates.push(defaultLocale); | 96 candidates.push(defaultLocale); |
97 | 97 |
98 return candidates; | 98 return candidates; |
99 }; | 99 }; |
100 | 100 |
101 var initCatalog = function(uiLocale) | |
102 { | |
103 var bidiDir = /^(ar|fa|he|ug|ur)(-|$)/.test(uiLocale) ? "rtl" : "ltr"; | |
104 var catalog = Object.create(null); | |
105 | |
106 catalog["@@ui_locale"] = [uiLocale.replace(/-/g, "_"), []]; | |
107 catalog["@@bidi_dir" ] = [bidiDir, []]; | |
108 | |
109 return catalog; | |
110 }; | |
111 | |
112 var selectedLocale = window.navigator.language; | 101 var selectedLocale = window.navigator.language; |
113 var match = /[?&]locale=([\w\-]+)/.exec(window.location.search); | 102 var match = /[?&]locale=([\w\-]+)/.exec(window.location.search); |
114 if (match) | 103 if (match) |
115 selectedLocale = match[1]; | 104 selectedLocale = match[1]; |
116 | 105 |
117 var locales = getLocaleCandidates(selectedLocale); | 106 var locales = getLocaleCandidates(selectedLocale); |
118 var catalog = initCatalog(locales[0]); | 107 var catalog = Object.create(null); |
119 var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/,
"") + ".json"; | 108 var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/,
"") + ".json"; |
120 | 109 |
121 var replacePlaceholder = function(text, placeholder, content) | 110 var replacePlaceholder = function(text, placeholder, content) |
122 { | 111 { |
123 return text.split("$" + placeholder + "$").join(content || ""); | 112 return text.split("$" + placeholder + "$").join(content || ""); |
124 }; | 113 }; |
125 | 114 |
126 var parseMessage = function(rawMessage) | 115 var parseMessage = function(rawMessage) |
127 { | 116 { |
128 var text = rawMessage.message; | 117 var text = rawMessage.message; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 | 150 |
162 var rawCatalog = JSON.parse(xhr.responseText); | 151 var rawCatalog = JSON.parse(xhr.responseText); |
163 for (var msgId in rawCatalog) | 152 for (var msgId in rawCatalog) |
164 { | 153 { |
165 if (!(msgId in catalog)) | 154 if (!(msgId in catalog)) |
166 catalog[msgId] = parseMessage(rawCatalog[msgId]); | 155 catalog[msgId] = parseMessage(rawCatalog[msgId]); |
167 } | 156 } |
168 }; | 157 }; |
169 | 158 |
170 global.ext.i18n = { | 159 global.ext.i18n = { |
| 160 locale: locales[0], |
171 getMessage: function(msgId, substitutions) | 161 getMessage: function(msgId, substitutions) |
172 { | 162 { |
173 while (true) | 163 while (true) |
174 { | 164 { |
175 var message = catalog[msgId]; | 165 var message = catalog[msgId]; |
176 if (message) | 166 if (message) |
177 { | 167 { |
178 var text = message[0]; | 168 var text = message[0]; |
179 var placeholders = message[1]; | 169 var placeholders = message[1]; |
180 | 170 |
181 if (!(substitutions instanceof Array)) | 171 if (!(substitutions instanceof Array)) |
182 substitutions = [substitutions]; | 172 substitutions = [substitutions]; |
183 | 173 |
184 for (var i = 0; i < placeholders.length; i++) | 174 for (var i = 0; i < placeholders.length; i++) |
185 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | 175 text = replacePlaceholder(text, placeholders[i], substitutions[i]); |
186 | 176 |
187 return text; | 177 return text; |
188 } | 178 } |
189 | 179 |
190 if (locales.length == 0) | 180 if (locales.length == 0) |
191 return ""; | 181 return ""; |
192 | 182 |
193 var locale = locales.shift(); | 183 var locale = locales.shift(); |
194 readCatalog(locale, "common.json"); | 184 readCatalog(locale, "common.json"); |
195 readCatalog(locale, catalogFile); | 185 readCatalog(locale, catalogFile); |
196 } | 186 } |
197 } | 187 } |
198 }; | 188 }; |
199 })(this); | 189 })(this); |
OLD | NEW |