83d24bc5384c4ec78dd603a0884b703c094a9bda
[packages/precise/mcollective.git] / lib / mcollective / vendor / i18n / lib / i18n / tests / localization / procs.rb
1 # encoding: utf-8
2
3 module I18n
4   module Tests
5     module Localization
6       module Procs
7         test "localize: using day names from lambdas" do
8           setup_time_proc_translations
9           time = ::Time.utc(2008, 3, 1, 6, 0)
10           assert_match(/Суббота/, I18n.l(time, :format => "%A, %d %B", :locale => :ru))
11           assert_match(/суббота/, I18n.l(time, :format => "%d %B (%A)", :locale => :ru))
12         end
13
14         test "localize: using month names from lambdas" do
15           setup_time_proc_translations
16           time = ::Time.utc(2008, 3, 1, 6, 0)
17           assert_match(/марта/, I18n.l(time, :format => "%d %B %Y", :locale => :ru))
18           assert_match(/Март /, I18n.l(time, :format => "%B %Y", :locale => :ru))
19         end
20
21         test "localize: using abbreviated day names from lambdas" do
22           setup_time_proc_translations
23           time = ::Time.utc(2008, 3, 1, 6, 0)
24           assert_match(/марта/, I18n.l(time, :format => "%d %b %Y", :locale => :ru))
25           assert_match(/март /, I18n.l(time, :format => "%b %Y", :locale => :ru))
26         end
27
28         test "localize Date: given a format that resolves to a Proc it calls the Proc with the object" do
29           setup_time_proc_translations
30           date = ::Date.new(2008, 3, 1, 6)
31           assert_equal '[Sat, 01 Mar 2008, {}]', I18n.l(date, :format => :proc, :locale => :ru)
32         end
33
34         test "localize Date: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
35           setup_time_proc_translations
36           date = ::Date.new(2008, 3, 1, 6)
37           assert_equal '[Sat, 01 Mar 2008, {:foo=>"foo"}]', I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
38         end
39
40         test "localize DateTime: given a format that resolves to a Proc it calls the Proc with the object" do
41           setup_time_proc_translations
42           datetime = ::DateTime.new(2008, 3, 1, 6)
43           assert_equal '[Sat, 01 Mar 2008 06:00:00 +00:00, {}]', I18n.l(datetime, :format => :proc, :locale => :ru)
44         end
45
46         test "localize DateTime: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
47           setup_time_proc_translations
48           datetime = ::DateTime.new(2008, 3, 1, 6)
49           assert_equal '[Sat, 01 Mar 2008 06:00:00 +00:00, {:foo=>"foo"}]', I18n.l(datetime, :format => :proc, :foo => 'foo', :locale => :ru)
50         end
51
52         test "localize Time: given a format that resolves to a Proc it calls the Proc with the object" do
53           setup_time_proc_translations
54           time = ::Time.utc(2008, 3, 1, 6, 0)
55           assert_equal inspect_args([time, {}]), I18n.l(time, :format => :proc, :locale => :ru)
56         end
57
58         test "localize Time: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
59           setup_time_proc_translations
60           time = ::Time.utc(2008, 3, 1, 6, 0)
61           options = { :foo => 'foo' }
62           assert_equal inspect_args([time, options]), I18n.l(time, options.merge(:format => :proc, :locale => :ru))
63         end
64
65         protected
66
67           def inspect_args(args)
68             args = args.map do |arg|
69               case arg
70               when ::Time, ::DateTime
71                 arg.strftime('%a, %d %b %Y %H:%M:%S %Z').sub('+0000', '+00:00')
72               when ::Date
73                 arg.strftime('%a, %d %b %Y')
74               when Hash
75                 arg.delete(:fallback)
76                 arg.inspect
77               else
78                 arg.inspect
79               end
80             end
81             "[#{args.join(', ')}]"
82           end
83
84           def setup_time_proc_translations
85             I18n.backend.store_translations :ru, {
86               :time => {
87                 :formats => {
88                   :proc => lambda { |*args| inspect_args(args) }
89                 }
90               },
91               :date => {
92                 :formats => {
93                   :proc => lambda { |*args| inspect_args(args) }
94                 },
95                 :'day_names' => lambda { |key, options|
96                   (options[:format] =~ /^%A/) ?
97                   %w(Воскресенье Понедельник Вторник Среда Четверг Пятница Суббота) :
98                   %w(воскресенье понедельник вторник среда четверг пятница суббота)
99                 },
100                 :'month_names' => lambda { |key, options|
101                   (options[:format] =~ /(%d|%e)(\s*)?(%B)/) ?
102                   %w(января февраля марта апреля мая июня июля августа сентября октября ноября декабря).unshift(nil) :
103                   %w(Январь Февраль Март Апрель Май Июнь Июль Август Сентябрь Октябрь Ноябрь Декабрь).unshift(nil)
104                 },
105                 :'abbr_month_names' => lambda { |key, options|
106                   (options[:format] =~ /(%d|%e)(\s*)(%b)/) ?
107                   %w(янв. февр. марта апр. мая июня июля авг. сент. окт. нояб. дек.).unshift(nil) :
108                   %w(янв. февр. март апр. май июнь июль авг. сент. окт. нояб. дек.).unshift(nil)
109                 },
110               }
111             }
112           end
113       end
114     end
115   end
116 end