Added support of locale auto detection based on browser offered language.

This commit is contained in:
Martin Edenhofer 2015-05-03 22:04:33 +02:00
parent d5252f599a
commit ea431683f5
3 changed files with 35 additions and 12 deletions

View file

@ -61,14 +61,13 @@ class App.Auth
for key, value of data.config for key, value of data.config
App.Config.set( key, value ) App.Config.set( key, value )
# refresh default collections
if data.collections
App.Collection.resetCollections( data.collections )
# empty session # empty session
App.Session.init() 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 # update model definition
if data.models if data.models
for model, attributes of data.models for model, attributes of data.models
@ -76,6 +75,15 @@ class App.Auth
App[model].attributes.push attribute.name App[model].attributes.push attribute.name
App[model].configure_attributes.push attribute 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; return false;
# clear local store # clear local store

View file

@ -124,21 +124,33 @@ class _i18nSingleton extends Spine.Module
@locale @locale
set: ( locale ) -> set: ( locale ) ->
if locale is 'en'
locale = 'en-us' # prepare locale
@locale = 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 # set if not translated should be logged
@_notTranslatedLog = @notTranslatedFeatureEnabled(locale) @_notTranslatedLog = @notTranslatedFeatureEnabled(@locale)
# set lang attribute of html tag # set lang attribute of html tag
$('html').prop( 'lang', locale.substr(0, 2) ) $('html').prop( 'lang', @locale.substr(0, 2) )
@mapString = {} @mapString = {}
App.Ajax.request( App.Ajax.request(
id: 'i18n-set-' + locale, id: 'i18n-set-' + @locale,
type: 'GET', type: 'GET',
url: App.Config.get('api_path') + '/translations/lang/' + locale, url: App.Config.get('api_path') + '/translations/lang/' + @locale,
async: false, async: false,
success: (data, status, xhr) => success: (data, status, xhr) =>

View file

@ -90,6 +90,9 @@ class SessionsController < ApplicationController
error: 'no valid session', error: 'no valid session',
config: config_frontend, config: config_frontend,
models: models, models: models,
collections: {
Locale.to_app_model => Locale.where( active: true )
}
} }
return return
end end