r18n-core 3.2.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -33
  3. data/lib/r18n-core.rb +29 -15
  4. data/lib/r18n-core/filter_list.rb +15 -10
  5. data/lib/r18n-core/filters.rb +22 -26
  6. data/lib/r18n-core/helpers.rb +2 -2
  7. data/lib/r18n-core/i18n.rb +61 -59
  8. data/lib/r18n-core/locale.rb +120 -97
  9. data/{locales → lib/r18n-core/locales}/af.rb +12 -8
  10. data/lib/r18n-core/locales/az.rb +38 -0
  11. data/lib/r18n-core/locales/bg.rb +29 -0
  12. data/{locales → lib/r18n-core/locales}/ca.rb +9 -6
  13. data/{locales → lib/r18n-core/locales}/cs.rb +11 -7
  14. data/lib/r18n-core/locales/cy.rb +50 -0
  15. data/{locales → lib/r18n-core/locales}/da.rb +10 -7
  16. data/lib/r18n-core/locales/de.rb +33 -0
  17. data/{locales → lib/r18n-core/locales}/en-au.rb +1 -1
  18. data/{locales → lib/r18n-core/locales}/en-gb.rb +1 -1
  19. data/{locales → lib/r18n-core/locales}/en-us.rb +2 -1
  20. data/{locales → lib/r18n-core/locales}/en.rb +8 -5
  21. data/{locales → lib/r18n-core/locales}/eo.rb +5 -3
  22. data/lib/r18n-core/locales/es-cl.rb +16 -0
  23. data/{locales → lib/r18n-core/locales}/es-us.rb +4 -3
  24. data/{locales → lib/r18n-core/locales}/es.rb +5 -3
  25. data/{locales → lib/r18n-core/locales}/fa.rb +1 -1
  26. data/lib/r18n-core/locales/fi.rb +39 -0
  27. data/{locales → lib/r18n-core/locales}/fr.rb +10 -6
  28. data/{locales → lib/r18n-core/locales}/gl.rb +5 -3
  29. data/{locales → lib/r18n-core/locales}/hr.rb +8 -5
  30. data/{locales → lib/r18n-core/locales}/hu.rb +8 -5
  31. data/{locales → lib/r18n-core/locales}/id.rb +8 -4
  32. data/{locales → lib/r18n-core/locales}/it.rb +8 -5
  33. data/{locales → lib/r18n-core/locales}/ja.rb +4 -3
  34. data/{locales → lib/r18n-core/locales}/kk.rb +11 -7
  35. data/{locales → lib/r18n-core/locales}/ko.rb +0 -0
  36. data/lib/r18n-core/locales/lv.rb +46 -0
  37. data/lib/r18n-core/locales/mn.rb +30 -0
  38. data/{locales → lib/r18n-core/locales}/nb.rb +5 -3
  39. data/{locales → lib/r18n-core/locales}/nl.rb +10 -7
  40. data/{locales → lib/r18n-core/locales}/no.rb +0 -0
  41. data/lib/r18n-core/locales/pl.rb +45 -0
  42. data/{locales → lib/r18n-core/locales}/pt-br.rb +1 -1
  43. data/lib/r18n-core/locales/pt.rb +35 -0
  44. data/lib/r18n-core/locales/ru.rb +46 -0
  45. data/{locales → lib/r18n-core/locales}/sk.rb +10 -6
  46. data/{locales → lib/r18n-core/locales}/sr-latn.rb +5 -3
  47. data/{locales → lib/r18n-core/locales}/sv-se.rb +6 -4
  48. data/lib/r18n-core/locales/th.rb +41 -0
  49. data/{locales → lib/r18n-core/locales}/tr.rb +5 -4
  50. data/lib/r18n-core/locales/uk.rb +31 -0
  51. data/{locales → lib/r18n-core/locales}/vi.rb +11 -7
  52. data/{locales → lib/r18n-core/locales}/zh-cn.rb +1 -1
  53. data/{locales → lib/r18n-core/locales}/zh-tw.rb +3 -3
  54. data/{locales → lib/r18n-core/locales}/zh.rb +4 -3
  55. data/lib/r18n-core/translated.rb +35 -34
  56. data/lib/r18n-core/translated_string.rb +4 -2
  57. data/lib/r18n-core/translation.rb +16 -17
  58. data/lib/r18n-core/unsupported_locale.rb +6 -5
  59. data/lib/r18n-core/untranslated.rb +10 -4
  60. data/lib/r18n-core/utils.rb +1 -14
  61. data/lib/r18n-core/version.rb +1 -1
  62. data/lib/r18n-core/yaml_loader.rb +13 -13
  63. data/lib/r18n-core/yaml_methods.rb +3 -1
  64. data/r18n-core.gemspec +1 -1
  65. data/spec/filters_spec.rb +3 -0
  66. data/spec/i18n_spec.rb +22 -8
  67. data/spec/locale_spec.rb +4 -5
  68. data/spec/locales/af_spec.rb +9 -0
  69. data/spec/locales/en-us_spec.rb +16 -1
  70. data/spec/locales/es-us_spec.rb +11 -0
  71. data/spec/locales/fi_spec.rb +9 -0
  72. data/spec/locales/hu_spec.rb +2 -0
  73. data/spec/locales/id_spec.rb +23 -0
  74. data/spec/locales/vi_spec.rb +4 -3
  75. data/spec/r18n_spec.rb +21 -7
  76. data/spec/spec_helper.rb +0 -7
  77. data/spec/translated_spec.rb +121 -65
  78. data/spec/translation_spec.rb +5 -2
  79. data/spec/translations/with_regions/en-US.yml +0 -0
  80. data/spec/translations/yaml/en-GB.yml +1 -0
  81. data/spec/translations/yaml/en-us.yml +1 -0
  82. data/spec/yaml_loader_spec.rb +4 -2
  83. metadata +64 -50
  84. data/locales/az.rb +0 -34
  85. data/locales/bg.rb +0 -25
  86. data/locales/cy.rb +0 -50
  87. data/locales/de.rb +0 -29
  88. data/locales/fi.rb +0 -35
  89. data/locales/lv.rb +0 -41
  90. data/locales/mn.rb +0 -27
  91. data/locales/pl.rb +0 -40
  92. data/locales/pt.rb +0 -29
  93. data/locales/ru.rb +0 -41
  94. data/locales/th.rb +0 -37
  95. data/locales/uk.rb +0 -27
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- describe R18n::Locales::EnUs do
3
+ describe R18n::Locales::EnUS do
4
4
  it 'formats American English date' do
