Skip to content

Commit

Permalink
use session to store current locale
Browse files Browse the repository at this point in the history
  • Loading branch information
abretaud committed Jan 19, 2018
1 parent c5fa911 commit 1c2bd5f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
31 changes: 31 additions & 0 deletions client/galaxy/scripts/galaxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ GalaxyApp.prototype._init = function __init(options, bootstrapped) {
self._initUser(options.user || {});
self.debug("GalaxyApp.user: ", self.user);

self._initUserLocale();
self.debug("currentLocale: ", sessionStorage.getItem("currentLocale"));

self._setUpListeners();
self.trigger("ready", self);

Expand Down Expand Up @@ -171,6 +174,34 @@ GalaxyApp.prototype._initLocale = function _initLocale(options) {
return self;
};

/** add the localize fn to this object and the window namespace (as '_l') */
GalaxyApp.prototype._initUserLocale = function _initUserLocale(options) {
var self = this;

// Choose best locale
var global_locale = self.config.default_locale ? self.config.default_locale.toLowerCase() : false;

var extra_user_preferences = {};
if (self.user && self.user.attributes.preferences && 'extra_user_preferences' in self.user.attributes.preferences) {
extra_user_preferences = JSON.parse(self.user.attributes.preferences.extra_user_preferences);
}

var user_locale = 'localization|locale' in extra_user_preferences ? extra_user_preferences['localization|locale'].toLowerCase() : false;

if (user_locale == 'auto') {
user_locale = false;
}

var nav_locale =
typeof navigator === "undefined"
? "__root"
: (navigator.language || navigator.userLanguage || "__root").toLowerCase();

locale = user_locale || global_locale || nav_locale;

sessionStorage.setItem('currentLocale', locale);
};

/** set up the current user as a Backbone model (mvc/user/user-model) */
GalaxyApp.prototype._initUser = function _initUser(userJSON) {
var self = this;
Expand Down
32 changes: 8 additions & 24 deletions client/galaxy/scripts/utils/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,19 @@ define(["i18n!nls/locale"], function(localeStrings) {
// =============================================================================
/** Simple string replacement localization. Language data from galaxy/scripts/nls */

function onloadLocaleConfig() {
// Wait until Galaxy.config is loaded.
if (Galaxy.config && localeStrings.hasOwnProperty("__root")) {
var global_locale = Galaxy.config.default_locale ? Galaxy.config.default_locale.toLowerCase() : false;
if (localeStrings.hasOwnProperty("__root")) {
//console.debug( 'amdi18n+webpack localization for ' + locale + ' loaded' );

var extra_user_preferences = {};
if (Galaxy.user && Galaxy.user.attributes.preferences && 'extra_user_preferences' in Galaxy.user.attributes.preferences) {
extra_user_preferences = JSON.parse(Galaxy.user.attributes.preferences.extra_user_preferences);
}

var user_locale = 'localization|locale' in extra_user_preferences ? extra_user_preferences['localization|locale'].toLowerCase() : false;

var nav_locale =
typeof navigator === "undefined"
? "__root"
: (navigator.language || navigator.userLanguage || "__root").toLowerCase();

console.debug('global_locale: ' + global_locale);
console.debug('user_locale: ' + user_locale);
console.debug('nav_locale: ' + nav_locale);
locale = sessionStorage.getItem("currentLocale");

if (locale) {
localeStrings =
localeStrings["__" + user_locale] || localeStrings["__" + global_locale] || localeStrings["__" + nav_locale] || localeStrings["__" + nav_locale.split("-")[0]] || localeStrings.__root;
} else {
setTimeout(onloadLocaleConfig, 100);
localeStrings["__" + locale] || localeStrings["__" + locale.split("-")[0]] || localeStrings.__root;
}
}
onloadLocaleConfig();

// } 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

// -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions lib/galaxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ def __init__(self, **kwargs):
self.biostar_enable_bug_reports = string_as_bool(kwargs.get('biostar_enable_bug_reports', True))
self.biostar_never_authenticate = string_as_bool(kwargs.get('biostar_never_authenticate', False))
self.pretty_datetime_format = expand_pretty_datetime_format(kwargs.get('pretty_datetime_format', '$locale (UTC)'))
self.default_locale = kwargs.get('default_locale', None)
self.master_api_key = kwargs.get('master_api_key', None)
if self.master_api_key == "changethis": # default in sample config file
raise ConfigurationError("Insecure configuration, please change master_api_key to something other than default (changethis)")
Expand Down
1 change: 1 addition & 0 deletions lib/galaxy/managers/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def _defaults_to(default):
'lims_doc_url' : _defaults_to("https://usegalaxy.org/u/rkchak/p/sts"),
'biostar_url' : _defaults_to(''),
'biostar_url_redirect' : lambda *a, **c: self.url_for(controller='biostar', action='biostar_redirect', qualified=True),
'default_locale' : _defaults_to(self.app.config.default_locale),

'enable_beta_ts_api_install' : _defaults_to(False),
'enable_communication_server' : _defaults_to(False),
Expand Down
1 change: 1 addition & 0 deletions templates/webapps/galaxy/galaxy.masthead.mako
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'lims_doc_url' : app.config.get("lims_doc_url", "https://usegalaxy.org/u/rkchak/p/sts"),
'biostar_url' : app.config.biostar_url,
'biostar_url_redirect' : h.url_for( controller='biostar', action='biostar_redirect', qualified=True ),
'default_locale' : app.config.get("default_locale", "auto"),
'support_url' : app.config.get("support_url", "https://galaxyproject.org/support"),
'search_url' : app.config.get("search_url", "http://galaxyproject.org/search/"),
'mailing_lists' : app.config.get("mailing_lists", "https://galaxyproject.org/mailing-lists"),
Expand Down

0 comments on commit 1c2bd5f

Please sign in to comment.