From 237df49932d1c64ce059126cb2d5be9091539635 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 4 Jan 2013 14:14:20 +0100 Subject: [PATCH] Added unit tests for backend transtions. --- app/models/translation.rb | 13 +++++++++++++ test/unit/translation_test.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 test/unit/translation_test.rb diff --git a/app/models/translation.rb b/app/models/translation.rb index 432a62f1f..f61d64bce 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -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 diff --git a/test/unit/translation_test.rb b/test/unit/translation_test.rb new file mode 100644 index 000000000..a76fd4196 --- /dev/null +++ b/test/unit/translation_test.rb @@ -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 \ No newline at end of file