Browse Source

Implemented issue #1171 - The ability to set the platform default language.

Martin Edenhofer 7 years ago
parent
commit
601960f5fb

+ 1 - 1
app/assets/javascripts/app/controllers/_settings/area_item.coffee

@@ -32,7 +32,7 @@ class App.SettingsAreaItem extends App.Controller
     )
 
     new App.ControllerForm(
-      el: @el.find('.form-item'),
+      el: @el.find('.form-item')
       model: { configure_attributes: @configure_attributes, className: '' }
       autofocus: false
     )

+ 21 - 0
app/assets/javascripts/app/controllers/_settings/area_item_default_locale.coffee

@@ -0,0 +1,21 @@
+class App.SettingsAreaItemDefaultLocale extends App.SettingsAreaItem
+
+  render: =>
+
+    options = {}
+    locales = App.Locale.all()
+    for locale in locales
+      options[locale.locale] = locale.name
+    configure_attributes = [
+      { name: 'locale_default', display: '', tag: 'searchable_select', null: false, class: 'input', options: options, default: @setting.state_current.value },
+    ]
+
+    @html App.view(@template)(
+      setting: @setting
+    )
+
+    new App.ControllerForm(
+      el: @el.find('.form-item')
+      model: { configure_attributes: configure_attributes, className: '' }
+      autofocus: false
+    )

+ 23 - 0
app/assets/javascripts/app/controllers/widget/default_locale.coffee

@@ -0,0 +1,23 @@
+class DefaultLocale extends App.Controller
+  constructor: ->
+    super
+
+    check = =>
+
+      preferences = App.Session.get('preferences')
+      return if !preferences
+      return if !_.isEmpty(preferences.locale)
+      locale = App.i18n.get()
+      @ajax(
+        id:          "i18n-set-user-#{locale}"
+        type:        'PUT'
+        url:         "#{App.Config.get('api_path')}/users/preferences"
+        data:        JSON.stringify(locale: locale)
+        processData: true
+      )
+
+    App.Event.bind('auth:login', (session) =>
+      @delay(check, 3500, 'default_locale')
+    )
+
+App.Config.set('default_locale', DefaultLocale, 'Widgets')

+ 1 - 1
app/assets/javascripts/app/controllers/widget/switch_back_to_user.coffee

@@ -47,4 +47,4 @@ class Widget extends App.ControllerWidgetOnDemand
       800
     )
 
-App.Config.set( 'switch_back_to_user', Widget, 'Widgets' )
+App.Config.set('switch_back_to_user', Widget, 'Widgets')

+ 2 - 3
app/assets/javascripts/app/lib/app_post/auth.coffee

@@ -79,8 +79,7 @@ class App.Auth
       @_updateModelAttributes(data.models)
 
       # set locale
-      locale = window.navigator.userLanguage || window.navigator.language || 'en-us'
-      App.i18n.set(locale)
+      App.i18n.set(App.i18n.detectBrowserLocale())
 
       # rebuild navbar with new navbar items
       App.Event.trigger('auth')
@@ -120,7 +119,7 @@ class App.Auth
     if preferences && preferences.locale
       locale = preferences.locale
     if !locale
-      locale = window.navigator.userLanguage || window.navigator.language || 'en-us'
+      locale = App.i18n.detectBrowserLocale()
     App.i18n.set(locale)
 
     App.Event.trigger('auth:login', data.session)

+ 18 - 0
app/assets/javascripts/app/lib/app_post/i18n.coffee

@@ -80,6 +80,24 @@ class App.i18n
       _instance ?= new _i18nSingleton()
     _instance.mapTime
 
+  @detectBrowserLocale: ->
+    return 'en-us' if !window.navigator.userLanguage && !window.navigator.language
+
+    if window.navigator.languages
+      allLocales = App.Locale.all()
+      for browserLocale in window.navigator.languages
+        for localAllLocale in allLocales
+          if browserLocale is localAllLocale.locale
+            return localAllLocale.locale
+
+      for browserLocale in window.navigator.languages
+        browserLocale = browserLocale.substr(0, 2)
+        for localAllLocale in allLocales
+          if browserLocale is localAllLocale.alias
+            return localAllLocale.locale
+
+    window.navigator.userLanguage || window.navigator.language || 'en-us'
+
 class _i18nSingleton extends Spine.Module
   @include App.LogInclude
 

+ 1 - 1
app/controllers/first_steps_controller.rb

@@ -177,7 +177,7 @@ class FirstStepsController < ApplicationController
     original_user_id = UserInfo.current_user_id
     result = NotificationFactory::Mailer.template(
       template: 'test_ticket',
-      locale: agent.preferences[:locale] || 'en-us',
+      locale: agent.preferences[:locale] || Setting.get('locale_default') || 'en-us',
       objects:  {
         agent: agent,
         customer: customer,

+ 1 - 1
app/models/locale.rb

@@ -24,7 +24,7 @@ returns
 
     # read used locales based on env, e. g. export Z_LOCALES='en-us:de-de'
     if ENV['Z_LOCALES']
-      locales = Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':') )
+      locales = Locale.where(active: true, locale: ENV['Z_LOCALES'].split(':'))
     end
     locales
   end

+ 2 - 2
app/models/text_module.rb

@@ -18,7 +18,7 @@ load text modules from online
 =end
 
   def self.load(locale, overwrite_existing_item = false)
-    raise 'Got no locale' if locale.empty?
+    raise 'Got no locale' if locale.blank?
     locale = locale.split(',').first.downcase # in case of accept_language header is given
     url = "https://i18n.zammad.com/api/v1/text_modules/#{locale}"
 
@@ -67,7 +67,7 @@ push text_modules to online
       text_modules_to_push.push text_module
     end
 
-    return true if text_modules_to_push.empty?
+    return true if text_modules_to_push.blank?
 
     url = 'https://i18n.zammad.com/api/v1/text_modules/thanks_for_your_support'
 

+ 1 - 1
app/models/transaction/clearbit_enrichment.rb

@@ -31,7 +31,7 @@ class Transaction::ClearbitEnrichment
 
     config = Setting.get('clearbit_config')
     return if !config
-    return if config['api_key'].empty?
+    return if config['api_key'].blank?
 
     user = User.lookup(id: @item[:object_id])
     return if !user

Some files were not shown because too many files changed in this diff