-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmini_i18n_spec.rb
More file actions
124 lines (99 loc) · 3.67 KB
/
mini_i18n_spec.rb
File metadata and controls
124 lines (99 loc) · 3.67 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
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
RSpec.describe MiniI18n do
describe 'load_translations' do
it "allows to load multiple locales and translations from different files" do
expect(MiniI18n.available_locales).to eq ["en", "es", "fr"]
expect(MiniI18n.translations.size). to eq 3
expect(MiniI18n.translations["en"]).to include 'bye'
end
it "allows to load translations from JSON" do
expect(MiniI18n.t(:from_json)).to eq 'from JSON'
end
it "does not raise if path is nil" do
expect(MiniI18n.load_translations(nil)).to eq []
end
end
describe 'default_locale=' do
it 'defaults to default_locale if locale is not valid' do
MiniI18n.default_locale = :foo
expect(MiniI18n.default_locale).to eq :en
end
end
describe 'available_locales=' do
it 'wraps into an array of strings' do
MiniI18n.available_locales = :en
expect(MiniI18n.available_locales).to eq ["en"]
end
end
describe 'locale' do
it 'allows to change locale globally' do
MiniI18n.locale = :en
expect(MiniI18n.t(:hello)).to eq 'hello'
MiniI18n.locale = :es
expect(MiniI18n.t(:hello)).to eq 'hola'
end
end
describe 'separator' do
it 'allows to customize separator for nested keys' do
MiniI18n.separator = ' '
expect(MiniI18n.t('second_level hello')).to eq 'hello 2'
MiniI18n.separator = '/'
expect(MiniI18n.t('second_level/hello')).to eq 'hello 2'
end
end
describe 'translate' do
it "simple key" do
expect(MiniI18n.t(:hello)).to eq 'hello'
end
it "nested key" do
expect(MiniI18n.t('second_level')).to be_a Hash
expect(MiniI18n.t('second_level.hello')).to eq 'hello 2'
end
it "multiple keys" do
expect(MiniI18n.t([:hello, :bye])).to eq ['hello', 'bye']
expect(MiniI18n.t([:hello, :bye], locale: :fr)).to eq ['bonjour', 'au revoir']
end
it "locale" do
expect(MiniI18n.t('hello', locale: :fr)).to eq 'bonjour'
expect(MiniI18n.t('hello', locale: :es)).to eq 'hola'
end
it "multiple locales" do
expect(MiniI18n.t(:hello, locale: [:en, :fr, :es])).to eq ['hello', 'bonjour', 'hola']
expect(MiniI18n.t(:hello_interpolation, name: 'world', locale: [:en])).to eq ['hello world']
end
it "all locales with '*'" do
expect(MiniI18n.t(:hello, locale: '*')).to eq ['hello', 'hola', 'bonjour']
end
it "scope" do
expect(MiniI18n.t('hello', scope: :second_level)).to eq 'hello 2'
end
it "returns nil if key does not exist" do
expect(MiniI18n.t('foo')).to eq nil
end
it "returns default if key does not exist" do
expect(MiniI18n.t('foo', default: 'bar')).to eq 'bar'
end
it "with interpolation" do
expect(MiniI18n.t('hello_interpolation')).to eq 'hello %{name}'
expect(MiniI18n.t('hello_interpolation', name: 'world')).to eq 'hello world'
end
it "fallbacks" do
expect(MiniI18n.t('fallback', locale: :es)).to eq ''
MiniI18n.fallbacks = true
expect(MiniI18n.t('fallback', locale: :es)).to eq 'fallback'
end
it "pluralization" do
expect(MiniI18n.t('notifications', count: 0)).to eq 'no unread notifications'
expect(MiniI18n.t('notifications', count: 1)).to eq '1 unread notification'
expect(MiniI18n.t('notifications', count: 5)).to eq '5 unread notifications'
end
it "pluralization with custom rules" do
MiniI18n.pluralization_rules = {
es: -> (n) {
n == 0 ? 'zero' : 'other'
}
}
expect(MiniI18n.t('notifications', count: 0, locale: :es)).to eq 'no hay nuevas notificaciones'
expect(MiniI18n.t('notifications', count: 1, locale: :es)).to eq 'tienes notificaciones por leer'
end
end
end