5
5
  en_us = R18n::I18n.new('en-US')
6
6
  expect(en_us.l(Date.parse('2009-05-01'), :full)).to eq('May 1st, 2009')
@@ -10,4 +10,19 @@ describe R18n::Locales::EnUs do
10
10
  expect(en_us.l(Date.parse('2009-05-11'), :full)).to eq('May 11th, 2009')
11
11
  expect(en_us.l(Date.parse('2009-05-21'), :full)).to eq('May 21st, 2009')
12
12
  end
13
+
14
+ it 'formats American English time' do
15
+ en_us = R18n::I18n.new('en-US')
16
+ expect(
17
+ en_us.l(Time.utc(2009, 5, 1, 6, 7, 8), :standard, with_seconds: true)
18
+ )
19
+ .to eq('05/01/2009 06:07:08 AM')
20
+ end
21
+
22
+ it 'takes locaize from `en` locale when loaded from dir with only regions' do
23
+ en_us = R18n::I18n.new(
24
+ 'en-US', File.join(__dir__, '..', 'translations', 'with_regions')
25
+ )
26
+ expect(en_us.l(Time.now - 61, :human)).to eq '1 minute ago'
27
+ end
13
28
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe R18n::Locales::EnUS do
4
+ it 'formats American Spanish time' do
5
+ es_us = R18n::I18n.new('es-US')
6
+ expect(
7
+ es_us.l(Time.utc(2009, 5, 1, 6, 7, 8), :standard, with_seconds: true)
8
+ )
9
+ .to eq('05/01/2009 06:07:08 AM')
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe R18n::Locales::Fi do
4
+ it 'formats Finnish time' do
5
+ fi = R18n::I18n.new('fi')
6
+ expect(fi.l(Time.utc(2009, 5, 1, 6, 7, 8), :standard, with_seconds: true))
7
+ .to eq('01.05.2009 06.07.08')
8
+ end
9
+ end
@@ -13,5 +13,7 @@ describe R18n::Locales::Hu do
13
13
  hu = R18n::I18n.new('hu')
14
14
  expect(hu.l(Time.at(0).utc)).to eq('1970. 01. 01., 00:00')
15
15
  expect(hu.l(Time.at(0).utc, :full)).to eq('1970. január 1., 00:00')
16
+ expect(hu.l(Time.utc(2009, 5, 1, 6, 7, 8), :standard, with_seconds: true))
17
+ .to eq('2009. 05. 01., 06:07:08')
16
18
  end
17
19
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe R18n::Locales::Id do
4
+ it 'formats Indonesian date' do
5
+ id = R18n::I18n.new('id')
6
+
7
+ expect(id.l(Date.parse('2009-11-01'), :full)).to eq('1 November 2009')
8
+ expect(id.l(Date.parse('2009-11-02'), :standard)).to eq('02/11/2009')
9
+ expect(id.l(Date.parse('2009-11-21'), '%d/%b/%y')).to eq('21/Nov/09')
10
+ end
11
+
12
+ it 'formats Indonesian time' do
13
+ id = R18n::I18n.new('id')
14
+ time = id.l(Time.utc(2009, 5, 1, 6, 7, 8), :standard)
15
+ expect(time).to eq('01/05/2009 pukul 06.07')
16
+ end
17
+
18
+ it 'formats Indonesian time with seconds' do
19
+ id = R18n::I18n.new('id')
20
+ time = id.l(Time.utc(2009, 5, 1, 6, 7, 8), :standard, with_seconds: true)
21
+ expect(time).to eq('01/05/2009 pukul 06.07.08')
22
+ end
23
+ end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
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')
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')
7
8
  end
8
9
  end
@@ -89,6 +89,14 @@ describe R18n do
89
89
  expect(R18n.change(R18n.locale('en')).locale.code).to eq('en')
90
90
  end
91
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
+
92
100
  it 'has shortcut to load locale' do
93
101
  expect(R18n.locale('ru')).to eq(R18n::Locale.load('ru'))
94
102
  end
@@ -109,13 +117,6 @@ describe R18n do
109
117
  expect(R18n.cache).to eq({})
110
118
  end
111
119
 
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
120
  it 'merges hash recursively' do
120
121
  a = { a: 1, b: { ba: 1, bb: 1 }, c: 1 }
121
122
  b = { b: { bb: 2, bc: 2 }, c: 2 }
@@ -175,4 +176,17 @@ describe R18n do
175
176
  R18n.set('en')
176
177
  expect(t.one).to eq('One')
177
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
178
192
  end
@@ -1,15 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- ## https://github.com/stevekinney/pizza/issues/103#issuecomment-136052789
4
- ## https://github.com/docker-library/ruby/issues/45
5
- Encoding.default_external = 'UTF-8'
6
-
7
3
  require 'pp'
8
4
 
9
5
  require_relative '../lib/r18n-core'
10
- Dir.glob(File.join(__dir__, '..', 'locales', '*.rb')) do |locale_file|
11
- require locale_file
12
- end
13
6
 
14
7
  TRANSLATIONS = File.join(__dir__, 'translations') unless defined? TRANSLATIONS
15
8
  DIR = File.join(TRANSLATIONS, 'general') unless defined? DIR
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe R18n::Translated do
4
- before do
5
- @user_class = Class.new do
4
+ let(:user_class) do
5
+ Class.new do
6
6
  include R18n::Translated
7
7
  attr_accessor :name_ru, :name_en
8
8
 
@@ -10,28 +10,32 @@ describe R18n::Translated do
10
10
 
11
11
  def name_ru!; end
12
12
  end
13
+ end
14
+
15
+ let(:user) { user_class.new }
16
+
17
+ before do
13
18
  R18n.set('en')
14
19
  end
15
20
 
16
21
  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=')
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=')
20
25
  end
21
26
 
22
27
  it 'autodetects methods map' do
23
- @user_class.translation :name
24
- expect(@user_class.unlocalized_getters(:name)).to eq(
28
+ user_class.translation :name
29
+ expect(user_class.unlocalized_getters(:name)).to eq(
25
30
  'en' => 'name_en', 'ru' => 'name_ru'
26
31
  )
27
- expect(@user_class.unlocalized_setters(:name)).to eq(
32
+ expect(user_class.unlocalized_setters(:name)).to eq(
28
33
  'en' => 'name_en=', 'ru' => 'name_ru='
29
34
  )
30
35
  end
31
36
 
32
37
  it 'translates methods' do
33
- @user_class.translation :name
34
- user = @user_class.new
38
+ user_class.translation :name
35
39
 
36
40
  user.name = 'John'
37
41
  expect(user.name).to eq('John')
@@ -42,8 +46,24 @@ describe R18n::Translated do
42
46
  expect(user.name).to eq('Джон')
43
47
  end
44
48
 
45
- it 'returns TranslatedString' do
46
- class SomeTranslatedClass
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
47
67
  include R18n::Translated
48
68
 
49
69
  def name_en
@@ -52,7 +72,9 @@ describe R18n::Translated do
52
72
 
53
73
  translation :name
54
74
  end
55
- obj = ::SomeTranslatedClass.new
75
+ allow(some_translated_class).to receive(:name)
76
+ .and_return 'SomeTranslatedClass'
77
+ obj = some_translated_class.new
56
78
 
57
79
  expect(obj.name).to be_kind_of(R18n::TranslatedString)
58
80
  expect(obj.name.locale).to eq(R18n.locale('en'))
@@ -60,8 +82,7 @@ describe R18n::Translated do
60
82
  end
61
83
 
62
84
  it 'searchs translation by locales priority' do
63
- @user_class.translation :name
64
- user = @user_class.new
85
+ user_class.translation :name
65
86
 
66
87
  R18n.set(%w[nolocale ru en])
67
88
  user.name_ru = 'Иван'
@@ -69,101 +90,136 @@ describe R18n::Translated do
69
90
  end
70
91
 
71
92
  it 'uses default locale' do
72
- @user_class.translation :name
73
- user = @user_class.new
93
+ user_class.translation :name
74
94
 
75
95
  R18n.set('nolocale')
76
96
  user.name_en = 'John'
77
97
  expect(user.name.locale).to eq(R18n.locale('en'))
78
98
  end
79
99
 
80
- it 'uses filters' do
81
- @user_class.class_exec do
82
- def age_en
83
- { 1 => '%1 year', 'n' => '%1 years' }
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
84
110
  end
85
- translation :age, type: 'pl', no_params: true
86
111
  end
87
- user = @user_class.new
88
112
 
89
- expect(user.age(20)).to eq('20 years')
113
+ it 'uses them' do
114
+ expect(user.age(20)).to eq('20 years')
115
+ end
90
116
  end
91
117
 
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
118
+ describe 'passing params to method' do
119
+ let(:user_class) do
120
+ Class.new do
121
+ include R18n::Translated
97
122
 
98
- def params_en(*params)
99
- params.join(' ')
100
- end
123
+ def no_params_en(*params)
124
+ params.join(' ')
125
+ end
126
+
127
+ def params_en(*params)
128
+ params.join(' ')
129
+ end
101
130
 
102
- translations [:no_params, { no_params: true }], :params
131
+ translations [:no_params, { no_params: true }], :params
132
+ end
103
133
  end
104
- user = @user_class.new
105
134
 
106
- expect(user.no_params(1, 2)).to eq('')
107
- expect(user.params(1, 2)).to eq('1 2')
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
108
139
  end
109
140
 
110
141
  it 'translates virtual methods' do
111
- @virtual_class = Class.new do
142
+ virtual_class = Class.new do
112
143
  include R18n::Translated
113
144
 
114
145
  translation :no_method, methods: { en: :no_method_en }
115
146
 
116
147
  def method_missing(name, *_params)
117
148
  return name.to_s if name.to_s =~ /^no_method*/
149
+
118
150
  super
119
151
  end
120
152
 
121
153
  def respond_to_missing?(name, *_params)
122
154
  return true if name.to_s =~ /^no_method*/
155
+
123
156
  super
124
157
  end
125
158
  end
126
- virtual = @virtual_class.new
159
+ virtual = virtual_class.new
127
160
 
128
161
  expect(virtual.no_method).to eq('no_method_en')
129
162
  end
130
163
 
131
- it 'returns original type of result' do
132
- @user_class.class_exec do
133
- translation :name
134
- def name_en
135
- :ivan
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
136
174
  end
137
175
  end
138
- user = @user_class.new
139
176
 
140
- expect(user.name).to eq(:ivan)
141
- end
177
+ it 'works' do
178
+ expect(user.name).to eq(:ivan)
179
+ end
142
180
 
143
- it 'returns nil' do
144
- @user_class.class_exec do
145
- translation :name
146
- def name_en
147
- nil
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
148
192
  end
149
- end
150
- user = @user_class.new
151
193
 
152
- expect(user.name).to be_nil
194
+ it 'works' do
195
+ expect(user.name).to be_nil
196
+ end
197
+ end
153
198
  end
154
199
 
155
- it 'allows to change I18n object' do
156
- @user_class.class_exec do
157
- translation :name
158
- attr_accessor :r18n
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
159
211
  end
160
- user = @user_class.new
161
- user.name_ru = 'Иван'
162
- user.name_en = 'John'
163
212
 
164
- user.r18n = R18n::I18n.new('ru')
165
- expect(user.name).to eq('Иван')
166
- user.r18n = R18n::I18n.new('en')
167
- expect(user.name).to eq('John')
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
168
224
  end
169
225
  end
@@ -104,8 +104,11 @@ describe R18n::Translation do
104
104
 
105
105
  it 'filters typed data' do
106
106
  en = R18n.locale('en')
107
- translation = R18n::Translation.new(en, '', locale: en, translations:
108
- { 'count' => R18n::Typed.new('pl', 1 => 'one', 'n' => 'many') })
107
+ translation = R18n::Translation.new(
108
+ en, '', locale: en, translations: {
109
+ 'count' => R18n::Typed.new('pl', 1 => 'one', 'n' => 'many')
110
+ }
111
+ )
109
112
 
110
113
  expect(translation.count(1)).to eq('one')
111
114
  expect(translation.count(5)).to eq('many')