File: cascade_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 (72 lines) | stat: -rw-r--r-- 2,724 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
# encoding: utf-8
$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../')); $:.uniq!
require 'test_helper'

class I18nBackendCascadeTest < Test::Unit::TestCase
  class Backend < I18n::Backend::Simple
    include I18n::Backend::Cascade
  end

  def setup
    I18n.backend = Backend.new
    store_translations(:en,
      :foo => 'foo',
      :bar => { :baz => 'baz' }
    )
  end

  def lookup(key, options = {})
    I18n.t(key, options.merge(:cascade => { :step => 1, :offset => 1, :skip_root => false }))
  end

  test "still returns an existing translation as usual" do
    assert_equal 'foo', lookup(:foo)
    assert_equal 'baz', lookup(:'bar.baz')
  end

  test "falls back by cutting keys off the end of the scope" do
    assert_equal 'foo', lookup(:foo, :scope => :'missing')
    assert_equal 'foo', lookup(:foo, :scope => :'missing.missing')
    assert_equal 'baz', lookup(:baz, :scope => :'bar.missing')
    assert_equal 'baz', lookup(:baz, :scope => :'bar.missing.missing')
  end

  test "raises I18n::MissingTranslationData exception when no translation was found" do
    assert_raise(I18n::MissingTranslationData) { lookup(:'foo.missing', :raise => true) }
    assert_raise(I18n::MissingTranslationData) { lookup(:'bar.baz.missing', :raise => true) }
    assert_raise(I18n::MissingTranslationData) { lookup(:'missing.bar.baz', :raise => true) }
  end

  test "cascades before evaluating the default" do
    assert_equal 'foo', lookup(:foo, :scope => :missing, :default => 'default')
  end
  
  test "cascades defaults, too" do
    assert_equal 'foo', lookup(nil, :default => [:'missing.missing', :'missing.foo'])
  end

  test "let's us assemble required fallbacks for ActiveRecord validation messages" do
    store_translations(:en,
      :errors => {
        :reply => {
          :title => {
            :blank => 'blank on reply title'
          },
          :taken => 'taken on reply'
        },
        :topic => {
          :title => {
            :format => 'format on topic title'
          },
          :length => 'length on topic'
        },
        :odd => 'odd on errors'
      }
    )
    assert_equal 'blank on reply title',  lookup(:'errors.reply.title.blank',  :default => :'errors.topic.title.blank')
    assert_equal 'taken on reply',        lookup(:'errors.reply.title.taken',  :default => :'errors.topic.title.taken')
    assert_equal 'format on topic title', lookup(:'errors.reply.title.format', :default => :'errors.topic.title.format')
    assert_equal 'length on topic',       lookup(:'errors.reply.title.length', :default => :'errors.topic.title.length')
    assert_equal 'odd on errors',         lookup(:'errors.reply.title.odd',    :default => :'errors.topic.title.odd')
  end
end