Skip to content

Commit

Permalink
Implemented issue zammad#1171 - The ability to set the platform defau…
Browse files Browse the repository at this point in the history
…lt language.
  • Loading branch information
martini committed Nov 15, 2017
1 parent 7056593 commit 601960f
Show file tree
Hide file tree
Showing 21 changed files with 198 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
Expand Up @@ -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')
5 changes: 2 additions & 3 deletions app/assets/javascripts/app/lib/app_post/auth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions app/assets/javascripts/app/lib/app_post/i18n.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/first_steps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_ticket
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,
Expand Down
2 changes: 1 addition & 1 deletion app/models/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.to_sync

# 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
Expand Down
4 changes: 2 additions & 2 deletions app/models/text_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TextModule < ApplicationModel
=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}"

Expand Down Expand Up @@ -67,7 +67,7 @@ def self.push(locale)
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'

Expand Down
2 changes: 1 addition & 1 deletion app/models/transaction/clearbit_enrichment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def perform

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
Expand Down
2 changes: 1 addition & 1 deletion app/models/transaction/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def add_recipient_list(ticket, user, channels, type)
def human_changes(user, record)

return {} if !@item[:changes]
locale = user.preferences[:locale] || 'en-us'
locale = user.preferences[:locale] || Setting.get('locale_default') || 'en-us'

# only show allowed attributes
attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
Expand Down
2 changes: 1 addition & 1 deletion app/models/transaction/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def human_changes(record)

return {} if !@item[:changes]
user = User.find(1)
locale = user.preferences[:locale] || 'en-us'
locale = user.preferences[:locale] || Setting.get('locale_default') || 'en-us'

# only show allowed attributes
attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
Expand Down
4 changes: 2 additions & 2 deletions app/models/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.push(locale)
end
end

return true if translations_to_push.empty?
return true if translations_to_push.blank?

url = 'https://i18n.zammad.com/api/v1/translations/thanks_for_your_support'

Expand Down Expand Up @@ -108,7 +108,7 @@ def self.reset(locale)
# only push changed translations
translations = Translation.where(locale: locale)
translations.each do |translation|
if !translation.target_initial || translation.target_initial.empty?
if translation.target_initial.blank?
translation.destroy
elsif translation.target != translation.target_initial
translation.target = translation.target_initial
Expand Down
28 changes: 28 additions & 0 deletions db/migrate/20171115000001_setting_default_locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class SettingDefaultLocale < ActiveRecord::Migration[5.1]
def up

# return if it's a new setup
return if !Setting.find_by(name: 'system_init_done')

Setting.create_if_not_exists(
title: 'Locale',
name: 'locale_default',
area: 'System::Base',
description: 'Defines the system default language.',
options: {
form: [
{
name: 'locale_default',
}
],
},
state: 'en-us',
preferences: {
controller: 'SettingsAreaItemDefaultLocale',
permission: ['admin.system'],
},
frontend: true
)
end

end
19 changes: 19 additions & 0 deletions db/seeds/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@
state: 'relative',
frontend: true
)
Setting.create_if_not_exists(
title: 'Locale',
name: 'locale_default',
area: 'System::Base',
description: 'Defines the system default language.',
options: {
form: [
{
name: 'locale_default',
}
],
},
state: 'en-us',
preferences: {
controller: 'SettingsAreaItemDefaultLocale',
permission: ['admin.system'],
},
frontend: true
)
options = {}
(10..99).each do |item|
options[item] = item
Expand Down
24 changes: 12 additions & 12 deletions lib/calendar_subscriptions/tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(user, preferences)
def all

events_data = []
return events_data if @preferences.empty?
return events_data if @preferences.blank?

events_data += new_open
events_data += pending
Expand All @@ -24,7 +24,7 @@ def alarm?

alarm = false

return alarm if @preferences.empty?
return alarm if @preferences.blank?
return alarm if !@preferences[:alarm]

@preferences[:alarm]
Expand All @@ -34,11 +34,11 @@ def owner_ids(method)

owner_ids = []

return owner_ids if @preferences.empty?
return owner_ids if !@preferences[ method ]
return owner_ids if @preferences[ method ].empty?
return owner_ids if @preferences.blank?
return owner_ids if !@preferences[method]
return owner_ids if @preferences[method].blank?

preferences = @preferences[ method ]
preferences = @preferences[method]

if preferences[:own]
owner_ids = [ @user.id ]
Expand All @@ -54,7 +54,7 @@ def new_open

events_data = []
owner_ids = owner_ids(:new_open)
return events_data if owner_ids.empty?
return events_data if owner_ids.blank?

condition = {
'ticket.owner_id' => {
Expand All @@ -76,7 +76,7 @@ def new_open
condition: condition,
)

user_locale = @user.preferences['locale'] || 'en'
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us'
translated_ticket = Translation.translate(user_locale, 'ticket')

events_data = []
Expand All @@ -101,7 +101,7 @@ def pending

events_data = []
owner_ids = owner_ids(:pending)
return events_data if owner_ids.empty?
return events_data if owner_ids.blank?

condition = {
'ticket.owner_id' => {
Expand All @@ -126,7 +126,7 @@ def pending
condition: condition,
)

user_locale = @user.preferences['locale'] || 'en'
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us'
translated_ticket = Translation.translate(user_locale, 'ticket')
customer = Translation.translate(user_locale, 'customer')

Expand Down Expand Up @@ -165,7 +165,7 @@ def escalation

events_data = []
owner_ids = owner_ids(:escalation)
return events_data if owner_ids.empty?
return events_data if owner_ids.blank?

condition = {
'ticket.owner_id' => {
Expand All @@ -183,7 +183,7 @@ def escalation
condition: condition,
)

user_locale = @user.preferences['locale'] || 'en'
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us'
translated_ticket_escalation = Translation.translate(user_locale, 'ticket escalation')
customer = Translation.translate(user_locale, 'customer')

Expand Down
2 changes: 1 addition & 1 deletion lib/notification_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.template_read(data)

template_subject = nil
template_body = ''
locale = data[:locale] || 'en'
locale = data[:locale] || Setting.get('locale_default') || 'en-us'
template = data[:template]
format = data[:format]
type = data[:type]
Expand Down
2 changes: 1 addition & 1 deletion lib/notification_factory/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def self.template(data)
end

template = NotificationFactory.template_read(
locale: data[:locale] || 'en',
locale: data[:locale] || Setting.get('locale_default') || 'en-us',
template: data[:template],
format: 'html',
type: 'mailer',
Expand Down
2 changes: 1 addition & 1 deletion lib/notification_factory/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NotificationFactory::Renderer

def initialize(objects, locale, template, escape = true)
@objects = objects
@locale = locale || 'en-us'
@locale = locale || Setting.get('locale_default') || 'en-us'
@template = NotificationFactory::Template.new(template, escape)
@escape = escape
end
Expand Down
2 changes: 1 addition & 1 deletion lib/notification_factory/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.template(data)
end

template = NotificationFactory.template_read(
locale: data[:locale] || 'en',
locale: data[:locale] || Setting.get('locale_default') || 'en-us',
template: data[:template],
format: 'md',
type: 'slack',
Expand Down
Loading

0 comments on commit 601960f

Please sign in to comment.