Fixes issue #2169 - Time format always english if generated from system.

This commit is contained in:
Mantas Masalskis 2020-01-27 10:28:17 +01:00 committed by Thorsten Eckel
parent b294708ec0
commit 7fab92d074
17 changed files with 117 additions and 45 deletions

View file

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

View file

@ -119,6 +119,14 @@ all:
result.data result.data
end end
# Default system locale
#
# @example
# Locale.default
def self.default
Setting.get('locale_default') || 'en-us'
end
private_class_method def self.to_database(data) private_class_method def self.to_database(data)
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
data.each do |locale| data.each do |locale|

View file

@ -1462,7 +1462,6 @@ result
# get subject # get subject
subject = NotificationFactory::Mailer.template( subject = NotificationFactory::Mailer.template(
templateInline: value['subject'], templateInline: value['subject'],
locale: 'en-en',
objects: objects, objects: objects,
quote: false, quote: false,
) )
@ -1470,7 +1469,6 @@ result
body = NotificationFactory::Mailer.template( body = NotificationFactory::Mailer.template(
templateInline: value['body'], templateInline: value['body'],
locale: 'en-en',
objects: objects, objects: objects,
quote: true, quote: true,
) )
@ -1561,8 +1559,6 @@ result
objects = build_notification_template_objects(article) objects = build_notification_template_objects(article)
body = NotificationFactory::Renderer.new( body = NotificationFactory::Renderer.new(
objects: objects, objects: objects,
locale: 'en-en',
timezone: Setting.get('timezone_default'),
template: value['body'], template: value['body'],
escape: false escape: false
).render.html2text.tr(' ', ' ') # convert non-breaking space to simple space ).render.html2text.tr(' ', ' ') # convert non-breaking space to simple space

View file

@ -235,7 +235,7 @@ class Transaction::Notification
return {} if !@item[:changes] return {} if !@item[:changes]
locale = user.preferences[:locale] || Setting.get('locale_default') || 'en-us' locale = user.locale
# only show allowed attributes # only show allowed attributes
attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user) attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)

View file

@ -86,8 +86,8 @@ class Transaction::Slack
result = NotificationFactory::Slack.template( result = NotificationFactory::Slack.template(
template: template, template: template,
locale: user[:preferences][:locale] || Setting.get('locale_default'), locale: user.locale,
timezone: user[:preferences][:timezone] || Setting.get('timezone_default'), timezone: Setting.get('timezone_default'),
objects: { objects: {
ticket: ticket, ticket: ticket,
article: article, article: article,

View file

@ -237,14 +237,15 @@ or
end end
end end
record = Translation.where(locale: locale, source: 'timestamp', format: 'time').pluck(:target).first
return timestamp.to_s if !record
begin begin
timestamp = timestamp.in_time_zone(timezone) timestamp = timestamp.in_time_zone(timezone)
rescue rescue
return timestamp.to_s return timestamp.to_s
end end
record = Translation.where(locale: locale, source: 'timestamp', format: 'time').pluck(:target).first
return timestamp.to_s if !record
record.sub!('dd', format('%02d', timestamp.day)) record.sub!('dd', format('%02d', timestamp.day))
record.sub!('d', timestamp.day.to_s) record.sub!('d', timestamp.day.to_s)
record.sub!('mm', format('%02d', timestamp.month)) record.sub!('mm', format('%02d', timestamp.month))

View file

@ -916,9 +916,9 @@ try to find correct name
firstname.blank? && lastname.blank? firstname.blank? && lastname.blank?
end end
# get locale of user or system if user's own is not set # get locale identifier of user or system if user's own is not set
def locale def locale
preferences.fetch(:locale) { Setting.get('locale_default') } preferences.fetch(:locale) { Locale.default }
end end
private private

View file

@ -76,7 +76,7 @@ class CalendarSubscriptions::Tickets
condition: condition, condition: condition,
) )
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us' user_locale = @user.locale
translated_ticket = Translation.translate(user_locale, 'ticket') translated_ticket = Translation.translate(user_locale, 'ticket')
events_data = [] events_data = []
@ -126,7 +126,7 @@ class CalendarSubscriptions::Tickets
condition: condition, condition: condition,
) )
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us' user_locale = @user.locale
translated_ticket = Translation.translate(user_locale, 'ticket') translated_ticket = Translation.translate(user_locale, 'ticket')
customer = Translation.translate(user_locale, 'customer') customer = Translation.translate(user_locale, 'customer')
@ -183,7 +183,7 @@ class CalendarSubscriptions::Tickets
condition: condition, condition: condition,
) )
user_locale = @user.preferences['locale'] || Setting.get('locale_default') || 'en-us' user_locale = @user.locale
translated_ticket_escalation = Translation.translate(user_locale, 'ticket escalation') translated_ticket_escalation = Translation.translate(user_locale, 'ticket escalation')
customer = Translation.translate(user_locale, 'customer') customer = Translation.translate(user_locale, 'customer')

