From 783c49c00e1040e2601fcb09573640877e77c7f2 Mon Sep 17 00:00:00 2001 From: Richard Cox Date: Thu, 24 Aug 2017 17:38:45 +0100 Subject: [PATCH] Ensure we wait for $translate onReady before initialising landing page language options --- .../landing-page/landing-page.directive.js | 7 +- .../src/view/language/language.service.js | 71 ++++++++++--------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/components/app-core/frontend/src/view/landing-page/landing-page.directive.js b/components/app-core/frontend/src/view/landing-page/landing-page.directive.js index 95e3186b22..cc0be1db00 100644 --- a/components/app-core/frontend/src/view/landing-page/landing-page.directive.js +++ b/components/app-core/frontend/src/view/landing-page/landing-page.directive.js @@ -28,9 +28,10 @@ function landingPageController($scope, languageService) { var vm = this; vm.languageService = languageService; - vm.languageOptions = vm.languageService.getAll(); - languageService.getLocale(true).then(function (locale) { - vm.currentLanguage = locale; + vm.languageOptions = []; + languageService.initialised.then(function () { + vm.languageOptions = languageService.getAll(); + vm.currentLanguage = languageService.getLocale(); $scope.$watch(function () { return vm.currentLanguage; diff --git a/components/app-core/frontend/src/view/language/language.service.js b/components/app-core/frontend/src/view/language/language.service.js index 8af70eb014..d71c1d8676 100644 --- a/components/app-core/frontend/src/view/language/language.service.js +++ b/components/app-core/frontend/src/view/language/language.service.js @@ -98,22 +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(); - if (locales.length === 1) { - $log.debug('Only 1 locale found, setting to preferred + fallback: ', locales[0]); - // Attempt to set the fallback + preferred - $translate.preferredLanguage(locales[0]); - $translate.useFallbackLanguage(locales[0]); - // Ensure that the user pref is this one. This avoids instances where older, unsupported locales have not been - // cleared out of the source tree - userPreference = locales[0]; - } - - // Ensure that the locale is set to the user's pref (or forced to the only locale) - setLocale(userPreference); + var initialised = $translate.onReady().then(init); var service = { /** @@ -158,19 +143,44 @@ * fetching, false to fetch immediately * @returns {string|object} If waitForSet is true returns promise containing locale, else locale */ - getLocale: getLocale - }; + 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.getLocaleLocalised() }; - }); - } + /** + * @name initialised + * @description Promise resolving when language service has been initialised + */ + initialised: initialised + }; return service; + function init() { + // Determine if there is only one locale which the user should always use + var locales = _getLocales(); + if (locales.length === 1) { + $log.debug('Only 1 locale found, setting to preferred + fallback: ', locales[0]); + // Attempt to set the fallback + preferred + $translate.preferredLanguage(locales[0]); + $translate.useFallbackLanguage(locales[0]); + // Ensure that the user pref is this one. This avoids instances where older, unsupported locales have not been + // cleared out of the source tree + userPreference = locales[0]; + } + + // Ensure that the locale is set to the user's pref (or forced to the only locale) + var setPromise = setLocale(userPreference); + + 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.getLocaleLocalised() }; + }); + } + + return setPromise; + } + function setLocale(locale) { if (locale) { // Only store the locale if it's explicitly been set... @@ -190,7 +200,7 @@ return $q.resolve(); } - setPromise = $translate.use(locale).then(function () { + return $translate.use(locale).then(function () { $log.debug("Changed locale to '" + $translate.use() + "'"); momentLocale = momentLocale.toLowerCase(); var newMomentLocale = moment.locale(momentLocale); @@ -203,15 +213,10 @@ $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 getLocale() { + return $translate.use(); } function getAll() {