Skip to content

Commit

Permalink
Merge pull request #58 in EXTENSIONS/browser-extension from feature/i…
Browse files Browse the repository at this point in the history
…ssues/889-locale to master

* commit 'd681cb48db29258bbc58543e6afeca71ce3c30a6':
  Fix #889 Locale detection fixed
  Fixed locale detection
  Fixed locale
  • Loading branch information
Aleksandr Tropnikov committed Feb 15, 2018
2 parents 146365b + d681cb4 commit ccf3346
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Extension/browser/firefox/lib/api/common-api.js
Expand Up @@ -84,7 +84,7 @@

adguard.app = (function () {

var locale = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry).getSelectedLocale('global').substring(0, 2).toLowerCase();
var locale = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry).getSelectedLocale('global');

var getId = function () {
return id;
Expand Down
7 changes: 0 additions & 7 deletions Extension/browser/safari/lib/content-script/common-script.js
Expand Up @@ -80,7 +80,6 @@
adguard.i18n = (function () {

var defaultLocale = 'en';
var supportedLocales = ['ru', 'en', 'tr', 'uk', 'de', 'pl', 'pt_BR', 'pt_PT', 'ko', 'zh_CN', 'sr', 'fr', 'sk', 'hy', 'es', 'es_419', 'it', 'id'];

var _messages = null;
var _defaultMessages = null;
Expand All @@ -92,12 +91,6 @@
if (parts[1]) {
locale += '_' + parts[1].toUpperCase();
}
if (supportedLocales.indexOf(locale) < 0) {
locale = parts[0];
}
if (supportedLocales.indexOf(locale) < 0) {
locale = defaultLocale;
}
return locale;
})();

Expand Down
10 changes: 6 additions & 4 deletions Extension/lib/filter/locale-detect.js
Expand Up @@ -117,12 +117,14 @@
*/
function detectLanguage(language) {

if (!language || language == "und") {
/**
* For an unknown language "und" will be returned
* https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/detectLanguage
*/
if (!language || language === "und") {
return;
}

language = language.trim().toLowerCase();

browsingLanguages.push({
language: language,
time: Date.now()
Expand All @@ -132,7 +134,7 @@
}

var history = browsingLanguages.filter(function (h) {
return h.language == language;
return h.language === language;
});

if (history.length >= SUCCESS_HIT_COUNT) {
Expand Down
32 changes: 19 additions & 13 deletions Extension/lib/filter/subscription.js
Expand Up @@ -188,10 +188,12 @@ adguard.subscriptions = (function (adguard) {
function applyGroupLocalization(group, i18nMetadata) {
var groupId = group.groupId;
var localizations = i18nMetadata[groupId];
var locale = adguard.app.getLocale();
if (localizations && locale in localizations) {
if (localizations) {
var locale = adguard.utils.i18n.normalize(localizations, adguard.app.getLocale());
var localization = localizations[locale];
group.groupName = localization.name;
if (localization) {
group.groupName = localization.name;
}
}
}

Expand All @@ -204,11 +206,13 @@ adguard.subscriptions = (function (adguard) {
function applyFilterLocalization(filter, i18nMetadata) {
var filterId = filter.filterId;
var localizations = i18nMetadata[filterId];
var locale = adguard.app.getLocale();
if (localizations && locale in localizations) {
if (localizations) {
var locale = adguard.utils.i18n.normalize(localizations, adguard.app.getLocale());
var localization = localizations[locale];
filter.name = localization.name;
filter.description = localization.description;
if (localization) {
filter.name = localization.name;
filter.description = localization.description;
}
}
}

Expand Down Expand Up @@ -256,20 +260,22 @@ adguard.subscriptions = (function (adguard) {
/**
* Gets list of filters for the specified languages
*
* @param lang Language to check
* @param locale Locale to check
* @returns List of filters identifiers
*/
var getFilterIdsForLanguage = function (lang) {
if (!lang) {
var getFilterIdsForLanguage = function (locale) {
if (!locale) {
return [];
}
lang = lang.substring(0, 2).toLowerCase();
var filterIds = [];
for (var i = 0; i < filters.length; i++) {
var filter = filters[i];
var languages = filter.languages;
if (languages && languages.indexOf(lang) >= 0) {
filterIds.push(filter.filterId);
if (languages && languages.length > 0) {
var language = adguard.utils.i18n.normalize(languages, locale);
if (language) {
filterIds.push(filter.filterId);
}
}
}
return filterIds;
Expand Down
11 changes: 2 additions & 9 deletions Extension/lib/ui-service.js
Expand Up @@ -79,15 +79,8 @@ adguard.ui = (function (adguard) { // jshint ignore:line

var extensionStoreLink = (function () {

var urlBuilder = ["http://adguard.com/"];

if (adguard.app.getLocale() === "ru") {
urlBuilder.push("ru");
} else {
urlBuilder.push("en");
}
urlBuilder.push("/extension-page.html?browser=");

var urlBuilder = ["https://adguard.com/extension-page.html"];
urlBuilder.push("?browser=");
if (adguard.utils.browser.isOperaBrowser()) {
urlBuilder.push("opera");
} else if (adguard.utils.browser.isFirefoxBrowser()) {
Expand Down
56 changes: 56 additions & 0 deletions Extension/lib/utils/common.js
Expand Up @@ -57,6 +57,7 @@ adguard.utils = (function () {
browser: null, // BrowserUtils
filters: null, // FilterUtils,
workaround: null, // WorkaroundUtils
i18n: null, // I18nUtils
StopWatch: null,
Promise: null // Deferred,
};
Expand Down Expand Up @@ -639,6 +640,61 @@ adguard.utils = (function () {

})(adguard.utils);

/**
* Simple i18n utils
*/
(function (api) {

function isArrayElement(array, elem) {
return array.indexOf(elem) >= 0;
}

function isObjectKey(object, key) {
return key in object;
}

api.i18n = {

/**
* Tries to find locale in the given collection of locales
* @param locales Collection of locales (array or object)
* @param locale Locale (e.g. en, en_GB, pt_BR)
* @returns matched locale from the locales collection or null
*/
normalize: function (locales, locale) {

if (!locale) {
return null;
}

// Transform Language-Country => Language_Country
locale = locale.replace("-", "_");

var search;

if (api.collections.isArray(locales)) {
search = isArrayElement;
} else {
search = isObjectKey;
}

if (search(locales, locale)) {
return locale;
}

// Try to search by the language
var parts = locale.split('_');
var language = parts[0];
if (search(locales, language)) {
return language;
}

return null;
}
};

})(adguard.utils);

/**
* Unload handler. When extension is unload then 'fireUnload' is invoked.
* You can add own handler with method 'when'
Expand Down

0 comments on commit ccf3346

Please sign in to comment.