From ea431683f59e87d804b042651957245a7f9eb577 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 3 May 2015 22:04:33 +0200 Subject: [PATCH] Added support of locale auto detection based on browser offered language. --- .../app/lib/app_post/auth.js.coffee | 18 +++++++++---- .../app/lib/app_post/i18n.js.coffee | 26 ++++++++++++++----- app/controllers/sessions_controller.rb | 3 +++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/auth.js.coffee b/app/assets/javascripts/app/lib/app_post/auth.js.coffee index 1dac8ef8e..5ffb3093c 100644 --- a/app/assets/javascripts/app/lib/app_post/auth.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/auth.js.coffee @@ -61,14 +61,13 @@ class App.Auth for key, value of data.config App.Config.set( key, value ) + # refresh default collections + if data.collections + App.Collection.resetCollections( data.collections ) + # empty session App.Session.init() - # rebuild navbar with new navbar items - App.Event.trigger( 'auth' ) - App.Event.trigger( 'auth:logout' ) - App.Event.trigger( 'ui:rerender' ) - # update model definition if data.models for model, attributes of data.models @@ -76,6 +75,15 @@ class App.Auth App[model].attributes.push attribute.name App[model].configure_attributes.push attribute + # set locale + locale = window.navigator.userLanguage || window.navigator.language || 'en-us' + App.i18n.set( locale ) + + # rebuild navbar with new navbar items + App.Event.trigger( 'auth' ) + App.Event.trigger( 'auth:logout' ) + App.Event.trigger( 'ui:rerender' ) + return false; # clear local store diff --git a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee index 0a9eec249..8f7dc91b7 100644 --- a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee @@ -124,21 +124,33 @@ class _i18nSingleton extends Spine.Module @locale set: ( locale ) -> - if locale is 'en' - locale = 'en-us' - @locale = locale + + # prepare locale + localeToSet = locale.toLowerCase() + + # try aliases + locales = App.Locale.all() + for locale in locales + if locale.alias is locale + localeToSet = locale.locale + + # check if locale need to be changed + return if localeToSet is @locale + + # set locale + @locale = localeToSet # set if not translated should be logged - @_notTranslatedLog = @notTranslatedFeatureEnabled(locale) + @_notTranslatedLog = @notTranslatedFeatureEnabled(@locale) # set lang attribute of html tag - $('html').prop( 'lang', locale.substr(0, 2) ) + $('html').prop( 'lang', @locale.substr(0, 2) ) @mapString = {} App.Ajax.request( - id: 'i18n-set-' + locale, + id: 'i18n-set-' + @locale, type: 'GET', - url: App.Config.get('api_path') + '/translations/lang/' + locale, + url: App.Config.get('api_path') + '/translations/lang/' + @locale, async: false, success: (data, status, xhr) => diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index df2b9aec4..75acdd1d7 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -90,6 +90,9 @@ class SessionsController < ApplicationController error: 'no valid session', config: config_frontend, models: models, + collections: { + Locale.to_app_model => Locale.where( active: true ) + } } return end