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

Ensure we wait for $translate onReady before initialising landing page language options #1234

Merged
merged 1 commit into from
Aug 25, 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
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