diff --git a/client/src/app/galaxy.js b/client/src/app/galaxy.js index 86dd4f59728..e0725c1b864 100644 --- a/client/src/app/galaxy.js +++ b/client/src/app/galaxy.js @@ -77,7 +77,8 @@ GalaxyApp.prototype._init = function (options, bootstrapped) { this._initUser(options.user || {}); this.debug("GalaxyApp.user: ", this.user); - this._initUserLocale(); + this.localize._setUserLocale(this.user, this.config); + this.localize._getUserLocale(); this.debug("currentLocale: ", sessionStorage.getItem("currentLocale")); this._setUpListeners(); diff --git a/client/src/utils/localization.js b/client/src/utils/localization.js index b8f81892334..6722793f3de 100644 --- a/client/src/utils/localization.js +++ b/client/src/utils/localization.js @@ -1,23 +1,5 @@ // eslint-disable-next-line no-undef define(["i18n!nls/locale"], function (localeStrings) { - // ============================================================================= - /** Simple string replacement localization. Language data from src/nls */ - - if (Object.prototype.hasOwnProperty.call(localeStrings, "__root")) { - //console.debug( 'amdi18n+webpack localization for ' + locale + ' loaded' ); - - const locale = sessionStorage.getItem("currentLocale"); - - if (locale) { - localeStrings = - localeStrings["__" + locale] || localeStrings["__" + locale.split("-")[0]] || localeStrings.__root; - } - - // } else { - // console.debug( 'i18n+requirejs localization for ' + locale + ' loaded' ); - } - // TODO: when this is no longer necessary remove this, i18n.js, and the resolveModule in config - // ----------------------------------------------------------------------------- /** Attempt to get a localized string for strToLocalize. If not found, return * the original strToLocalize. @@ -42,6 +24,56 @@ define(["i18n!nls/locale"], function (localeStrings) { }; localize.cacheNonLocalized = false; + localize._setUserLocale = function _setUserLocale(user, config) { + // Choose best locale + const global_locale = + config.default_locale && config.default_locale != "auto" ? config.default_locale.toLowerCase() : false; + + let extra_user_preferences = {}; + if (user && user.attributes.preferences && "extra_user_preferences" in user.attributes.preferences) { + extra_user_preferences = JSON.parse(user.attributes.preferences.extra_user_preferences); + } + + let user_locale = + "localization|locale" in extra_user_preferences + ? extra_user_preferences["localization|locale"].toLowerCase() + : false; + + if (user_locale == "auto") { + user_locale = false; + } + + const nav_locale = + typeof navigator === "undefined" + ? "__root" + : (navigator.language || navigator.userLanguage || "__root").toLowerCase(); + + const locale = user_locale || nav_locale || global_locale; + + sessionStorage.setItem("currentLocale", locale); + }; + // ============================================================================= + /** Simple string replacement localization. Language data from src/nls */ + + localize._getUserLocale = function _getUserLocale(user, config) { + let locale = null; + if (Object.prototype.hasOwnProperty.call(localeStrings, "__root")) { + //console.debug( 'amdi18n+webpack localization for ' + locale + ' loaded' ); + + locale = sessionStorage.getItem("currentLocale"); + + if (locale) { + localeStrings = + localeStrings["__" + locale] || localeStrings["__" + locale.split("-")[0]] || localeStrings.__root; + } + + // } else { + // console.debug( 'i18n+requirejs localization for ' + locale + ' loaded' ); + } + return locale; + // TODO: when this is no longer necessary remove this, i18n.js, and the resolveModule in config + }; + // ============================================================================= return localize; });