r18n-core 4.0.0 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog.md +398 -0
  3. data/README.md +23 -13
  4. data/lib/r18n-core.rb +10 -25
  5. data/lib/r18n-core/filter_list.rb +6 -3
  6. data/lib/r18n-core/filters.rb +21 -22
  7. data/lib/r18n-core/i18n.rb +9 -16
  8. data/lib/r18n-core/locale.rb +20 -21
  9. data/lib/r18n-core/locales/cy.rb +1 -1
  10. data/lib/r18n-core/locales/en-au.rb +1 -1
  11. data/lib/r18n-core/locales/en-gb.rb +1 -1
  12. data/lib/r18n-core/locales/en-us.rb +1 -1
  13. data/lib/r18n-core/locales/en.rb +2 -2
  14. data/lib/r18n-core/locales/es-cl.rb +1 -1
  15. data/lib/r18n-core/locales/es-us.rb +1 -1
  16. data/lib/r18n-core/locales/es.rb +2 -0
  17. data/lib/r18n-core/locales/fi.rb +2 -2
  18. data/lib/r18n-core/locales/fr.rb +3 -3
  19. data/lib/r18n-core/locales/it.rb +3 -3
  20. data/lib/r18n-core/locales/pt.rb +2 -0
  21. data/lib/r18n-core/locales/zh-cn.rb +1 -1
  22. data/lib/r18n-core/locales/zh-tw.rb +1 -1
  23. data/lib/r18n-core/locales/zh.rb +2 -0
  24. data/lib/r18n-core/translated.rb +4 -4
  25. data/lib/r18n-core/translated_string.rb +1 -6
  26. data/lib/r18n-core/translation.rb +1 -1
  27. data/lib/r18n-core/unsupported_locale.rb +2 -0
  28. data/lib/r18n-core/utils.rb +1 -1
  29. data/lib/r18n-core/version.rb +1 -1
  30. metadata +198 -89
  31. data/.rspec +0 -1
  32. data/Rakefile +0 -13
  33. data/r18n-core.gemspec +0 -29
  34. data/spec/filters_spec.rb +0 -327
  35. data/spec/i18n_spec.rb +0 -274
  36. data/spec/locale_spec.rb +0 -251
  37. data/spec/locales/af_spec.rb +0 -9
  38. data/spec/locales/cs_spec.rb +0 -23
  39. data/spec/locales/en-us_spec.rb +0 -28
  40. data/spec/locales/en_spec.rb +0 -13
  41. data/spec/locales/es-us_spec.rb +0 -11
  42. data/spec/locales/fa_spec.rb +0 -10
  43. data/spec/locales/fi_spec.rb +0 -9
  44. data/spec/locales/fr_spec.rb +0 -9
  45. data/spec/locales/hu_spec.rb +0 -19
  46. data/spec/locales/id_spec.rb +0 -23
  47. data/spec/locales/it_spec.rb +0 -10
  48. data/spec/locales/no_spec.rb +0 -9
  49. data/spec/locales/pl_spec.rb +0 -23
  50. data/spec/locales/ru_spec.rb +0 -23
  51. data/spec/locales/sk_spec.rb +0 -23
  52. data/spec/locales/th_spec.rb +0 -9
  53. data/spec/locales/vi_spec.rb +0 -9
  54. data/spec/r18n_spec.rb +0 -192
  55. data/spec/spec_helper.rb +0 -38
  56. data/spec/translated_spec.rb +0 -225
  57. data/spec/translation_spec.rb +0 -189
  58. data/spec/translations/extension/deep/en.yml +0 -1
  59. data/spec/translations/extension/en.yml +0 -2
  60. data/spec/translations/extension/notransl.yml +0 -1
  61. data/spec/translations/general/en.yml +0 -52
  62. data/spec/translations/general/nolocale.yml +0 -6
  63. data/spec/translations/general/ru.yml +0 -7
  64. data/spec/translations/two/en.yml +0 -2
  65. data/spec/translations/two/fr.yml +0 -0
  66. data/spec/translations/with_regions/en-US.yml +0 -0
  67. data/spec/translations/yaml/en-GB.yml +0 -1
  68. data/spec/translations/yaml/en-us.yml +0 -1
  69. data/spec/translations/yaml/en.yaml +0 -1
  70. data/spec/yaml_loader_spec.rb +0 -63
