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.

This commit is contained in:
Martin Edenhofer 2013-01-04 15:28:55 +01:00
parent 7a41c11e64
commit 743d538e1f
9 changed files with 88 additions and 14 deletions

View file

@ -333,6 +333,7 @@ class TicketsController < ApplicationController
# replace tags # replace tags
signature['body'] = NotificationFactory.build( signature['body'] = NotificationFactory.build(
:locale => current_user.locale,
:string => signature['body'], :string => signature['body'],
:objects => { :objects => {
:ticket => ticket, :ticket => ticket,

View file

@ -184,6 +184,7 @@ Enjoy,
# prepare subject & body # prepare subject & body
[:subject, :body].each { |key| [:subject, :body].each { |key|
data[key.to_sym] = NotificationFactory.build( data[key.to_sym] = NotificationFactory.build(
:locale => user.locale,
:string => data[key.to_sym], :string => data[key.to_sym],
:objects => { :objects => {
:token => token, :token => token,

View file

@ -34,11 +34,11 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
:subject => 'New Ticket (#{ticket.title})', :subject => 'New Ticket (#{ticket.title})',
:body => 'Hi #{recipient.firstname}, :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} Group: #{ticket.group.name}
Owner: #{ticket.owner.firstname} #{ticket.owner.lastname} Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
State: #{ticket.ticket_state.name} State: i18n(#{ticket.ticket_state.name})
From: #{article.from} From: #{article.from}
<snip> <snip>
@ -100,11 +100,11 @@ Your Zammad Team
:subject => 'Follow Up (#{ticket.title})', :subject => 'Follow Up (#{ticket.title})',
:body => 'Hi #{recipient.firstname}, :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} Group: #{ticket.group.name}
Owner: #{ticket.owner.firstname} #{ticket.owner.lastname} Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
State: #{ticket.ticket_state.name} State: i18n(#{ticket.ticket_state.name})
From: #{article.from} From: #{article.from}
<snip> <snip>
@ -129,11 +129,11 @@ From: #{article.from}
:subject => 'Updated (#{ticket.title})', :subject => 'Updated (#{ticket.title})',
:body => 'Hi #{recipient.firstname}, :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} Group: #{ticket.group.name}
Owner: #{ticket.owner.firstname} #{ticket.owner.lastname} Owner: #{ticket.owner.firstname} #{ticket.owner.lastname}
State: #{ticket.ticket_state.name} State: i18n(#{ticket.ticket_state.name})
From: #{article.from} From: #{article.from}
<snip> <snip>
@ -201,6 +201,7 @@ From: #{article.from}
notification = {} notification = {}
[:subject, :body].each { |key| [:subject, :body].each { |key|
notification[key.to_sym] = NotificationFactory.build( notification[key.to_sym] = NotificationFactory.build(
:locale => user.locale,
:string => data[key.to_sym], :string => data[key.to_sym],
:objects => { :objects => {
:ticket => ticket, :ticket => ticket,

View file

@ -4,12 +4,16 @@ class Translation < ApplicationModel
def self.translate(locale, string) def self.translate(locale, string)
# translate string # translate string
record = Translation.where( :locale => locale, :source => string ).first records = Translation.where( :locale => locale, :source => string )
return record.target if record records.each {|record|
return record.target if record.source == string
}
# fallback lookup in en # fallback lookup in en
record = Translation.where( :locale => 'en', :source => string ).first records = Translation.where( :locale => 'en', :source => string )
return record.target if record records.each {|record|
return record.target if record.source == string
}
return string return string
end end

View file

@ -126,6 +126,7 @@ Your #{config.product_name} Team
# prepare subject & body # prepare subject & body
[:subject, :body].each { |key| [:subject, :body].each { |key|
data[key.to_sym] = NotificationFactory.build( data[key.to_sym] = NotificationFactory.build(
:locale => user.locale,
:string => data[key.to_sym], :string => data[key.to_sym],
:objects => { :objects => {
:token => token, :token => token,

View file

@ -0,0 +1,8 @@
class ChangeUser < ActiveRecord::Migration
def up
add_column :users, :locale, :string, :limit => 10, :null => true
end
def down
end
end

View file

@ -59,6 +59,14 @@ module NotificationFactory
Rails.logger.error e.inspect Rails.logger.error e.inspect
end end
} }
# translate
data[:string].gsub!( /i18n\((.+?)\)/ ) { |s|
string = $1
locale = data[:locale] || 'en'
s = Translation.translate( locale, string )
}
return data[:string] return data[:string]
end end

View file

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

View file

@ -7,27 +7,27 @@ class TranslationTest < ActiveSupport::TestCase
# test 1 # test 1
{ {
:lang => 'en', :locale => 'en',
:string => 'New', :string => 'New',
:result => 'New', :result => 'New',
}, },
# test 2 # test 2
{ {
:lang => 'de', :locale => 'de',
:string => 'New', :string => 'New',
:result => 'Neu', :result => 'Neu',
}, },
# test 3 # test 3
{ {
:lang => 'de', :locale => 'de',
:string => 'not translated - lalala', :string => 'not translated - lalala',
:result => 'not translated - lalala', :result => 'not translated - lalala',
}, },
] ]
tests.each { |test| tests.each { |test|
result = Translation.translate( test[:lang], test[:string] ) result = Translation.translate( test[:locale], test[:string] )
assert_equal( result, test[:result], "verify result" ) assert_equal( result, test[:result], "verify result" )
} }
end end