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

Side by Side Diff: polyfill.js

Issue 29573083: Issue 5028 - Use browser namespace (Closed) Base URL: https://hg.adblockplus.org/adblockplusui/
Patch Set: Move i18n code into polyfill.js Created Oct. 11, 2017, 11:27 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« background.html ('K') | « mobile-options.js ('k') | no next file » | 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 <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 if (typeof browser == "undefined")
23 window.browser = {};
24
25 /* I18n */
26
27 let getLocaleCandidates = function(selectedLocale)
28 {
29 let candidates = [];
30 let defaultLocale = "en-US";
31
32 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second
33 // dash is dropped, since we only support language and region
34 let parts = selectedLocale.split("-");
35 let language = parts[0];
36 let region = (parts[1] || "").toUpperCase();
37
38 if (region)
39 candidates.push(language + "-" + region);
40
41 candidates.push(language);
42
43 if (candidates.indexOf(defaultLocale) == -1)
44 candidates.push(defaultLocale);
45
46 return candidates;
47 };
48
49 let selectedLocale = window.navigator.language;
50 let match = /[?&]locale=([\w-]+)/.exec(window.location.search);
51 if (match)
52 selectedLocale = match[1];
53
54 let locales = getLocaleCandidates(selectedLocale);
55 let catalog = Object.create(null);
56 let catalogFile = window.location.pathname.replace(/.*\//, "")
57 .replace(/\..*/, "") + ".json";
58
59 let replacePlaceholder = function(text, placeholder, content)
60 {
61 return text.split("$" + placeholder + "$").join(content || "");
62 };
63
64 let parseMessage = function(rawMessage)
65 {
66 let text = rawMessage.message;
67 let placeholders = [];
68
69 for (let placeholder in rawMessage.placeholders)
70 {
71 let {content} = rawMessage.placeholders[placeholder];
72
73 if (/^\$\d+$/.test(content))
74 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder;
75 else
76 text = replacePlaceholder(text, placeholder, content);
77 }
78
79 return [text, placeholders];
80 };
81
82 let readCatalog = function(locale, file)
83 {
84 let xhr = new XMLHttpRequest();
85 xhr.open("GET", "locale/" + locale + "/" + file, false);
86 xhr.overrideMimeType("text/plain");
87
88 try
89 {
90 xhr.send();
91 }
92 catch (e)
93 {
94 return;
95 }
96
97 if (xhr.status != 200 && xhr.status != 0)
98 return;
99
100 let rawCatalog = JSON.parse(xhr.responseText);
101 for (let msgId in rawCatalog)
102 {
103 if (!(msgId in catalog))
104 catalog[msgId] = parseMessage(rawCatalog[msgId]);
105 }
106 };
107
108 browser.i18n = {
109 getUILanguage()
110 {
111 return locales[0].replace(/_/g, "-");
112 },
113 getMessage(msgId, substitutions)
114 {
115 while (true)
116 {
117 let message = catalog[msgId];
118 if (message)
119 {
120 let text = message[0];
121 let placeholders = message[1];
122
123 if (!(substitutions instanceof Array))
124 substitutions = [substitutions];
125
126 for (let i = 0; i < placeholders.length; i++)
127 text = replacePlaceholder(text, placeholders[i], substitutions[i]);
128
129 return text;
130 }
131
132 if (locales.length == 0)
133 return "";
134
135 let locale = locales.shift();
136 readCatalog(locale, "common.json");
137 readCatalog(locale, catalogFile);
138 }
139 }
140 };
141 }());
OLDNEW
« background.html ('K') | « mobile-options.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld