Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Fix issue where previously selected language was not shown on landing…
Browse files Browse the repository at this point in the history
… page lang selector (#1209)

- We'd fetch the lang before previously selected lang was applied to $translate
- Probably a factor due to the landing page changes, we show the select lang directive quicker than before
  • Loading branch information
richard-cox authored and Irfan Habib committed Aug 4, 2017
1 parent d96eb1b commit 9162ccb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@
};
}

function landingPageController($scope, $translate, languageService) {
function landingPageController($scope, languageService) {
var vm = this;
vm.languageService = languageService;
vm.languageOptions = vm.languageService.getAll();
vm.currentLanguage = $translate.use();
$scope.$watch(function () {
return vm.currentLanguage;
}, function (newValue, oldValue) {
if (newValue !== oldValue) {
languageService.setLocale(newValue);
}
languageService.getLocale(true).then(function (locale) {
vm.currentLanguage = locale;

$scope.$watch(function () {
return vm.currentLanguage;
}, function (newValue, oldValue) {
if (newValue !== oldValue) {
languageService.setLocale(newValue);
}
});
});

}
})();
56 changes: 37 additions & 19 deletions components/app-core/frontend/src/view/language/language.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
function languageServiceFactory($q, $log, $translate, frameworkAsyncTaskDialog, modelManager, appLocalStorage) {

var userPreference = appLocalStorage.getItem(localeStorageId);
var setPromise = $q.resolve();

// Determine if there is only one locale which the user should always use
var locales = _getLocales();
Expand All @@ -112,9 +113,7 @@
}

// Ensure that the locale is set to the user's pref (or forced to the only locale)
_setLocale({
currentLocale: userPreference
});
setLocale(userPreference);

var service = {
/**
Expand All @@ -123,48 +122,55 @@
* @returns {boolean} true if the language can be selected
*/
enableLanguageSelection: enableLanguageSelection,

/**
* @name showLanguageSelection
* @description Display Language Selection Dialog
* @returns {*} frameworkAsyncTaskDialog
*/
showLanguageSelection: showLanguageSelection,

/**
* @name getCurrent
* @description Gets the current language
* @returns {string} the current language
* @name getLocaleLocalised
* @description Gets the current localised locale
* @returns {string} the current localised locale
*/
getCurrent: getCurrent,
getLocaleLocalised: getLocaleLocalised,

/**
* @name getAll
* @description Get all languages
* @description Get all supported languages
* @returns {array} collection of languages with an object for each containing name and label
*/
getAll: getAll,

/**
* @name setLocale
* @description Set the locale
* @param (string) locale - locale string
*/
setLocale: setLocale
setLocale: setLocale,

/**
* @name getLocale
* @description Get the locale
* @param (boolean) waitForSet - true if the call should wait for any previous setLocale calls to complete before
* fetching, false to fetch immediately
* @returns {string|object} If waitForSet is true returns promise containing locale, else locale
*/
getLocale: getLocale
};

if (enableLanguageSelection()) {
var userNavModel = modelManager.retrieve('app.model.navigation').user;
var item = userNavModel.addMenuItemFunction('select-language', service.showLanguageSelection, 'menu.language', 2);
item.setTextValues(function () {
return { current: service.getCurrent() };
return { current: service.getLocaleLocalised() };
});
}

return service;

function _setLocale(data) {
var locale = data.currentLocale;
return setLocale(locale);
}

function setLocale(locale) {
if (locale) {
// Only store the locale if it's explicitly been set...
Expand All @@ -184,7 +190,7 @@
return $q.resolve();
}

return $translate.use(locale).then(function () {
setPromise = $translate.use(locale).then(function () {
$log.debug("Changed locale to '" + $translate.use() + "'");
momentLocale = momentLocale.toLowerCase();
var newMomentLocale = moment.locale(momentLocale);
Expand All @@ -197,6 +203,15 @@
$log.warn("Failed to load language for locale '" + locale + "', falling back to '" + $translate.use() + "'");
return $q.reject(reason);
});
return setPromise;
}

function getLocale(waitForSet) {
if (waitForSet && setPromise) {
return setPromise.then($translate.use);
} else {
return $translate.use();
}
}

function getAll() {
Expand Down Expand Up @@ -237,12 +252,15 @@
currentLocale: $translate.use()
}
},
_setLocale
function (data) {
var locale = data.currentLocale;
return setLocale(locale);
}
);
}

function getCurrent() {
return $translate.instant('locales.' + $translate.use());
function getLocaleLocalised() {
return $translate.instant('locales.' + getLocale(false));
}
}

Expand Down

0 comments on commit 9162ccb

Please sign in to comment.