require File.dirname(__FILE__) + '/test_helper.rb' class GettextI18nTest < Test::Unit::TestCase context "Simple translate" do should "return itself if are not translated" do I18n.locale = :en assert_equal "Some not translated text", I18n.gettext("Some not translated text") end should "return translated string if are translated" do I18n.locale = :pl assert_equal "Testowa translacja", I18n.gettext("Test translation") end should "return translated string in the same language if such is defined" do I18n.locale = :en assert_equal "Correct translation", I18n.gettext("Translation with mistake") end should "return translation for specified key" do I18n.locale = :pl assert_equal "Sukces!", I18n._(:key_to_translate) assert_equal "Sukces!", I18n._(:"nested.translation") end should "translate string and symbol using built in helpers" do I18n.locale = :pl assert_equal "Testowa translacja", "Test translation".t assert_equal "Sukces!", :"nested.translation".t end should "fallback translation to :root language" do I18n.locale = :en assert_equal "It's work!", I18n.gettext("Translation fallback") end end context "Translate with options" do should "display specified parameter in result" do I18n.locale = :pl assert_equal "Witaj John!", I18n.gettext("Hello {{who}}!", :who => "John") end should "display specified parameter when translation was not found" do I18n.locale = :en assert_equal "Hello Bill!", I18n.gettext("Hello {{who}}!", :who => "Bill") end end context "Translate inside modules and classes" do should "allow to use gettext methods in extended module" do module SampleFoo extend I18n::Gettext def self.test gettext('Test translation') end def self.test2 gettext("Hello {{who}}!", :who => "John") end end results = [] I18n.locale = :pl assert begin results << SampleFoo.test results << SampleFoo.test2 true rescue false end assert_equal "Testowa translacja", results.first assert_equal "Witaj John!", results.last end should "allow to use gettext methods in extended class and it singleton" do class SampleBar include I18n::Gettext include I18n::Gettext::Singleton def self.test gettext('Test translation') end def test gettext('Test translation') end end results = [] I18n.locale = :pl assert begin results << SampleBar.test results << SampleBar.new.test true rescue false end assert_equal "Testowa translacja", results.first assert_equal "Testowa translacja", results.last end end context "Translation in Rails" do should "allow to use gettext methods in models" do class SampleModel < ActiveRecord::Base def self.columns; []; end def self.foo gettext('Test translation') end end result = nil I18n.locale = :pl assert begin result = SampleModel.foo true rescue false end assert_equal "Testowa translacja", result end # TODO: tests for controllers and views end end