File: cldr_test.rb

package info (click to toggle)
libi18n-ruby 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 576 kB
  • ctags: 619
  • sloc: ruby: 4,655; makefile: 5
file content (150 lines) | stat: -rw-r--r-- 3,934 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# encoding: utf-8

begin
  require 'cldr'
rescue LoadError
  puts "Skipping tests for I18n::Backend::Cldr because the ruby-cldr gem is not installed."
end

if defined?(Cldr)
  $:.unshift(File.expand_path(File.dirname(__FILE__) + '/../')); $:.uniq!
  require 'test_helper'
  require 'i18n/backend/cldr'
  require 'date'

  class I18nBackendCldrTest < Test::Unit::TestCase
    class Backend < I18n::Backend::Simple
      include I18n::Backend::Cldr
    end

    def setup
      I18n.backend = Backend.new
      I18n.locale = :de
      I18n.load_path += Dir[locales_dir + '/cldr/**/*.{yml,rb}']
      super
    end

    # NUMBER

    test "format_number" do
      assert_equal '123.456,78', I18n.l(123456.78)
    end

    # CURRENCY

    test "format_currency" do
      assert_equal '123.456,78 EUR', I18n.l(123456.78, :currency => 'EUR')
    end

    # hu? does this actually make any sense?
    test "format_currency translating currency names" do
      assert_equal '1,00 Irisches Pfund', I18n.l(1, :currency => :IEP)
      assert_equal '2,00 Irische Pfund',  I18n.l(2, :currency => :IEP)
    end

    # PERCENT

    # this is odd but the cldr percent format does not include a fraction
    test "format_percent" do
      assert_equal '123.457 %', I18n.l(123456.78, :as => :percent)
    end

    # so we can pass a precision manually
    test "format_percent w/ precision" do
      assert_equal '123.456,70 %', I18n.l(123456.7, :as => :percent, :precision => 2)
    end

    # DATE

    def date
      Date.new(2010, 1, 1)
    end

    test "format_date :full" do
      assert_equal 'Freitag, 1. Januar 2010', I18n.l(date, :format => :full)
    end

    test "format_date :long" do
      assert_equal '1. Januar 2010', I18n.l(date, :format => :long)
    end

    test "format_date :medium" do
      assert_equal '01.01.2010', I18n.l(date)
    end

    test "format_date :short" do
      assert_equal '01.01.10', I18n.l(date, :format => :short)
    end

    # TIME

    def time
      Time.utc(2010, 1, 1, 13, 15, 17)
    end

    # TODO cldr export lacks localized timezone data
    # test "format_time :full" do
    #   assert_equal 'Freitag, 1. Januar 2010', I18n.l(time, :format => :full)
    # end

    test "format_time :long" do
      assert_equal '13:15:17 UTC', I18n.l(time, :format => :long)
    end

    test "format_time :medium" do
      assert_equal '13:15:17', I18n.l(time)
    end

    test "format_time :short" do
      assert_equal '13:15', I18n.l(time, :format => :short)
    end

    # DATETIME

    def datetime
      DateTime.new(2010, 11, 12, 13, 14, 15)
    end

    # TODO cldr export lacks localized timezone data
    # test "format_datetime :full" do
    #   assert_equal 'Thursday, 12. November 2010 13:14:15', I18n.l(datetime, :format => :full)
    # end

    test "format_datetime :long" do
      assert_equal '12. November 2010 13:14:15 +00:00', I18n.l(datetime, :format => :long)
    end

    test "format_datetime :medium" do
      assert_equal '12.11.2010 13:14:15', I18n.l(datetime)
    end

    test "format_datetime :short" do
      assert_equal '12.11.10 13:14', I18n.l(datetime, :format => :short)
    end

    test "format_datetime mixed :long + :short" do
      assert_equal '12. November 2010 13:14', I18n.l(datetime, :date_format => :long, :time_format => :short)
    end

    test "format_datetime mixed :short + :long" do
      assert_equal '12.11.10 13:14:15 +00:00', I18n.l(datetime, :date_format => :short, :time_format => :long)
    end

    # CUSTOM FORMATS

    test "can deal with customized formats data" do
      store_translations :de, :numbers => {
        :formats => {
          :decimal => {
            :patterns => {
              :default => "#,##0.###",
              :stupid  => "#"
            }
          }
        }
      }
      assert_equal '123.456,78', I18n.l(123456.78)
      assert_equal '123457',     I18n.l(123456.78, :format => :stupid)
    end
  end
end