Skip to content

Commit c9add4f

Browse files
authored
Merge pull request #2539 from uProxy/jab-fix-issue-1693
initialize language setting based on browser settings
2 parents ce80965 + c874e17 commit c9add4f

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

src/generic_core/globals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export var settings :uproxy_core_api.GlobalSettings = {
4949
splashState: 0,
5050
statsReportingEnabled: false,
5151
consoleFilter: loggingprovider.Level.warn,
52-
language: 'en',
52+
language: null, // sentinel indicating lang should be calculated from browser settings
5353
force_message_version: 0, // zero means "don't override"
5454
quiverUserName: '',
5555
showCloud: false,

src/generic_ui/polymer/root.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// <reference path='../../../../third_party/polymer/polymer.d.ts' />
33
/// <reference path='../../../../third_party/typings/browser.d.ts' />
44

5+
import _ = require('lodash');
56
import social = require('../../interfaces/social');
67
import translator = require('../scripts/translator');
78
import ui_types = require('../../interfaces/ui');
@@ -148,7 +149,7 @@ Polymer({
148149
var browserCustomElement = document.createElement(ui.browserApi.browserSpecificElement);
149150
this.$.browserElementContainer.appendChild(browserCustomElement);
150151
}
151-
this.updateDirectionality();
152+
this.setDirectionality();
152153
},
153154
tabSelected: function(e :Event) {
154155
if (this.ui.isSharingDisabled &&
@@ -214,15 +215,15 @@ Polymer({
214215
this.isSharingEnabledWithOthers = trustedContacts.length > 0;
215216
}
216217
},
217-
updateDirectionality: function() {
218-
// Update the directionality of the UI.
219-
for (var i = 0; i < RTL_LANGUAGES.length; i++) {
220-
if (RTL_LANGUAGES[i] == model.globalSettings.language.substring(0,2)) {
221-
this.dir = 'rtl';
222-
return;
223-
}
218+
/*
219+
* Set our `dir` value to 'ltr' or 'rtl' based on current language.
220+
*/
221+
setDirectionality: function() {
222+
if (_.includes(RTL_LANGUAGES, model.globalSettings.language)) {
223+
this.dir = 'rtl';
224+
} else {
225+
this.dir = 'ltr';
224226
}
225-
this.dir = 'ltr';
226227
},
227228
languageChanged: function(oldLanguage :string, newLanguage :string) {
228229
if (oldLanguage && oldLanguage !== newLanguage) {

src/generic_ui/scripts/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Model {
6363
allowNonUnicast: false,
6464
statsReportingEnabled: false,
6565
consoleFilter: 0,
66-
language: 'en',
66+
language: null, // sentinel indicating lang should be calculated from browser settings
6767
force_message_version: 0,
6868
quiverUserName: '',
6969
showCloud: false,

src/generic_ui/scripts/translator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ export const i18n_t = (placeholder :string, params ?:any): string => {
5959
};
6060

6161
export const i18n_setLng = i18next.setLng;
62+
63+
export const i18n_languagesAvailable :string[] = Object.keys(window.i18nResources);

src/generic_ui/scripts/ui.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Common User Interface state holder and changer.
1010
*/
1111

12+
import _ = require('lodash');
1213
import ui_constants = require('../../interfaces/ui');
1314
import background_ui = require('./background_ui');
1415
import CoreConnector = require('./core_connector');
@@ -78,6 +79,12 @@ export function getImageData(userId: string, oldImageData: string,
7879
jdenticon.toSvg(userIdHash, 100).replace(/#/g, '%23') + '\'';
7980
}
8081

82+
83+
/* Suppress `error TS2339: Property 'languages' does not exist on type
84+
* 'Navigator'` triggered by `navigator.languages` reference below.
85+
*/
86+
declare var navigator :any;
87+
8188
/**
8289
* The User Interface class.
8390
*
@@ -1071,6 +1078,21 @@ export class UserInterface implements ui_constants.UiApi {
10711078
this.model.networkNames = state.networkNames;
10721079
this.model.cloudProviderNames = state.cloudProviderNames;
10731080
this.availableVersion = state.availableVersion;
1081+
if (!state.globalSettings.language) {
1082+
// Set state.globalSettings.language based on browser settings:
1083+
// Choose the first language in navigator.languages we have available.
1084+
let lang :string;
1085+
try {
1086+
lang = _(navigator.languages).map((langCode :string) => {
1087+
return langCode.substring(0, 2).toLowerCase(); // Normalize
1088+
}).find((langCode :string) => { // Return first lang we have available.
1089+
return _.includes(translator_module.i18n_languagesAvailable, langCode);
1090+
});
1091+
} catch (e) {
1092+
lang = 'en';
1093+
}
1094+
state.globalSettings.language = lang || 'en';
1095+
}
10741096
if (state.globalSettings.language !== this.model.globalSettings.language) {
10751097
this.updateLanguage(state.globalSettings.language);
10761098
}

0 commit comments

Comments
 (0)