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

Delta Between Two Patch Sets: lib/utils.js

Issue 29338190: Issue 3697 - Fall back to i18n.getUILanguage if @ui_locale isn't supported (Closed)
Left Patch Set: Address comments. Created April 11, 2016, 4:39 p.m.
Right Patch Set: Address the nits Created Aug. 30, 2017, 10:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/notificationHelper.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-present 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 var Utils = exports.Utils = { 18 "use strict";
19
20 let Utils = exports.Utils = {
19 systemPrincipal: null, 21 systemPrincipal: null,
20 getString: function(id) 22 getString(id)
21 { 23 {
22 if (typeof ext !== "undefined" && "i18n" in ext) 24 return ext.i18n.getMessage("global_" + id);
23 return ext.i18n.getMessage("global_" + id);
24 else
25 return id;
26 }, 25 },
27 runAsync: function(callback) 26 runAsync(callback)
28 { 27 {
29 if (document.readyState == "loading") 28 if (document.readyState == "loading")
30 { 29 {
31 // Make sure to not run asynchronous actions before all 30 // Make sure to not run asynchronous actions before all
32 // scripts loaded. This caused issues on Opera in the past. 31 // scripts loaded. This caused issues on Opera in the past.
33 let onDOMContentLoaded = function() 32 let onDOMContentLoaded = () =>
34 { 33 {
35 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded); 34 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
36 callback(); 35 callback();
37 }; 36 };
38 document.addEventListener("DOMContentLoaded", onDOMContentLoaded); 37 document.addEventListener("DOMContentLoaded", onDOMContentLoaded);
39 } 38 }
40 else 39 else
41 { 40 {
42 setTimeout(callback, 0); 41 setTimeout(callback, 0);
43 } 42 }
44 }, 43 },
45 get appLocale() 44 get appLocale()
46 { 45 {
47 var locale = ext.i18n.getMessage("@@ui_locale"); 46 let locale = ext.i18n.getUILanguage();
48 if (locale == "") {
49 locale = ext.i18n.getUILanguage();
50 }
51 locale = locale.replace(/_/g, "-");
52 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); 47 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true});
53 return this.appLocale; 48 return this.appLocale;
54 }, 49 },
55 get readingDirection() 50 get readingDirection()
56 { 51 {
57 var direction = ext.i18n.getMessage("@@bidi_dir"); 52 let direction = ext.i18n.getMessage("@@bidi_dir");
58 if (direction == "") { 53 // This fallback is only necessary for Microsoft Edge
Sebastian Noack 2016/05/12 22:14:29 Nit: I would rather check for !direction, this rea
Sebastian Noack 2016/05/12 22:14:30 Nit: Braces aren't required according to our codin
59 direction = /^(ar|fa|he|ug|ur)(_|$)/.test(this.appLocale) ? "rtl" : "ltr"; 54 if (!direction)
60 } 55 direction = /^(?:ar|fa|he|ug|ur)\b/.test(this.appLocale) ? "rtl" : "ltr";
61 Object.defineProperty(this, "readingDirection", {value: direction, enumerabl e: true}); 56 Object.defineProperty(
57 this,
58 "readingDirection",
59 {value: direction, enumerable: true}
60 );
62 return this.readingDirection; 61 return this.readingDirection;
63 }, 62 },
64 generateChecksum: function(lines) 63 generateChecksum(lines)
65 { 64 {
66 // We cannot calculate MD5 checksums yet :-( 65 // We cannot calculate MD5 checksums yet :-(
67 return null; 66 return null;
68 }, 67 },
69 makeURI: function(url) 68 checkLocalePrefixMatch(prefixes)
70 {
71 return Services.io.newURI(url);
72 },
73
74 checkLocalePrefixMatch: function(prefixes)
75 { 69 {
76 if (!prefixes) 70 if (!prefixes)
77 return null; 71 return null;
78 72
79 var list = prefixes.split(","); 73 for (let prefix of prefixes.split(","))
80 for (var i = 0; i < list.length; i++) 74 {
81 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) 75 if (new RegExp("^" + prefix + "\\b").test(this.appLocale))
82 return list[i]; 76 return prefix;
77 }
83 78
84 return null; 79 return null;
85 }, 80 },
86 81
87 chooseFilterSubscription: function(subscriptions) 82 chooseFilterSubscription(subscriptions)
88 { 83 {
89 var selectedItem = null; 84 let selectedItem = null;
90 var selectedPrefix = null; 85 let selectedPrefix = null;
91 var matchCount = 0; 86 let matchCount = 0;
92 for (var i = 0; i < subscriptions.length; i++) 87 for (let subscription of subscriptions)
93 { 88 {
94 var subscription = subscriptions[i];
95 if (!selectedItem) 89 if (!selectedItem)
96 selectedItem = subscription; 90 selectedItem = subscription;
97 91
98 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); 92 let prefix = Utils.checkLocalePrefixMatch(
93 subscription.getAttribute("prefixes")
94 );
99 if (prefix) 95 if (prefix)
100 { 96 {
101 if (!selectedPrefix || selectedPrefix.length < prefix.length) 97 if (!selectedPrefix || selectedPrefix.length < prefix.length)
102 { 98 {
103 selectedItem = subscription; 99 selectedItem = subscription;
104 selectedPrefix = prefix; 100 selectedPrefix = prefix;
105 matchCount = 1; 101 matchCount = 1;
106 } 102 }
107 else if (selectedPrefix && selectedPrefix.length == prefix.length) 103 else if (selectedPrefix && selectedPrefix.length == prefix.length)
108 { 104 {
109 matchCount++; 105 matchCount++;
110 106
111 // If multiple items have a matching prefix of the same length: 107 // If multiple items have a matching prefix of the same length:
112 // Select one of the items randomly, probability should be the same 108 // Select one of the items randomly, probability should be the same
113 // for all items. So we replace the previous match here with 109 // for all items. So we replace the previous match here with
114 // probability 1/N (N being the number of matches). 110 // probability 1/N (N being the number of matches).
115 if (Math.random() * matchCount < 1) 111 if (Math.random() * matchCount < 1)
116 { 112 {
117 selectedItem = subscription; 113 selectedItem = subscription;
118 selectedPrefix = prefix; 114 selectedPrefix = prefix;
119 } 115 }
120 } 116 }
121 } 117 }
122 } 118 }
123 return selectedItem; 119 return selectedItem;
124 }, 120 },
125 121
126 getDocLink: function(linkID) 122 getDocLink(linkID)
127 { 123 {
128 var Prefs = require("prefs").Prefs; 124 let docLink = require("prefs").Prefs.documentation_link;
129 var docLink = Prefs.documentation_link; 125 return docLink.replace(/%LINK%/g, linkID)
130 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale ); 126 .replace(/%LANG%/g, Utils.appLocale);
131 }, 127 },
132 128
133 yield: function() 129 yield()
134 { 130 {
135 } 131 }
136 }; 132 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld