forked from svenfuchs/i18n-active_record
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing_test.rb
More file actions
71 lines (58 loc) · 2.96 KB
/
missing_test.rb
File metadata and controls
71 lines (58 loc) · 2.96 KB
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
require File.expand_path('../test_helper', __FILE__)
class I18nActiveRecordMissingTest < Test::Unit::TestCase
class Backend < I18n::Backend::ActiveRecord
include I18n::Backend::ActiveRecord::Missing
end
def setup
I18n.backend.store_translations(:en, :bar => 'Bar', :i18n => { :plural => { :keys => [:zero, :one, :other] } })
I18n.backend = I18n::Backend::Chain.new(Backend.new, I18n.backend)
I18n::Backend::ActiveRecord::Translation.delete_all
end
test "can persist interpolations" do
translation = I18n::Backend::ActiveRecord::Translation.new(:key => 'foo', :value => 'bar', :locale => :en)
translation.interpolations = %w(count name)
translation.save
assert translation.valid?
end
test "lookup persists the key" do
I18n.t('foo.bar.baz')
assert_equal 1, I18n::Backend::ActiveRecord::Translation.count
assert I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo.bar.baz')
end
test "lookup does not persist the key twice" do
2.times { I18n.t('foo.bar.baz') }
assert_equal 1, I18n::Backend::ActiveRecord::Translation.count
assert I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo.bar.baz')
end
test "lookup persists interpolation keys when looked up directly" do
I18n.t('foo.bar.baz', :cow => "lucy" ) # creates stub translation.
translation_stub = I18n::Backend::ActiveRecord::Translation.locale(:en).lookup('foo.bar.baz').first
assert translation_stub.interpolates?(:cow)
end
test "creates one stub per pluralization" do
I18n.t('foo', :count => 999)
translations = I18n::Backend::ActiveRecord::Translation.locale(:en).find_all_by_key %w{ foo.zero foo.one foo.other }
assert_equal 3, translations.length
end
test "creates no stub for base key in pluralization" do
I18n.t('foo', :count => 999)
assert_equal 3, I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo").count
assert !I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key("foo")
end
test "creates a stub when a custom separator is used" do
I18n.t('foo|baz', :separator => '|')
I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz").first.update_attributes!(:value => 'baz!')
assert_equal 'baz!', I18n.t('foo|baz', :separator => '|')
end
test "creates a stub per pluralization when a custom separator is used" do
I18n.t('foo|bar', :count => 999, :separator => '|')
translations = I18n::Backend::ActiveRecord::Translation.locale(:en).find_all_by_key %w{ foo.bar.zero foo.bar.one foo.bar.other }
assert_equal 3, translations.length
end
test "creates a stub when a custom separator is used and the key contains the flatten separator (a dot character)" do
key = 'foo|baz.zab'
I18n.t(key, :separator => '|')
I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz\001zab").first.update_attributes!(:value => 'baz!')
assert_equal 'baz!', I18n.t(key, :separator => '|')
end
end if defined?(ActiveRecord)