1 require File.join( File.dirname(__FILE__), '..', 'test_helper' )
 
   2 require 'globalize/backend/static'
 
   3 require 'globalize/translation'
 
   5 include ActionView::Helpers::NumberHelper
 
   7 I18n.locale = :'en-US'    # Need to set this, since I18n defaults to 'en'
 
   9 class StaticTest < ActiveSupport::TestCase
 
  11     I18n.backend = Globalize::Backend::Static.new
 
  12     translations = {:"en-US" => {:foo => "foo in en-US", :boz => 'boz', :buz => {:bum => 'bum'}},
 
  13                     :"en"    => {:bar => "bar in en"},
 
  14                     :"de-DE" => {:baz => "baz in de-DE"},
 
  15                     :"de"    => {:boo => "boo in de", :number => { :currency => { :format => { :unit => '€', :format => '%n %u'}}}}}
 
  16     translations.each do |locale, data| 
 
  17       I18n.backend.store_translations locale, data 
 
  19     I18n.fallbacks.map :"de-DE" => :"en-US", :he => :en
 
  22   test "returns an instance of Translation:Static" do
 
  23     translation = I18n.translate :foo
 
  24     assert_instance_of Globalize::Translation::Static, translation
 
  27   test "returns the translation in en-US if present" do
 
  28     assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"en-US") 
 
  31   test "returns the translation in en if en-US is not present" do
 
  32     assert_equal "bar in en", I18n.translate(:bar, :locale => :"en-US") 
 
  35   test "returns the translation in de-DE if present" do
 
  36     assert_equal "baz in de-DE", I18n.translate(:baz, :locale => :"de-DE") 
 
  39   test "returns the translation in de if de-DE is not present" do
 
  40     assert_equal "boo in de", I18n.translate(:boo, :locale => :"de-DE") 
 
  43   test "returns the translation in en-US if none of de-DE and de are present" do
 
  44     assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"de-DE") 
 
  47   test "returns the translation in en if none of de-DE, de and en-US are present" do
 
  48     assert_equal "bar in en", I18n.translate(:bar, :locale => :"de-DE") 
 
  51   test "returns the translation in en if none in he is present" do
 
  52     assert_equal "bar in en", I18n.translate(:bar, :locale => :he) 
 
  55   test "returns the given default String when the key is not present for any locale" do
 
  56     assert_equal "default", I18n.translate(:missing, :default => "default") 
 
  59   test "returns the fallback translation for the key if present for a fallback locale" do
 
  60     I18n.backend.store_translations :de, :non_default => "non_default in de"
 
  61     assert_equal "non_default in de", I18n.translate(:non_default, :default => "default", :locale => :"de-DE") 
 
  64   test "returns an array of translations" do
 
  65     assert_instance_of Array, I18n.translate([:foo, :boz])
 
  68   test "returns an array of instances of Translation::Static" do
 
  69     assert_equal [Globalize::Translation::Static], I18n.translate([:foo, :boz]).map(&:class).uniq
 
  72   test "returns a hash of translations" do
 
  73     assert_instance_of Hash, I18n.translate(:"buz")
 
  76   test "returns an array of translations 2" do
 
  77     assert_equal [Globalize::Translation::Static], I18n.translate(:"buz").values.map(&:class) 
 
  80   test "returns currency properly formated" do
 
  81     currency = number_to_currency(10)
 
  82     assert_equal "$10.00", currency
 
  85   test "returns currency properly formated for locale" do
 
  86     currency = number_to_currency(10, :locale => :'de')
 
  87     assert_equal "10.000 €", currency
 
  90   test "returns currency properly formated from parameters" do
 
  91     currency = number_to_currency(10, :format => "%n %u", :unit => '€')
 
  92     assert_equal "10.00 €", currency
 
  95   test "makes sure interpolation does not break even with False as string" do
 
  96     assert_equal "translation missing: en, support, array, skip_last_comma", I18n.translate(:"support.array.skip_last_comma")
 
 100 class TranslationStaticTest < ActiveSupport::TestCase
 
 102     I18n.backend = Globalize::Backend::Static.new 
 
 104       :greeting => "Hi {{name}}",
 
 105       :messages => { :one => "You have one message.", :other => "You have {{count}} messages."}
 
 107     I18n.backend.store_translations :"en", translations
 
 111     I18n.translate :greeting, :locale => :"en-US", :name => "Joshua"
 
 114   test "stores the actual locale" do
 
 115     assert_equal :en, greeting.locale
 
 118   test "stores the requested locale" do
 
 119     assert_equal :'en-US', greeting.requested_locale
 
 122   test "stores the requested key" do
 
 123     assert_equal :greeting, greeting.key
 
 126   test "stores the options given to #translate" do
 
 127     assert_equal( {:name => "Joshua"}, greeting.options ) 
 
 130   test "stores the original translation before test was interpolated" do
 
 131     assert_equal "Hi {{name}}", greeting.original 
 
 134   test "stores the plural_key :one if pluralized as such" do
 
 135     message = I18n.translate :messages, :locale => :"en-US", :count => 1
 
 136     assert_equal :one, message.plural_key
 
 139   test "stores the plural_key :other if pluralized as such" do
 
 140     messages = I18n.translate :messages, :locale => :"en-US", :count => 2
 
 141     assert_equal :other, messages.plural_key