View file

@ -5,7 +5,7 @@ class ExcelSheet
@header = header @header = header
@records = records @records = records
@timezone = timezone.presence || Setting.get('timezone_default') @timezone = timezone.presence || Setting.get('timezone_default')
@locale = locale || 'en-en' @locale = locale || Locale.default
@tempfile = Tempfile.new('excel-export.xls') @tempfile = Tempfile.new('excel-export.xls')
@workbook = WriteExcel.new(@tempfile) @workbook = WriteExcel.new(@tempfile)
@worksheet = @workbook.add_worksheet @worksheet = @workbook.add_worksheet

View file

@ -53,7 +53,7 @@ returns
private_class_method :template_path private_class_method :template_path
def self.template_filenames(data) def self.template_filenames(data)
locale = data[:locale] || Setting.get('locale_default') || 'en-us' locale = data[:locale] || Locale.default
[locale, locale[0, 2], 'en'] [locale, locale[0, 2], 'en']
.uniq .uniq

View file

@ -295,7 +295,7 @@ returns
end end
template = NotificationFactory.template_read( template = NotificationFactory.template_read(
locale: data[:locale] || Setting.get('locale_default') || 'en-us', locale: data[:locale] || Locale.default,
template: data[:template], template: data[:template],
format: data[:format] || 'html', format: data[:format] || 'html',
type: 'mailer', type: 'mailer',

View file

@ -26,8 +26,8 @@ examples how to use
=end =end
def initialize(objects:, locale: nil, timezone: nil, template:, escape: true) def initialize(objects:, locale: nil, timezone: nil, template:, escape: true)
@objects = objects @objects = objects
@locale = locale || Setting.get('locale_default') || 'en-us' @locale = locale || Locale.default
@timezone = timezone || Setting.get('timezone_default') @timezone = timezone || Setting.get('timezone_default')
@template = NotificationFactory::Template.new(template, escape) @template = NotificationFactory::Template.new(template, escape)
@escape = escape @escape = escape

View file

@ -33,7 +33,7 @@ returns
end end
template = NotificationFactory.template_read( template = NotificationFactory.template_read(
locale: data[:locale] || Setting.get('locale_default') || 'en-us', locale: data[:locale] || Locale.default,
template: data[:template], template: data[:template],
format: 'md', format: 'md',
type: 'slack', type: 'slack',

View file

@ -0,0 +1,23 @@
require 'rails_helper'
RSpec.describe Locale, type: :model do
describe 'Class methods:' do
describe '.default' do
context 'with default locale' do
before { Setting.set('locale_default', 'foo') }
it 'returns the system-wide default locale' do
expect(described_class.default).to eq('foo')
end
end
context 'without default locale' do
before { Setting.set('locale_default', nil) }
it 'returns en-us' do
expect(described_class.default).to eq('en-us')
end
end
end
end
end

View file

@ -38,11 +38,11 @@ RSpec.describe Translation do
end end
it 'not_existing with timestamp as string' do it 'not_existing with timestamp as string' do
expect(described_class.timestamp('not_existing', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('2018-10-10 10:00:00 UTC') expect(described_class.timestamp('not_existing', 'Europe/Berlin', '2018-10-10T10:00:00Z0')).to eq('2018-10-10 12:00:00 +0200')
end end
it 'not_existing with time object' do it 'not_existing with time object' do
expect(described_class.timestamp('not_existing', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('2018-10-10 10:00:00 UTC') expect(described_class.timestamp('not_existing', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))).to eq('2018-10-10 12:00:00 +0200')
end end
it 'not_existing with invalid timestamp string' do it 'not_existing with invalid timestamp string' do

View file

@ -3,29 +3,63 @@ require 'rails_helper'
RSpec.describe Trigger do RSpec.describe Trigger do
describe 'sms' do describe 'sms' do
before do
Translation.fetch(locale)
Setting.set('locale_default', locale)
Setting.set('timezone_default', time_zone)
end
it 'sends interpolated, html-free SMS' do let(:time_zone) { 'Europe/Vilnius' }
agent = create(:agent_user) let(:locale) { 'de-de' }
another_agent = create(:admin_user, mobile: '+37061010000')
Group.lookup(id: 1).users << another_agent
create(:channel, area: 'Sms::Notification') context 'sends interpolated, html-free SMS' do
create(:trigger, before do
disable_notification: false, another_agent = create(:admin_user, mobile: '+37061010000')
perform: { Group.lookup(id: 1).users << another_agent
'notification.sms': {
recipient: 'ticket_agents',
body: 'space&nbsp;between #{ticket.title}', # rubocop:disable Lint/InterpolationCheck
}
})
ticket = create(:ticket, group: Group.lookup(id: 1), created_by_id: agent.id) create(:channel, area: 'Sms::Notification')
Observer::Transaction.commit create(:trigger,
disable_notification: false,
perform: {
'notification.sms': {
recipient: 'ticket_agents',
body: message_body,
}
})
end
triggered_article = Ticket::Article.last let(:message_body) { 'space&nbsp;between #{ticket.title} #{ticket.created_at}' } # rubocop:disable Lint/InterpolationCheck
expect(triggered_article.body).to match(/space between/) let(:agent) { create(:agent_user) }
expect(triggered_article.body).to match(ticket.title) let(:ticket) do
ticket = create(:ticket, group: Group.lookup(id: 1), created_by_id: agent.id)
Observer::Transaction.commit
ticket
end
let(:triggered_article) do
ticket.articles.last
end
it 'renders HTML chars' do
expect(triggered_article.body).to match(/space between/)
end
it 'interpolates ticket properties' do
expect(triggered_article.body).to match(ticket.title)
end
it 'interpolates time in selected time zone' do
time_in_zone = triggered_article.ticket.created_at.in_time_zone(time_zone)
expect(triggered_article.body).to match(time_in_zone.strftime('%H:%M'))
end
it 'interpolates date in selected locale format' do
time_in_zone = triggered_article.ticket.created_at.in_time_zone(time_zone)
expect(triggered_article.body).to match(time_in_zone.strftime('%d.%m.%y'))
end
end end
end end
end end

View file

@ -647,10 +647,20 @@ RSpec.describe User, type: :model do
context 'with no #preferences[:locale]' do context 'with no #preferences[:locale]' do
let(:preferences) { {} } let(:preferences) { {} }
before { Setting.set('locale_default', 'foo') } context 'with default locale' do
before { Setting.set('locale_default', 'foo') }
it 'returns the system-wide default locale' do it 'returns the system-wide default locale' do
expect(user.locale).to eq('foo') expect(user.locale).to eq('foo')
end
end
context 'without default locale' do
before { Setting.set('locale_default', nil) }
it 'returns en-us' do
expect(user.locale).to eq('en-us')
end
end end
end end