Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: ext/common.js

Issue 6427347985104896: Issue 1663 - Making first-run page testable in the adblockplusui repository (Closed)
Patch Set: Addressed comments Created Dec. 17, 2014, 2:19 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | firstRun.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 (function(global)
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 = window.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);
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 if (!global.ext)
114 global.ext = {};
115
116 global.ext.i18n = {
117 getMessage: function(msgId, substitutions)
118 {
119 while (true)
120 {
121 var message = catalog[msgId];
122 if (message)
123 {
124 var text = message[0];
125 var placeholders = message[1];
126
127 if (!(substitutions instanceof Array))
128 substitutions = [substitutions];
129
130 for (var i = 0; i < placeholders.length; i++)
131 text = replacePlaceholder(text, placeholders[i], substitutions[i]);
132
133 return text;
134 }
135
136 if (locales.length == 0)
137 return "";
138
139 readCatalog(locales.shift());
140 }
141 }
142 };
143 })(this);
OLDNEW
« no previous file with comments | « no previous file | firstRun.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld