r18n-core 3.1.1 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog.md +398 -0
  3. data/README.md +63 -47
  4. data/base/cy.yml +34 -0
  5. data/lib/r18n-core.rb +37 -38
  6. data/lib/r18n-core/filter_list.rb +21 -13
  7. data/lib/r18n-core/filters.rb +43 -48
  8. data/lib/r18n-core/helpers.rb +2 -2
  9. data/lib/r18n-core/i18n.rb +70 -78
  10. data/lib/r18n-core/locale.rb +137 -107
  11. data/lib/r18n-core/locales/af.rb +36 -0
  12. data/lib/r18n-core/locales/az.rb +38 -0
  13. data/lib/r18n-core/locales/bg.rb +29 -0
  14. data/{locales → lib/r18n-core/locales}/ca.rb +9 -6
  15. data/{locales → lib/r18n-core/locales}/cs.rb +15 -11
  16. data/lib/r18n-core/locales/cy.rb +50 -0
  17. data/{locales → lib/r18n-core/locales}/da.rb +10 -7
  18. data/lib/r18n-core/locales/de.rb +33 -0
  19. data/{locales → lib/r18n-core/locales}/en-au.rb +2 -2
  20. data/{locales → lib/r18n-core/locales}/en-gb.rb +2 -2
  21. data/{locales → lib/r18n-core/locales}/en-us.rb +4 -3
  22. data/lib/r18n-core/locales/en.rb +51 -0
  23. data/{locales → lib/r18n-core/locales}/eo.rb +6 -4
  24. data/lib/r18n-core/locales/es-cl.rb +16 -0
  25. data/{locales → lib/r18n-core/locales}/es-us.rb +5 -4
  26. data/{locales → lib/r18n-core/locales}/es.rb +7 -3
  27. data/{locales → lib/r18n-core/locales}/fa.rb +2 -2
  28. data/lib/r18n-core/locales/fi.rb +39 -0
  29. data/lib/r18n-core/locales/fr.rb +39 -0
  30. data/{locales → lib/r18n-core/locales}/gl.rb +5 -3
  31. data/{locales → lib/r18n-core/locales}/hr.rb +13 -10
  32. data/{locales → lib/r18n-core/locales}/hu.rb +10 -7
  33. data/{locales → lib/r18n-core/locales}/id.rb +8 -4
  34. data/lib/r18n-core/locales/it.rb +37 -0
  35. data/{locales → lib/r18n-core/locales}/ja.rb +5 -4
  36. data/{locales → lib/r18n-core/locales}/kk.rb +12 -8
  37. data/{locales → lib/r18n-core/locales}/ko.rb +0 -0
  38. data/lib/r18n-core/locales/lv.rb +46 -0
  39. data/lib/r18n-core/locales/mn.rb +30 -0
  40. data/{locales → lib/r18n-core/locales}/nb.rb +6 -4
  41. data/lib/r18n-core/locales/nl.rb +31 -0
  42. data/{locales → lib/r18n-core/locales}/no.rb +0 -0
  43. data/lib/r18n-core/locales/pl.rb +45 -0
  44. data/{locales → lib/r18n-core/locales}/pt-br.rb +1 -1
  45. data/lib/r18n-core/locales/pt.rb +37 -0
  46. data/lib/r18n-core/locales/ru.rb +46 -0
  47. data/{locales → lib/r18n-core/locales}/sk.rb +13 -9
  48. data/{locales → lib/r18n-core/locales}/sr-latn.rb +10 -8
  49. data/{locales → lib/r18n-core/locales}/sv-se.rb +7 -5
  50. data/lib/r18n-core/locales/th.rb +41 -0
  51. data/{locales → lib/r18n-core/locales}/tr.rb +7 -6
  52. data/lib/r18n-core/locales/uk.rb +31 -0
  53. data/lib/r18n-core/locales/vi.rb +37 -0
  54. data/{locales → lib/r18n-core/locales}/zh-cn.rb +2 -2
  55. data/{locales → lib/r18n-core/locales}/zh-tw.rb +4 -4
  56. data/{locales → lib/r18n-core/locales}/zh.rb +7 -4
  57. data/lib/r18n-core/translated.rb +38 -37
  58. data/lib/r18n-core/translated_string.rb +19 -8
  59. data/lib/r18n-core/translation.rb +24 -19
  60. data/lib/r18n-core/unsupported_locale.rb +14 -6
  61. data/lib/r18n-core/untranslated.rb +23 -7
  62. data/lib/r18n-core/utils.rb +19 -23
  63. data/lib/r18n-core/version.rb +1 -1
  64. data/lib/r18n-core/yaml_loader.rb +17 -13
  65. data/lib/r18n-core/yaml_methods.rb +3 -1
  66. metadata +245 -118
  67. data/.rspec +0 -1
  68. data/Rakefile +0 -13
  69. data/locales/af.rb +0 -32
  70. data/locales/az.rb +0 -34
  71. data/locales/bg.rb +0 -25
  72. data/locales/de.rb +0 -29
  73. data/locales/en.rb +0 -48
  74. data/locales/fi.rb +0 -35
  75. data/locales/fr.rb +0 -35
  76. data/locales/it.rb +0 -34
  77. data/locales/lv.rb +0 -41
  78. data/locales/mn.rb +0 -27
  79. data/locales/nl.rb +0 -28
  80. data/locales/pl.rb +0 -40
  81. data/locales/pt.rb +0 -29
  82. data/locales/ru.rb +0 -41
  83. data/locales/th.rb +0 -37
  84. data/locales/uk.rb +0 -27
  85. data/locales/vi.rb +0 -33
  86. data/r18n-core.gemspec +0 -29
  87. data/spec/filters_spec.rb +0 -324
  88. data/spec/i18n_spec.rb +0 -260
  89. data/spec/locale_spec.rb +0 -216
  90. data/spec/locales/cs_spec.rb +0 -23
  91. data/spec/locales/en-us_spec.rb +0 -13
  92. data/spec/locales/en_spec.rb +0 -13
  93. data/spec/locales/fa_spec.rb +0 -10
  94. data/spec/locales/fr_spec.rb +0 -9
  95. data/spec/locales/hu_spec.rb +0 -17
  96. data/spec/locales/it_spec.rb +0 -10
  97. data/spec/locales/no_spec.rb +0 -9
  98. data/spec/locales/pl_spec.rb +0 -23
  99. data/spec/locales/ru_spec.rb +0 -23
  100. data/spec/locales/sk_spec.rb +0 -23
  101. data/spec/locales/th_spec.rb +0 -9
  102. data/spec/locales/vi_spec.rb +0 -8
  103. data/spec/r18n_spec.rb +0 -172
  104. data/spec/spec_helper.rb +0 -41
  105. data/spec/translated_spec.rb +0 -163
  106. data/spec/translation_spec.rb +0 -164
  107. data/spec/translations/extension/deep/en.yml +0 -1
  108. data/spec/translations/extension/en.yml +0 -2
  109. data/spec/translations/extension/notransl.yml +0 -1
  110. data/spec/translations/general/en.yml +0 -47
  111. data/spec/translations/general/nolocale.yml +0 -6
  112. data/spec/translations/general/ru.yml +0 -7
  113. data/spec/translations/two/en.yml +0 -2
  114. data/spec/translations/two/fr.yml +0 -0
  115. data/spec/yaml_loader_spec.rb +0 -61
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Cs do
4
- it 'uses Czech pluralization' do
5
- cs = R18n.locale('cs')
6
- expect(cs.pluralize(0)).to eq(0)
7
- expect(cs.pluralize(1)).to eq(1)
8
-
9
- expect(cs.pluralize(2)).to eq(2)
10
- expect(cs.pluralize(3)).to eq(2)
11
- expect(cs.pluralize(4)).to eq(2)
12
-
13
- expect(cs.pluralize(5)).to eq('n')
14
- expect(cs.pluralize(21)).to eq('n')
15
- expect(cs.pluralize(11)).to eq('n')
16
- expect(cs.pluralize(12)).to eq('n')
17
- expect(cs.pluralize(22)).to eq('n')
18
- expect(cs.pluralize(57)).to eq('n')
19
- expect(cs.pluralize(101)).to eq('n')
20
- expect(cs.pluralize(102)).to eq('n')
21
- expect(cs.pluralize(111)).to eq('n')
22
- end
23
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::EnUs do
4
- it 'formats American English date' do
5
- en_us = R18n::I18n.new('en-US')
6
- expect(en_us.l(Date.parse('2009-05-01'), :full)).to eq('May 1st, 2009')
7
- expect(en_us.l(Date.parse('2009-05-02'), :full)).to eq('May 2nd, 2009')
8
- expect(en_us.l(Date.parse('2009-05-03'), :full)).to eq('May 3rd, 2009')
9
- expect(en_us.l(Date.parse('2009-05-04'), :full)).to eq('May 4th, 2009')
10
- expect(en_us.l(Date.parse('2009-05-11'), :full)).to eq('May 11th, 2009')
11
- expect(en_us.l(Date.parse('2009-05-21'), :full)).to eq('May 21st, 2009')
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::En do
4
- it 'formats English date' do
5
- en = R18n::I18n.new('en')
6
- expect(en.l(Date.parse('2009-05-01'), :full)).to eq('1st of May, 2009')
7
- expect(en.l(Date.parse('2009-05-02'), :full)).to eq('2nd of May, 2009')
8
- expect(en.l(Date.parse('2009-05-03'), :full)).to eq('3rd of May, 2009')
9
- expect(en.l(Date.parse('2009-05-04'), :full)).to eq('4th of May, 2009')
10
- expect(en.l(Date.parse('2009-05-11'), :full)).to eq('11th of May, 2009')
11
- expect(en.l(Date.parse('2009-05-21'), :full)).to eq('21st of May, 2009')
12
- end
13
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Fa do
4
- it 'formats Persian numerals' do
5
- fa = R18n::I18n.new('fa')
6
- expect(fa.l(1_234_567_890)).to eq('۱٬۲۳۴٬۵۶۷٬۸۹۰')
7
- expect(fa.l(10.123)).to eq('۱۰٫۱۲۳')
8
- expect(fa.l(Date.parse('2009-05-01'))).to eq('۲۰۰۹/۰۵/۰۱')
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Fr do
4
- it 'formats French date' do
5
- fr = R18n::I18n.new('fr')
6
- expect(fr.l(Date.parse('2009-07-01'), :full)).to eq('1er juillet 2009')
7
- expect(fr.l(Date.parse('2009-07-02'), :full)).to eq(' 2 juillet 2009')
8
- end
9
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Hu do
4
- it 'uses Hungarian digits groups' do
5
- hu = R18n::I18n.new('hu')
6
- expect(hu.l(1000)).to eq('1000')
7
- expect(hu.l(10_000)).to eq('10 000')
8
- expect(hu.l(-10_000)).to eq('−10 000')
9
- expect(hu.l(100_000)).to eq('100 000')
10
- end
11
-
12
- it 'uses Hungarian time format' do
13
- hu = R18n::I18n.new('hu')
14
- expect(hu.l(Time.at(0).utc)).to eq('1970. 01. 01., 00:00')
15
- expect(hu.l(Time.at(0).utc, :full)).to eq('1970. január 1., 00:00')
16
- end
17
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::It do
4
- it 'formats Italian date' do
5
- italian = R18n::I18n.new('it')
6
- expect(italian.l(Date.parse('2009-07-01'), :full)).to eq('1º luglio 2009')
7
- expect(italian.l(Date.parse('2009-07-02'), :full)).to eq(' 2 luglio 2009')
8
- expect(italian.l(Date.parse('2009-07-12'), :full)).to eq('12 luglio 2009')
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::No do
4
- it 'deprecated' do
5
- expect { R18n::I18n.new('no') }.to output(
6
- /R18n::Locales::No.new is deprecated; use R18n::Locales::Nb.new instead./
7
- ).to_stderr
8
- end
9
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Pl do
4
- it 'uses Polish pluralization' do
5
- pl = R18n.locale('pl')
6
- expect(pl.pluralize(0)).to eq(0)
7
- expect(pl.pluralize(1)).to eq(1)
8
-
9
- expect(pl.pluralize(2)).to eq(2)
10
- expect(pl.pluralize(4)).to eq(2)
11
- expect(pl.pluralize(22)).to eq(2)
12
- expect(pl.pluralize(102)).to eq(2)
13
-
14
- expect(pl.pluralize(5)).to eq('n')
15
- expect(pl.pluralize(11)).to eq('n')
16
- expect(pl.pluralize(12)).to eq('n')
17
- expect(pl.pluralize(21)).to eq('n')
18
- expect(pl.pluralize(57)).to eq('n')
19
- expect(pl.pluralize(101)).to eq('n')
20
- expect(pl.pluralize(111)).to eq('n')
21
- expect(pl.pluralize(112)).to eq('n')
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Ru do
4
- it 'uses Russian pluralization' do
5
- ru = R18n.locale('ru')
6
- expect(ru.pluralize(0)).to eq(0)
7
-
8
- expect(ru.pluralize(1)).to eq(1)
9
- expect(ru.pluralize(21)).to eq(1)
10
- expect(ru.pluralize(101)).to eq(1)
11
-
12
- expect(ru.pluralize(2)).to eq(2)
13
- expect(ru.pluralize(4)).to eq(2)
14
- expect(ru.pluralize(22)).to eq(2)
15
- expect(ru.pluralize(102)).to eq(2)
16
-
17
- expect(ru.pluralize(5)).to eq('n')
18
- expect(ru.pluralize(11)).to eq('n')
19
- expect(ru.pluralize(12)).to eq('n')
20
- expect(ru.pluralize(57)).to eq('n')
21
- expect(ru.pluralize(111)).to eq('n')
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Sk do
4
- it 'uses Slovak pluralization' do
5
- sk = R18n.locale('Sk')
6
- expect(sk.pluralize(0)).to eq(0)
7
- expect(sk.pluralize(1)).to eq(1)
8
-
9
- expect(sk.pluralize(2)).to eq(2)
10
- expect(sk.pluralize(3)).to eq(2)
11
- expect(sk.pluralize(4)).to eq(2)
12
-
13
- expect(sk.pluralize(5)).to eq('n')
14
- expect(sk.pluralize(21)).to eq('n')
15
- expect(sk.pluralize(11)).to eq('n')
16
- expect(sk.pluralize(12)).to eq('n')
17
- expect(sk.pluralize(22)).to eq('n')
18
- expect(sk.pluralize(57)).to eq('n')
19
- expect(sk.pluralize(101)).to eq('n')
20
- expect(sk.pluralize(102)).to eq('n')
21
- expect(sk.pluralize(111)).to eq('n')
22
- end
23
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Th do
4
- it 'uses Thai calendar' do
5
- th = R18n::I18n.new('th')
6
- expect(th.l(Time.at(0).utc, '%Y %y')).to eq('2513 13')
7
- expect(th.l(Time.at(0).utc)).to eq('01/01/2513 00:00')
8
- end
9
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Vi do
4
- it 'change times position' do
5
- th = R18n::I18n.new('vi')
6
- expect(th.l(Time.at(0).utc)).to eq('00:00, 01/01/1970')
7
- end
8
- end
data/spec/r18n_spec.rb DELETED
@@ -1,172 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n do
4
- include R18n::Helpers
5
-
6
- after do
7
- R18n.default_loader = R18n::Loader::YAML
8
- R18n.default_places = nil
9
- R18n.reset!
10
- end
11
-
12
- it 'stores I18n' do
13
- i18n = R18n::I18n.new('en')
14
- R18n.set(i18n)
15
- expect(R18n.get).to eq(i18n)
16
-
17
- R18n.reset!
18
- expect(R18n.get).to be_nil
19
- end
20
-
21
- it 'sets setter to I18n' do
22
- i18n = R18n::I18n.new('en')
23
- R18n.set(i18n)
24
-
25
- i18n = R18n::I18n.new('ru')
26
- R18n.set { i18n }
27
-
28
- expect(R18n.get).to eq(i18n)
29
- end
30
-
31
- it 'creates I18n object by shortcut' do
32
- R18n.set('en', DIR)
33
- expect(R18n.get).to be_kind_of(R18n::I18n)
34
- expect(R18n.get.locales).to eq([R18n.locale('en')])
35
- expect(R18n.get.translation_places).to eq([R18n::Loader::YAML.new(DIR)])
36
- end
37
-
38
- it 'allows to return I18n arguments in setter block' do
39
- R18n.set { 'en' }
40
- expect(R18n.get.locales).to eq([R18n.locale('en')])
41
- end
42
-
43
- it 'clears cache' do
44
- R18n.cache[:a] = 1
45
- R18n.clear_cache!
46
- expect(R18n.cache).to be_empty
47
- end
48
-
49
- it 'resets I18n objects and cache' do
50
- R18n.cache[:a] = 1
51
- R18n.set('en')
52
- R18n.thread_set('en')
53
-
54
- R18n.reset!
55
- expect(R18n.get).to be_nil
56
- expect(R18n.cache).to be_empty
57
- end
58
-
59
- it 'stores I18n via thread_set' do
60
- i18n = R18n::I18n.new('en')
61
- R18n.thread_set(i18n)
62
- expect(R18n.get).to eq(i18n)
63
-
64
- i18n = R18n::I18n.new('ru')
65
- R18n.thread_set { i18n }
66
- expect(R18n.get).to eq(i18n)
67
- end
68
-
69
- it 'allows to temporary change locale' do
70
- R18n.default_places = DIR
71
- expect(R18n.change('en').locales).to eq([R18n.locale('en')])
72
- expect(R18n.change('en').translation_places.size).to eq(1)
73
- expect(R18n.change('en').translation_places.first.dir).to eq(DIR.to_s)
74
- end
75
-
76
- it 'allows to temporary change current locales' do
77
- R18n.set('ru')
78
- expect(R18n.change('en').locales).to eq(
79
- [R18n.locale('en'), R18n.locale('ru')]
80
- )
81
- expect(R18n.change('en').translation_places).to eq(
82
- R18n.get.translation_places
83
- )
84
- expect(R18n.get.locale.code).to eq('ru')
85
- end
86
-
87
- it 'allows to get Locale to temporary change' do
88
- R18n.set('ru')
89
- expect(R18n.change(R18n.locale('en')).locale.code).to eq('en')
90
- end
91
-
92
- it 'has shortcut to load locale' do
93
- expect(R18n.locale('ru')).to eq(R18n::Locale.load('ru'))
94
- end
95
-
96
- it 'stores default loader class' do
97
- expect(R18n.default_loader).to eq(R18n::Loader::YAML)
98
- R18n.default_loader = Class
99
- expect(R18n.default_loader).to be_kind_of(Class)
100
- end
101
-
102
- it 'stores cache' do
103
- expect(R18n.cache).to be_kind_of(Hash)
104
-
105
- R18n.cache = { 1 => 2 }
106
- expect(R18n.cache).to eq(1 => 2)
107
-
108
- R18n.clear_cache!
109
- expect(R18n.cache).to eq({})
110
- end
111
-
112
- it 'maps hash' do
113
- hash = R18n::Utils.hash_map('a' => 1, 'b' => 2) do |k, v|
114
- [k + 'a', v + 1]
115
- end
116
- expect(hash).to eq('aa' => 2, 'ba' => 3)
117
- end
118
-
119
- it 'merges hash recursively' do
120
- a = { a: 1, b: { ba: 1, bb: 1 }, c: 1 }
121
- b = { b: { bb: 2, bc: 2 }, c: 2 }
122
-
123
- R18n::Utils.deep_merge!(a, b)
124
- expect(a).to eq(a: 1, b: { ba: 1, bb: 2, bc: 2 }, c: 2)
125
- end
126
-
127
- it 'has l and t methods' do
128
- R18n.set('en')
129
- expect(t.yes).to eq('Yes')
130
- expect(l(Time.at(0).utc)).to eq('1970-01-01 00:00')
131
- end
132
-
133
- it 'has helpers mixin' do
134
- obj = R18n::I18n.new('en')
135
- R18n.set(obj)
136
-
137
- expect(r18n).to eq(obj)
138
- expect(i18n).to eq(obj)
139
- expect(t.yes).to eq('Yes')
140
- expect(l(Time.at(0).utc)).to eq('1970-01-01 00:00')
141
- end
142
-
143
- it 'returns available translations' do
144
- expect(R18n.available_locales(DIR)).to match_array([
145
- R18n.locale('nolocale'),
146
- R18n.locale('ru'),
147
- R18n.locale('en')
148
- ])
149
- end
150
-
151
- it 'uses default places' do
152
- R18n.default_places = DIR
153
- R18n.set('en')
154
- expect(t.one).to eq('One')
155
- expect(R18n.available_locales).to match_array([
156
- R18n.locale('ru'),
157
- R18n.locale('en'),
158
- R18n.locale('nolocale')
159
- ])
160
- end
161
-
162
- it 'sets default places by block' do
163
- R18n.default_places { DIR }
164
- expect(R18n.default_places).to eq(DIR)
165
- end
166
-
167
- it 'allows to ignore default places' do
168
- R18n.default_places = DIR
169
- i18n = R18n::I18n.new('en', nil)
170
- expect(i18n.one).not_to be_translated
171
- end
172
- end
data/spec/spec_helper.rb DELETED
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pp'
4
-
5
- require_relative '../lib/r18n-core'
6
- Dir.glob(File.join(__dir__, '..', 'locales', '*.rb')) do |locale_file|
7
- require locale_file
8
- end
9
-
10
- TRANSLATIONS = File.join(__dir__, 'translations') unless defined? TRANSLATIONS
11
- DIR = File.join(TRANSLATIONS, 'general') unless defined? DIR
12
- TWO = File.join(TRANSLATIONS, 'two') unless defined? TWO
13
- unless defined? EXT
14
- EXT = R18n::Loader::YAML.new(File.join(TRANSLATIONS, 'extension'))
15
- end
16
-
17
- RSpec.configure do |config|
18
- config.before { R18n.clear_cache! }
19
- end
20
-
21
- gem 'kramdown'
22
- gem 'RedCloth'
23
-
24
- class CounterLoader
25
- attr_reader :available
26
- attr_reader :loaded
27
-
28
- def initialize(*available)
29
- @available = available.map { |i| R18n.locale(i) }
30
- @loaded = 0
31
- end
32
-
33
- def load(_locale)
34
- @loaded += 1
35
- {}
36
- end
37
-
38
- def hash
39
- @available.hash
40
- end
41
- end
@@ -1,163 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Translated do
4
- before do
5
- @user_class = Class.new do
6
- include R18n::Translated
7
- attr_accessor :name_ru, :name_en
8
-
9
- def name_ru?; end
10
-
11
- def name_ru!; end
12
- end
13
- R18n.set('en')
14
- end
15
-
16
- it 'saves methods map' do
17
- @user_class.translation :name, methods: { ru: :name_ru }
18
- expect(@user_class.unlocalized_getters(:name)).to eq('ru' => 'name_ru')
19
- expect(@user_class.unlocalized_setters(:name)).to eq('ru' => 'name_ru=')
20
- end
21
-
22
- it 'autodetects methods map' do
23
- @user_class.translation :name
24
- expect(@user_class.unlocalized_getters(:name)).to eq(
25
- 'en' => 'name_en', 'ru' => 'name_ru'
26
- )
27
- expect(@user_class.unlocalized_setters(:name)).to eq(
28
- 'en' => 'name_en=', 'ru' => 'name_ru='
29
- )
30
- end
31
-
32
- it 'translates methods' do
33
- @user_class.translation :name
34
- user = @user_class.new
35
-
36
- user.name = 'John'
37
- expect(user.name).to eq('John')
38
-
39
- R18n.set('ru')
40
- expect(user.name).to eq('John')
41
- user.name = 'Джон'
42
- expect(user.name).to eq('Джон')
43
- end
44
-
45
- it 'returns TranslatedString' do
46
- class SomeTranslatedClass
47
- include R18n::Translated
48
-
49
- def name_en
50
- 'John'
51
- end
52
-
53
- translation :name
54
- end
55
- obj = ::SomeTranslatedClass.new
56
-
57
- expect(obj.name).to be_kind_of(R18n::TranslatedString)
58
- expect(obj.name.locale).to eq(R18n.locale('en'))
59
- expect(obj.name.path).to eq('SomeTranslatedClass#name')
60
- end
61
-
62
- it 'searchs translation by locales priority' do
63
- @user_class.translation :name
64
- user = @user_class.new
65
-
66
- R18n.set(%w[nolocale ru en])
67
- user.name_ru = 'Иван'
68
- expect(user.name.locale).to eq(R18n.locale('ru'))
69
- end
70
-
71
- it 'uses default locale' do
72
- @user_class.translation :name
73
- user = @user_class.new
74
-
75
- R18n.set('nolocale')
76
- user.name_en = 'John'
77
- expect(user.name.locale).to eq(R18n.locale('en'))
78
- end
79
-
80
- it 'uses filters' do
81
- @user_class.class_exec do
82
- def age_en
83
- { 1 => '%1 year', 'n' => '%1 years' }
84
- end
85
- translation :age, type: 'pl', no_params: true
86
- end
87
- user = @user_class.new
88
-
89
- expect(user.age(20)).to eq('20 years')
90
- end
91
-
92
- it 'sends params to method if user want it' do
93
- @user_class.class_exec do
94
- def no_params_en(*params)
95
- params.join(' ')
96
- end
97
-
98
- def params_en(*params)
99
- params.join(' ')
100
- end
101
-
102
- translations [:no_params, { no_params: true }], :params
103
- end
104
- user = @user_class.new
105
-
106
- expect(user.no_params(1, 2)).to eq('')
107
- expect(user.params(1, 2)).to eq('1 2')
108
- end
109
-
110
- it 'translates virtual methods' do
111
- @virtual_class = Class.new do
112
- include R18n::Translated
113
-
114
- translation :no_method, methods: { en: :no_method_en }
115
-
116
- def method_missing(name, *_params)
117
- name.to_s
118
- end
119
- end
120
- virtual = @virtual_class.new
121
-
122
- expect(virtual.no_method).to eq('no_method_en')
123
- end
124
-
125
- it 'returns original type of result' do
126
- @user_class.class_exec do
127
- translation :name
128
- def name_en
129
- :ivan
130
- end
131
- end
132
- user = @user_class.new
133
-
134
- expect(user.name).to eq(:ivan)
135
- end
136
-
137
- it 'returns nil' do
138
- @user_class.class_exec do
139
- translation :name
140
- def name_en
141
- nil
142
- end
143
- end
144
- user = @user_class.new
145
-
146
- expect(user.name).to be_nil
147
- end
148
-
149
- it 'allows to change I18n object' do
150
- @user_class.class_exec do
151
- translation :name
152
- attr_accessor :r18n
153
- end
154
- user = @user_class.new
155
- user.name_ru = 'Иван'
156
- user.name_en = 'John'
157
-
158
- user.r18n = R18n::I18n.new('ru')
159
- expect(user.name).to eq('Иван')
160
- user.r18n = R18n::I18n.new('en')
161
- expect(user.name).to eq('John')
162
- end
163
- end