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

View file

@ -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) =>

View file

@ -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