Added unit tests for backend transtions.
This commit is contained in:
parent
14c63c0383
commit
237df49932
2 changed files with 47 additions and 0 deletions
|
@ -1,6 +1,19 @@
|
|||
class Translation < ApplicationModel
|
||||
before_create :set_initial
|
||||
|
||||
def self.translate(locale, string)
|
||||
|
||||
# translate string
|
||||
record = Translation.where( :locale => locale, :source => string ).first
|
||||
return record.target if record
|
||||
|
||||
# fallback lookup in en
|
||||
record = Translation.where( :locale => 'en', :source => string ).first
|
||||
return record.target if record
|
||||
|
||||
return string
|
||||
end
|
||||
|
||||
private
|
||||
def set_initial
|
||||
self.target_initial = self.target
|
||||
|
|
34
test/unit/translation_test.rb
Normal file
34
test/unit/translation_test.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
# encoding: utf-8
|
||||
require 'test_helper'
|
||||
|
||||
class TranslationTest < ActiveSupport::TestCase
|
||||
test 'translation' do
|
||||
tests = [
|
||||
|
||||
# test 1
|
||||
{
|
||||
:lang => 'en',
|
||||
:string => 'New',
|
||||
:result => 'New',
|
||||
},
|
||||
|
||||
# test 2
|
||||
{
|
||||
:lang => 'de',
|
||||
:string => 'New',
|
||||
:result => 'Neu',
|
||||
},
|
||||
|
||||
# test 3
|
||||
{
|
||||
:lang => 'de',
|
||||
:string => 'not translated - lalala',
|
||||
:result => 'not translated - lalala',
|
||||
},
|
||||
]
|
||||
tests.each { |test|
|
||||
result = Translation.translate( test[:lang], test[:string] )
|
||||
assert_equal( result, test[:result], "verify result" )
|
||||
}
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue