Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
3 * Copyright (C) 2006-2014 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 this.ext = function(ext) | |
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 { | |
20 /* I18n */ | |
21 | |
22 var getLocaleCandidates = function(selectedLocale) | |
23 { | |
24 var candidates = []; | |
25 var defaultLocale = "en-US"; | |
26 | |
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 | |
29 var parts = selectedLocale.split("-"); | |
30 var language = parts[0]; | |
31 var region = (parts[1] || "").toUpperCase(); | |
32 | |
33 if (region) | |
34 candidates.push(language + "-" + region); | |
35 | |
36 candidates.push(language); | |
37 | |
38 if (candidates.indexOf(defaultLocale) == -1) | |
39 candidates.push(defaultLocale); | |
40 | |
41 return candidates; | |
42 }; | |
43 | |
44 var initCatalog = function(uiLocale) | |
45 { | |
46 var bidiDir = /^(ar|fa|he|ug|ur)(-|$)/.test(uiLocale) ? "rtl" : "ltr"; | |
47 var catalog = Object.create(null); | |
48 | |
49 catalog["@@ui_locale"] = [uiLocale.replace(/-/g, "_"), []]; | |
50 catalog["@@bidi_dir" ] = [bidiDir, []]; | |
51 | |
52 return catalog; | |
53 }; | |
54 | |
55 var selectedLocale = navigator.language; | |
56 var match = /[?&]locale=([\w\-]+)/.exec(window.location.search); | |
57 if (match) | |
58 selectedLocale = match[1]; | |
59 | |
60 var locales = getLocaleCandidates(selectedLocale); | |
61 var catalog = initCatalog(locales[0]); | |
62 var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/, "") + ".json"; | |
63 | |
64 var replacePlaceholder = function(text, placeholder, content) | |
65 { | |
66 return text.split("$" + placeholder + "$").join(content || ""); | |
67 }; | |
68 | |
69 var parseMessage = function(rawMessage) | |
70 { | |
71 var text = rawMessage.message; | |
72 var placeholders = []; | |
73 | |
74 for (var placeholder in rawMessage.placeholders) | |
75 { | |
76 var content = rawMessage.placeholders[placeholder].content; | |
77 | |
78 if (/^\$\d+$/.test(content)) | |
79 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder; | |
80 else | |
81 text = replacePlaceholder(text, placeholder, content); | |
82 } | |
83 | |
84 return [text, placeholders]; | |
85 }; | |
86 | |
87 var readCatalog = function(locale) | |
88 { | |
89 var xhr = new XMLHttpRequest(); | |
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"); | |
92 | |
93 try | |
94 { | |
95 xhr.send(); | |
96 } | |
97 catch (e) | |
98 { | |
99 return; | |
100 } | |
101 | |
102 if (xhr.status != 200 && xhr.status != 0) | |
103 return; | |
104 | |
105 var rawCatalog = JSON.parse(xhr.responseText); | |
106 for (var msgId in rawCatalog) | |
107 { | |
108 if (!(msgId in catalog)) | |
109 catalog[msgId] = parseMessage(rawCatalog[msgId]); | |
110 } | |
111 }; | |
112 | |
113 ext.i18n = { | |
114 getMessage: function(msgId, substitutions) | |
115 { | |
116 while (true) | |
117 { | |
118 var message = catalog[msgId]; | |
119 if (message) | |
120 { | |
121 var text = message[0]; | |
122 var placeholders = message[1]; | |
123 | |
124 if (!(substitutions instanceof Array)) | |
125 substitutions = [substitutions]; | |
126 | |
127 for (var i = 0; i < placeholders.length; i++) | |
128 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | |
129 | |
130 return text; | |
131 } | |
132 | |
133 if (locales.length == 0) | |
134 return ""; | |
135 | |
136 readCatalog(locales.shift()); | |
137 } | |
138 } | |
139 }; | |
140 return ext; | |
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
| |
OLD | NEW |