From 743d538e1f91c5d352e008a5f0f5bbe1647c3b5e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 4 Jan 2013 15:28:55 +0100 Subject: [PATCH] Added unit tests for notification template generation (e. g. for emails). Added translation tags i18n('some text') ot i18n(ticket.ticket_state.name) will be translated to frontend locale of recipient. Fixed #26. --- app/controllers/tickets_controller.rb | 1 + app/controllers/users_controller.rb | 1 + app/models/observer/ticket/notification.rb | 13 +++--- app/models/translation.rb | 12 ++++-- app/models/user.rb | 1 + db/migrate/20130104141228_change_user.rb | 8 ++++ lib/notification_factory.rb | 8 ++++ test/unit/notifiation_factory_test.rb | 50 ++++++++++++++++++++++ test/unit/translation_test.rb | 8 ++-- 9 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20130104141228_change_user.rb create mode 100644 test/unit/notifiation_factory_test.rb diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 6cb8b78fa..b59aaaf27 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -333,6 +333,7 @@ class TicketsController < ApplicationController # replace tags signature['body'] = NotificationFactory.build( + :locale => current_user.locale, :string => signature['body'], :objects => { :ticket => ticket, diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 39643043f..458334d39 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -184,6 +184,7 @@ Enjoy, # prepare subject & body [:subject, :body].each { |key| data[key.to_sym] = NotificationFactory.build( + :locale => user.locale, :string => data[key.to_sym], :objects => { :token => token, diff --git a/app/models/observer/ticket/notification.rb b/app/models/observer/ticket/notification.rb index 5c8b6a18e..b3bc869b8 100644 --- a/app/models/observer/ticket/notification.rb +++ b/app/models/observer/ticket/notification.rb @@ -34,11 +34,11 @@ class Observer::Ticket::Notification < ActiveRecord::Observer :subject => 'New Ticket (#{ticket.title})', :body => 'Hi #{recipient.firstname}, -a new Ticket (#{ticket.title}) via #{article.ticket_article_type.name}. +a new Ticket (#{ticket.title}) via i18n(#{article.ticket_article_type.name}). Group: #{ticket.group.name} Owner: #{ticket.owner.firstname} #{ticket.owner.lastname} -State: #{ticket.ticket_state.name} +State: i18n(#{ticket.ticket_state.name}) From: #{article.from} @@ -100,11 +100,11 @@ Your Zammad Team :subject => 'Follow Up (#{ticket.title})', :body => 'Hi #{recipient.firstname}, -a follow Up (#{ticket.title}) via #{article.ticket_article_type.name}. +a follow Up (#{ticket.title}) via i18n(#{article.ticket_article_type.name}). Group: #{ticket.group.name} Owner: #{ticket.owner.firstname} #{ticket.owner.lastname} -State: #{ticket.ticket_state.name} +State: i18n(#{ticket.ticket_state.name}) From: #{article.from} @@ -129,11 +129,11 @@ From: #{article.from} :subject => 'Updated (#{ticket.title})', :body => 'Hi #{recipient.firstname}, -updated (#{ticket.title}) via #{article.ticket_article_type.name}. +updated (#{ticket.title}) via i18n(#{article.ticket_article_type.name}). Group: #{ticket.group.name} Owner: #{ticket.owner.firstname} #{ticket.owner.lastname} -State: #{ticket.ticket_state.name} +State: i18n(#{ticket.ticket_state.name}) From: #{article.from} @@ -201,6 +201,7 @@ From: #{article.from} notification = {} [:subject, :body].each { |key| notification[key.to_sym] = NotificationFactory.build( + :locale => user.locale, :string => data[key.to_sym], :objects => { :ticket => ticket, diff --git a/app/models/translation.rb b/app/models/translation.rb index f61d64bce..e581c7984 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -4,12 +4,16 @@ class Translation < ApplicationModel def self.translate(locale, string) # translate string - record = Translation.where( :locale => locale, :source => string ).first - return record.target if record + records = Translation.where( :locale => locale, :source => string ) + records.each {|record| + return record.target if record.source == string + } # fallback lookup in en - record = Translation.where( :locale => 'en', :source => string ).first - return record.target if record + records = Translation.where( :locale => 'en', :source => string ) + records.each {|record| + return record.target if record.source == string + } return string end diff --git a/app/models/user.rb b/app/models/user.rb index 9424a8b64..cea305d96 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -126,6 +126,7 @@ Your #{config.product_name} Team # prepare subject & body [:subject, :body].each { |key| data[key.to_sym] = NotificationFactory.build( + :locale => user.locale, :string => data[key.to_sym], :objects => { :token => token, diff --git a/db/migrate/20130104141228_change_user.rb b/db/migrate/20130104141228_change_user.rb new file mode 100644 index 000000000..e469e26fd --- /dev/null +++ b/db/migrate/20130104141228_change_user.rb @@ -0,0 +1,8 @@ +class ChangeUser < ActiveRecord::Migration + def up + add_column :users, :locale, :string, :limit => 10, :null => true + end + + def down + end +end diff --git a/lib/notification_factory.rb b/lib/notification_factory.rb index 79f339e00..1126a5b11 100644 --- a/lib/notification_factory.rb +++ b/lib/notification_factory.rb @@ -59,6 +59,14 @@ module NotificationFactory Rails.logger.error e.inspect end } + + # translate + data[:string].gsub!( /i18n\((.+?)\)/ ) { |s| + string = $1 + locale = data[:locale] || 'en' + s = Translation.translate( locale, string ) + } + return data[:string] end diff --git a/test/unit/notifiation_factory_test.rb b/test/unit/notifiation_factory_test.rb new file mode 100644 index 000000000..101b0332c --- /dev/null +++ b/test/unit/notifiation_factory_test.rb @@ -0,0 +1,50 @@ +# encoding: utf-8 +require 'test_helper' + +class NotificationFactoryTest < ActiveSupport::TestCase + test 'notifications' do + tests = [ + { + :locale => 'en', + :string => 'Hi #{recipient.firstname},', + :result => 'Hi Nicole,', + }, + { + :locale => 'de', + :string => 'Hi #{recipient.firstname},', + :result => 'Hi Nicole,', + }, + { + :locale => 'de', + :string => 'Hi #{recipient.firstname}, Group: #{ticket.group.name}', + :result => 'Hi Nicole, Group: Users', + }, + { + :locale => 'de', + :string => '#{config.http_type} some text', + :result => 'http some text', + }, + { + :locale => 'de', + :string => 'i18n(#{"New"}) some text', + :result => 'Neu some text', + }, + { + :locale => 'de', + :string => '\'i18n(#{ticket.ticket_state.name})\' ticket state', + :result => '\'neu\' ticket state', + }, + ] + tests.each { |test| + result = NotificationFactory.build( + :string => test[:string], + :objects => { + :ticket => Ticket.find(1), + :recipient => User.find(2), + }, + :locale => test[:locale] + ) + assert_equal( result, test[:result], "verify result" ) + } + end +end \ No newline at end of file diff --git a/test/unit/translation_test.rb b/test/unit/translation_test.rb index a76fd4196..8ceb1c6e7 100644 --- a/test/unit/translation_test.rb +++ b/test/unit/translation_test.rb @@ -7,27 +7,27 @@ class TranslationTest < ActiveSupport::TestCase # test 1 { - :lang => 'en', + :locale => 'en', :string => 'New', :result => 'New', }, # test 2 { - :lang => 'de', + :locale => 'de', :string => 'New', :result => 'Neu', }, # test 3 { - :lang => 'de', + :locale => 'de', :string => 'not translated - lalala', :result => 'not translated - lalala', }, ] tests.each { |test| - result = Translation.translate( test[:lang], test[:string] ) + result = Translation.translate( test[:locale], test[:string] ) assert_equal( result, test[:result], "verify result" ) } end