Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 this.ext = function(ext) | 18 (function(global) |
Thomas Greiner
2014/12/17 11:24:44
Why do you use `this.ext` here when we usually use
Wladimir Palant
2014/12/17 13:00:38
I simply don't like assuming that this code is run
| |
19 { | 19 { |
20 /* I18n */ | 20 /* I18n */ |
21 | 21 |
22 var getLocaleCandidates = function(selectedLocale) | 22 var getLocaleCandidates = function(selectedLocale) |
23 { | 23 { |
24 var candidates = []; | 24 var candidates = []; |
25 var defaultLocale = "en-US"; | 25 var defaultLocale = "en-US"; |
26 | 26 |
27 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second | 27 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second |
28 // dash is dropped, since we only support language and region | 28 // dash is dropped, since we only support language and region |
(...skipping 16 matching lines...) Expand all Loading... | |
45 { | 45 { |
46 var bidiDir = /^(ar|fa|he|ug|ur)(-|$)/.test(uiLocale) ? "rtl" : "ltr"; | 46 var bidiDir = /^(ar|fa|he|ug|ur)(-|$)/.test(uiLocale) ? "rtl" : "ltr"; |
47 var catalog = Object.create(null); | 47 var catalog = Object.create(null); |
48 | 48 |
49 catalog["@@ui_locale"] = [uiLocale.replace(/-/g, "_"), []]; | 49 catalog["@@ui_locale"] = [uiLocale.replace(/-/g, "_"), []]; |
50 catalog["@@bidi_dir" ] = [bidiDir, []]; | 50 catalog["@@bidi_dir" ] = [bidiDir, []]; |
51 | 51 |
52 return catalog; | 52 return catalog; |
53 }; | 53 }; |
54 | 54 |
55 var selectedLocale = navigator.language; | 55 var selectedLocale = window.navigator.language; |
56 var match = /[?&]locale=([\w\-]+)/.exec(window.location.search); | 56 var match = /[?&]locale=([\w\-]+)/.exec(window.location.search); |
57 if (match) | 57 if (match) |
58 selectedLocale = match[1]; | 58 selectedLocale = match[1]; |
59 | 59 |
60 var locales = getLocaleCandidates(selectedLocale); | 60 var locales = getLocaleCandidates(selectedLocale); |
61 var catalog = initCatalog(locales[0]); | 61 var catalog = initCatalog(locales[0]); |
62 var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/, "") + ".json"; | 62 var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/, "") + ".json"; |
63 | 63 |
64 var replacePlaceholder = function(text, placeholder, content) | 64 var replacePlaceholder = function(text, placeholder, content) |
65 { | 65 { |
(...skipping 14 matching lines...) Expand all Loading... | |
80 else | 80 else |
81 text = replacePlaceholder(text, placeholder, content); | 81 text = replacePlaceholder(text, placeholder, content); |
82 } | 82 } |
83 | 83 |
84 return [text, placeholders]; | 84 return [text, placeholders]; |
85 }; | 85 }; |
86 | 86 |
87 var readCatalog = function(locale) | 87 var readCatalog = function(locale) |
88 { | 88 { |
89 var xhr = new XMLHttpRequest(); | 89 var xhr = new XMLHttpRequest(); |
90 xhr.open("GET", "locale/" + locale + "/" + catalogFile, false); | 90 xhr.open("GET", "locale/" + locale + "/" + catalogFile, false); |
Thomas Greiner
2014/12/17 11:24:44
I suppose this is the reason why test_server.py is
Wladimir Palant
2014/12/17 13:00:38
You are correct, Chrome won't allow accessing any
| |
91 xhr.overrideMimeType("text/plain"); | 91 xhr.overrideMimeType("text/plain"); |
92 | 92 |
93 try | 93 try |
94 { | 94 { |
95 xhr.send(); | 95 xhr.send(); |
96 } | 96 } |
97 catch (e) | 97 catch (e) |
98 { | 98 { |
99 return; | 99 return; |
100 } | 100 } |
101 | 101 |
102 if (xhr.status != 200 && xhr.status != 0) | 102 if (xhr.status != 200 && xhr.status != 0) |
103 return; | 103 return; |
104 | 104 |
105 var rawCatalog = JSON.parse(xhr.responseText); | 105 var rawCatalog = JSON.parse(xhr.responseText); |
106 for (var msgId in rawCatalog) | 106 for (var msgId in rawCatalog) |
107 { | 107 { |
108 if (!(msgId in catalog)) | 108 if (!(msgId in catalog)) |
109 catalog[msgId] = parseMessage(rawCatalog[msgId]); | 109 catalog[msgId] = parseMessage(rawCatalog[msgId]); |
110 } | 110 } |
111 }; | 111 }; |
112 | 112 |
113 ext.i18n = { | 113 if (!global.ext) |
114 global.ext = {}; | |
115 | |
116 global.ext.i18n = { | |
114 getMessage: function(msgId, substitutions) | 117 getMessage: function(msgId, substitutions) |
115 { | 118 { |
116 while (true) | 119 while (true) |
117 { | 120 { |
118 var message = catalog[msgId]; | 121 var message = catalog[msgId]; |
119 if (message) | 122 if (message) |
120 { | 123 { |
121 var text = message[0]; | 124 var text = message[0]; |
122 var placeholders = message[1]; | 125 var placeholders = message[1]; |
123 | 126 |
124 if (!(substitutions instanceof Array)) | 127 if (!(substitutions instanceof Array)) |
125 substitutions = [substitutions]; | 128 substitutions = [substitutions]; |
126 | 129 |
127 for (var i = 0; i < placeholders.length; i++) | 130 for (var i = 0; i < placeholders.length; i++) |
128 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | 131 text = replacePlaceholder(text, placeholders[i], substitutions[i]); |
129 | 132 |
130 return text; | 133 return text; |
131 } | 134 } |
132 | 135 |
133 if (locales.length == 0) | 136 if (locales.length == 0) |
134 return ""; | 137 return ""; |
135 | 138 |
136 readCatalog(locales.shift()); | 139 readCatalog(locales.shift()); |
137 } | 140 } |
138 } | 141 } |
139 }; | 142 }; |
140 return ext; | 143 })(this); |
141 }(this.ext || {}); | |
Thomas Greiner
2014/12/17 11:24:44
Unless you expect this to run in a context where `
Wladimir Palant
2014/12/17 13:00:38
It's probably cleaner to simply pass in the global
| |
LEFT | RIGHT |