Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where previously selected language was not shown on landing page #1209

Merged
merged 1 commit into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix issue where previously selected language was not shown on landing…
… page lang selector

- 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 committed Aug 2, 2017
commit 45ebea775b1c8efd8f733e8856ee93a10bc4dbd1
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