Skip to content

Commit

Permalink
Merge pull request #1234 from SUSE/fix-landing-page-localise
Browse files Browse the repository at this point in the history
Ensure we wait for $translate onReady before initialising landing page language options
  • Loading branch information
nwmac authored Aug 25, 2017
2 parents 558d5da + 783c49c commit 5643f21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 38 additions & 33 deletions components/app-core/frontend/src/view/language/language.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -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...
Expand All @@ -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);
Expand All @@ -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() {
Expand Down

0 comments on commit 5643f21

Please sign in to comment.