@@ -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,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Locales::Vi do
4
- it 'formats Vietnamese time' do
5
- vi = R18n::I18n.new('vi')
6
- expect(vi.l(Time.utc(2009, 5, 1, 6, 7, 8), :standard, with_seconds: true))
7
- .to eq('06:07:08, 01/05/2009')
8
- end
9
- end
data/spec/r18n_spec.rb DELETED
@@ -1,192 +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 'allows to temporary change locale inside block' do
93
- R18n.set('ru')
94
- code_inside_block = nil
95
- R18n.change(R18n.locale('en')) { code_inside_block = R18n.get.locale.code }
96
- expect(code_inside_block).to eq('en')
97
- expect(R18n.get.locale.code).to eq('ru')
98
- end
99
-
100
- it 'has shortcut to load locale' do
101
- expect(R18n.locale('ru')).to eq(R18n::Locale.load('ru'))
102
- end
103
-
104
- it 'stores default loader class' do
105
- expect(R18n.default_loader).to eq(R18n::Loader::YAML)
106
- R18n.default_loader = Class
107
- expect(R18n.default_loader).to be_kind_of(Class)
108
- end
109
-
110
- it 'stores cache' do
111
- expect(R18n.cache).to be_kind_of(Hash)
112
-
113
- R18n.cache = { 1 => 2 }
114
- expect(R18n.cache).to eq(1 => 2)
115
-
116
- R18n.clear_cache!
117
- expect(R18n.cache).to eq({})
118
- end
119
-
120
- it 'merges hash recursively' do
121
- a = { a: 1, b: { ba: 1, bb: 1 }, c: 1 }
122
- b = { b: { bb: 2, bc: 2 }, c: 2 }
123
-
124
- R18n::Utils.deep_merge!(a, b)
125
- expect(a).to eq(a: 1, b: { ba: 1, bb: 2, bc: 2 }, c: 2)
126
- end
127
-
128
- it 'has l and t methods' do
129
- R18n.set('en')
130
- expect(t.yes).to eq('Yes')
131
- expect(l(Time.at(0).utc)).to eq('1970-01-01 00:00')
132
- end
133
-
134
- it 'has helpers mixin' do
135
- obj = R18n::I18n.new('en')
136
- R18n.set(obj)
137
-
138
- expect(r18n).to eq(obj)
139
- expect(i18n).to eq(obj)
140
- expect(t.yes).to eq('Yes')
141
- expect(l(Time.at(0).utc)).to eq('1970-01-01 00:00')
142
- end
143
-
144
- it 'returns available translations' do
145
- expect(R18n.available_locales(DIR)).to match_array([
146
- R18n.locale('nolocale'),
147
- R18n.locale('ru'),
148
- R18n.locale('en')
149
- ])
150
- end
151
-
152
- it 'uses default places' do
153
- R18n.default_places = DIR
154
- R18n.set('en')
155
- expect(t.one).to eq('One')
156
- expect(R18n.available_locales).to match_array([
157
- R18n.locale('ru'),
158
- R18n.locale('en'),
159
- R18n.locale('nolocale')
160
- ])
161
- end
162
-
163
- it 'sets default places by block' do
164
- R18n.default_places { DIR }
165
- expect(R18n.default_places).to eq(DIR)
166
- end
167
-
168
- it 'allows to ignore default places' do
169
- R18n.default_places = DIR
170
- i18n = R18n::I18n.new('en', nil)
171
- expect(i18n.one).not_to be_translated
172
- end
173
-
174
- it 'allows to load files with `.yaml` extension' do
175
- R18n.default_places = File.join(TRANSLATIONS, 'yaml')
176
- R18n.set('en')
177
- expect(t.one).to eq('One')
178
- end
179
-
180
- it 'allows to load files with downcased region in name' do
181
- R18n.default_places = File.join(TRANSLATIONS, 'yaml')
182
- R18n.set('en-us')
183
- expect(t.one).to eq('American One')
184
- end
185
-
186
- it 'allows to load files with upcased region in name' do
187
- R18n.default_places = File.join(TRANSLATIONS, 'yaml')
188
- R18n.set('en-gb')
189
- expect(R18n.get.locale.code).to eq('en-GB')
190
- expect(t.one).to eq('British One')
191
- end
192
- end
data/spec/spec_helper.rb DELETED
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pp'
4
-
5
- require_relative '../lib/r18n-core'
6
-
7
- TRANSLATIONS = File.join(__dir__, 'translations') unless defined? TRANSLATIONS
8
- DIR = File.join(TRANSLATIONS, 'general') unless defined? DIR
9
- TWO = File.join(TRANSLATIONS, 'two') unless defined? TWO
10
- unless defined? EXT
11
- EXT = R18n::Loader::YAML.new(File.join(TRANSLATIONS, 'extension'))
12
- end
13
-
14
- RSpec.configure do |config|
15
- config.before { R18n.clear_cache! }
16
- end
17
-
18
- gem 'kramdown'
19
- gem 'RedCloth'
20
-
21
- class CounterLoader
22
- attr_reader :available
23
- attr_reader :loaded
24
-
25
- def initialize(*available)
26
- @available = available.map { |i| R18n.locale(i) }
27
- @loaded = 0
28
- end
29
-
30
- def load(_locale)
31
- @loaded += 1
32
- {}
33
- end
34
-
35
- def hash
36
- @available.hash
37
- end
38
- end
@@ -1,225 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe R18n::Translated do
4
- let(:user_class) do
5
- 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
- end
14
-
15
- let(:user) { user_class.new }
16
-
17
- before do
18
- R18n.set('en')
19
- end
20
-
21
- it 'saves methods map' do
22
- user_class.translation :name, methods: { ru: :name_ru }
23
- expect(user_class.unlocalized_getters(:name)).to eq('ru' => 'name_ru')
24
- expect(user_class.unlocalized_setters(:name)).to eq('ru' => 'name_ru=')
25
- end
26
-
27
- it 'autodetects methods map' do
28
- user_class.translation :name
29
- expect(user_class.unlocalized_getters(:name)).to eq(
30
- 'en' => 'name_en', 'ru' => 'name_ru'
31
- )
32
- expect(user_class.unlocalized_setters(:name)).to eq(
33
- 'en' => 'name_en=', 'ru' => 'name_ru='
34
- )
35
- end
36
-
37
- it 'translates methods' do
38
- user_class.translation :name
39
-
40
- user.name = 'John'
41
- expect(user.name).to eq('John')
42
-
43
- R18n.set('ru')
44
- expect(user.name).to eq('John')
45
- user.name = 'Джон'
46
- expect(user.name).to eq('Джон')
47
- end
48
-
49
- it 'translates locales with regions' do
50
- some_translated_class = Class.new do
51
- include R18n::Translated
52
-
53
- def name_en_us
54
- 'John'
55
- end
56
-
57
- translation :name
58
- end
59
- obj = some_translated_class.new
60
-
61
- R18n.set('en-US')
62
- expect(obj.name).to eq('John')
63
- end
64
-
65
- it 'returns `TranslatedString`' do
66
- some_translated_class = Class.new do
67
- include R18n::Translated
68
-
69
- def name_en
70
- 'John'
71
- end
72
-
73
- translation :name
74
- end
75
- allow(some_translated_class).to receive(:name)
76
- .and_return 'SomeTranslatedClass'
77
- obj = some_translated_class.new
78
-
79
- expect(obj.name).to be_kind_of(R18n::TranslatedString)
80
- expect(obj.name.locale).to eq(R18n.locale('en'))
81
- expect(obj.name.path).to eq('SomeTranslatedClass#name')
82
- end
83
-
84
- it 'searchs translation by locales priority' do
85
- user_class.translation :name
86
-
87
- R18n.set(%w[nolocale ru en])
88
- user.name_ru = 'Иван'
89
- expect(user.name.locale).to eq(R18n.locale('ru'))
90
- end
91
-
92
- it 'uses default locale' do
93
- user_class.translation :name
94
-
95
- R18n.set('nolocale')
96
- user.name_en = 'John'
97
- expect(user.name.locale).to eq(R18n.locale('en'))
98
- end
99
-
100
- describe 'filters' do
101
- let(:user_class) do
102
- Class.new do
103
- include R18n::Translated
104
-
105
- def age_en
106
- { 1 => '%1 year', 'n' => '%1 years' }
107
- end
108
-
109
- translation :age, type: 'pl', no_params: true
110
- end
111
- end
112
-
113
- it 'uses them' do
114
- expect(user.age(20)).to eq('20 years')
115
- end
116
- end
117
-
118
- describe 'passing params to method' do
119
- let(:user_class) do
120
- Class.new do
121
- include R18n::Translated
122
-
123
- def no_params_en(*params)
124
- params.join(' ')
125
- end
126
-
127
- def params_en(*params)
128
- params.join(' ')
129
- end
130
-
131
- translations [:no_params, { no_params: true }], :params
132
- end
133
- end
134
-
135
- it 'works' do
136
- expect(user.no_params(1, 2)).to eq('')
137
- expect(user.params(1, 2)).to eq('1 2')
138
- end
139
- end
140
-
141
- it 'translates virtual methods' do
142
- virtual_class = Class.new do
143
- include R18n::Translated
144
-
145
- translation :no_method, methods: { en: :no_method_en }
146
-
147
- def method_missing(name, *_params)
148
- return name.to_s if name.to_s =~ /^no_method*/
149
-
150
- super
151
- end
152
-
153
- def respond_to_missing?(name, *_params)
154
- return true if name.to_s =~ /^no_method*/
155
-
156
- super
157
- end
158
- end
159
- virtual = virtual_class.new
160
-
161
- expect(virtual.no_method).to eq('no_method_en')
162
- end
163
-
164
- describe 'returning original type of result' do
165
- let(:user_class) do
166
- Class.new do
167
- include R18n::Translated
168
-
169
- translation :name
170
-
171
- def name_en
172
- :ivan
173
- end
174
- end
175
- end
176
-
177
- it 'works' do
178
- expect(user.name).to eq(:ivan)
179
- end
180
-
181
- context 'is nil' do
182
- let(:user_class) do
183
- Class.new do
184
- include R18n::Translated
185
-
186
- translation :name
187
-
188
- def name_en
189
- nil
190
- end
191
- end
192
- end
193
-
194
- it 'works' do
195
- expect(user.name).to be_nil
196
- end
197
- end
198
- end
199
-
200
- describe 'allowing to change I18n object' do
201
- let(:user_class) do
202
- Class.new do
203
- include R18n::Translated
204
-
205
- attr_accessor :name_ru, :name_en
206
-
207
- translation :name
208
-
209
- attr_accessor :r18n
210
- end
211
- end
212
-
213
- before do
214
- user.name_ru = 'Иван'
215
- user.name_en = 'John'
216
- end
217
-
218
- it 'works' do
219
- user.r18n = R18n::I18n.new('ru')
220
- expect(user.name).to eq('Иван')
221
- user.r18n = R18n::I18n.new('en')
222
- expect(user.name).to eq('John')
223
- end
224
- end
225
- end