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
signature['body'] = NotificationFactory.build(
:locale => current_user.locale,
:string => signature['body'],
:objects => {
:ticket => ticket,

View file

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

View file

@ -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}
<snip>
@ -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}
<snip>
@ -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}
<snip>
@ -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,

View file

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

View file

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

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
end
}
# translate
data[:string].gsub!( /i18n\((.+?)\)/ ) { |s|
string = $1
locale = data[:locale] || 'en'
s = Translation.translate( locale, string )
}
return data[:string]
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
{
: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