r18n-rails-api 3.0.5 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,9 +31,9 @@ module R18n
31
31
 
32
32
  RESERVED_KEYS = %i[scope default separator].freeze
33
33
 
34
- # Find translation in R18n. It didnt use +locale+ argument, only current
35
- # R18n I18n object. Also it doesnt support Proc and variables in +default+
36
- # String option.
34
+ # Find translation in R18n. It didn't use `locale` argument, only current
35
+ # R18n I18n object. Also it doesn't support `Proc` and variables in
36
+ # `default` String option.
37
37
  def translate(locale, key, options = {})
38
38
  return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)
39
39
 
@@ -49,10 +49,11 @@ module R18n
49
49
  default = [default] unless default.is_a? Array
50
50
 
51
51
  default.each do |entry|
52
- if entry.is_a? Symbol
52
+ case entry
53
+ when Symbol
53
54
  value = lookup(locale, scope, entry, separator, params)
54
55
  return value unless value.is_a? Untranslated
55
- elsif entry.is_a? Proc
56
+ when Proc
56
57
  proc_key = options.delete(:object) || key
57
58
  return entry.call(proc_key, options)
58
59
  else
@@ -66,13 +67,13 @@ module R18n
66
67
  end
67
68
  end
68
69
 
69
- # Convert +object+ to String, according to the rules of the current
70
- # R18n locale. It didnt use +locale+ argument, only current R18n I18n
71
- # object. It support Integer, Float, Time, Date and DateTime.
70
+ # Convert `object` to `String`, according to the rules of the current
71
+ # R18n locale. It didn't use `locale` argument, only current R18n I18n
72
+ # object. It support `Integer`, `Float`, `Time`, `Date` and `DateTime`.
72
73
  #
73
- # Support Rails I18n (+:default+, +:short+, +:long+, +:long_ordinal+,
74
- # +:only_day+ and +:only_second+) and R18n (+:full+, +:human+, +:standard+
75
- # and +:month+) time formatters.
74
+ # Support Rails I18n (`:default`, `:short`, `:long`, `:long_ordinal`,
75
+ # `:only_day` and `:only_second`) and R18n (`:full`, `:human`, `:standard`
76
+ # and `:month`) time formatters.
76
77
  def localize(locale, object, format = :default, _options = {})
77
78
  i18n = get_i18n(locale)
78
79
  if format.is_a? Symbol
@@ -102,13 +103,12 @@ module R18n
102
103
  end
103
104
 
104
105
  def format_value(result)
105
- if result.is_a? TranslatedString
106
+ case result
107
+ when TranslatedString
106
108
  result.to_s
107
- elsif result.is_a? UnpluralizetedTranslation
108
- Utils.hash_map(result.to_hash) do |key, value|
109
- [RailsPlural.from_r18n(key), value]
110
- end
111
- elsif result.is_a? Translation
109
+ when UnpluralizedTranslation
110
+ result.to_hash.transform_keys { |key| RailsPlural.from_r18n(key) }
111
+ when Translation
112
112
  translation_to_hash(result)
113
113
  else
114
114
  result
@@ -116,18 +116,15 @@ module R18n
116
116
  end
117
117
 
118
118
  def translation_to_hash(translation)
119
- Utils.hash_map(translation.to_hash) do |key, value|
120
- value =
121
- if value.is_a? Hash
122
- translation_to_hash(value)
123
- else
124
- format_value(value)
125
- end
126
- [key.to_sym, value]
127
- end
119
+ translation.to_hash.map do |key, value|
120
+ [
121
+ key.to_sym,
122
+ value.is_a?(Hash) ? translation_to_hash(value) : format_value(value)
123
+ ]
124
+ end.to_h
128
125
  end
129
126
 
130
- # Find translation by <tt>scope.key(params)</tt> in current R18n I18n
127
+ # Find translation by `scope.key(params)` in current R18n I18n
131
128
  # object.
132
129
  def lookup(locale, scope, key, separator, params)
133
130
  keys = (Array(scope) + Array(key))
@@ -27,9 +27,7 @@ R18n::Filters.add(String, :named_variables) do |content, config, params|
27
27
  content = content.clone
28
28
  params.each_pair do |name, value|
29
29
  value = config[:locale].localize(value)
30
- if defined? ActiveSupport::SafeBuffer
31
- value = ActiveSupport::SafeBuffer.new + value
32
- end
30
+ value = ActiveSupport::SafeBuffer.new + value if defined? ActiveSupport::SafeBuffer
33
31
  content.gsub! "%{#{name}}", value
34
32
  content.gsub! "{{#{name}}}", value
35
33
  end
@@ -39,7 +37,7 @@ end
39
37
 
40
38
  module R18n
41
39
  # Class to mark unpluralized translation and convert Rails plural keys
42
- class RailsUnpluralizetedTranslation < UnpluralizetedTranslation
40
+ class RailsUnpluralizedTranslation < UnpluralizedTranslation
43
41
  def [](name, *params)
44
42
  result = super
45
43
  if result.is_a? Untranslated
@@ -51,15 +49,15 @@ module R18n
51
49
  end
52
50
  end
53
51
 
54
- # Pluralization by named variable <tt>%{count}</tt>.
52
+ # Pluralization by named variable `%{count}`.
55
53
  R18n::Filters.add('pl', :named_pluralization) do |content, config, param|
56
54
  if param.is_a?(Hash) && param.key?(:count)
57
55
  hash = content.to_hash
58
56
  type = config[:locale].pluralize(param[:count])
59
57
  type = 'n' unless hash.key?(type)
60
58
  hash[type]
61
- elsif content.is_a? R18n::UnpluralizetedTranslation
62
- R18n::RailsUnpluralizetedTranslation.new(
59
+ elsif content.is_a? R18n::UnpluralizedTranslation
60
+ R18n::RailsUnpluralizedTranslation.new(
63
61
  config[:locale], config[:path],
64
62
  locale: config[:locale], translations: content.to_hash
65
63
  )
@@ -34,41 +34,45 @@ module R18n
34
34
  class Rails
35
35
  include ::R18n::YamlMethods
36
36
 
37
- # Create new loader for some +backend+ from Rails I18n. Backend must have
38
- # +reload!+, +init_translations+ and +translations+ methods.
37
+ # Create new loader for some `backend` from Rails I18n. Backend must have
38
+ # `reload!`, `init_translations` and `translations` methods.
39
39
  def initialize(backend = ::I18n::Backend::Simple.new)
40
40
  @backend = backend
41
41
  detect_yaml_private_type
42
42
  end
43
43
 
44
- # Array of locales, which has translations in +I18n.load_path+.
44
+ # `Array` of locales, which has translations in `I18n.load_path`.
45
45
  def available
46
46
  reload!
47
47
  @translations.keys.map { |code| R18n.locale(code) }
48
48
  end
49
49
 
50
- # Return Hash with translations for +locale+.
50
+ # Return `Hash` with translations for `locale`.
51
51
  def load(locale)
52
52
  initialize_types
53
53
  reload!
54
54
  @translations[locale.code]
55
55
  end
56
56
 
57
- # Reload backend if <tt>I18n.load_path</tt> is changed.
57
+ # Reload backend if `I18n.load_path` is changed.
58
58
  def reload!
59
59
  return if defined?(@last_path) && @last_path == ::I18n.load_path
60
+
60
61
  @last_path = ::I18n.load_path.clone
61
62
  @backend.reload!
62
63
  @backend.send(:init_translations)
63
- @translations = transform @backend.send(:translations)
64
+ @translations =
65
+ @backend.send(:translations).map do |locale, values|
66
+ [R18n::Locale.sanitize_code(locale), transform(values)]
67
+ end.to_h
64
68
  end
65
69
 
66
- # Return hash for object and <tt>I18n.load_path</tt>.
70
+ # Return hash for object and `I18n.load_path`.
67
71
  def hash
68
72
  ::I18n.load_path.hash
69
73
  end
70
74
 
71
- # Is another +loader+ is also load Rails translations.
75
+ # Is another `loader` is also load Rails translations.
72
76
  def ==(other)
73
77
  self.class == other.class
74
78
  end
@@ -80,15 +84,13 @@ module R18n
80
84
  if value.is_a? Hash
81
85
  if value.empty?
82
86
  value
83
- elsif value.keys.inject(true) { |a, i| a && RailsPlural.is_rails?(i) }
87
+ elsif value.keys.inject(true) { |a, i| a && RailsPlural.rails?(i) }
84
88
  Typed.new(
85
89
  'pl',
86
- R18n::Utils.hash_map(value) do |k, v|
87
- [RailsPlural.to_r18n(k), transform(v)]
88
- end
90
+ value.map { |k, v| [RailsPlural.to_r18n(k), transform(v)] }.to_h
89
91
  )
90
92
  else
91
- Utils.hash_map(value) { |k, v| [k.to_s, transform(v)] }
93
+ value.map { |k, v| [k.to_s, transform(v)] }.to_h
92
94
  end
93
95
  elsif defined?(@private_type_class) && value.is_a?(@private_type_class)
94
96
  Typed.new(value.type_id, value.value)
@@ -20,19 +20,19 @@
20
20
  module R18n
21
21
  # Converter between R18n and Rails I18n plural keys.
22
22
  class RailsPlural
23
- # Check, that +key+ is Rails plural key.
24
- def self.is_rails?(k)
25
- %i[zero one few many other].include? k
23
+ # Check, that `key` is Rails plural key.
24
+ def self.rails?(key)
25
+ %i[zero one few many other].include? key
26
26
  end
27
27
 
28
28
  # Convert Rails I18n plural key to R18n.
29
- def self.to_r18n(k)
30
- { zero: 0, one: 1, few: 2, many: 'n', other: 'n' }[k]
29
+ def self.to_r18n(key)
30
+ { zero: 0, one: 1, few: 2, many: 'n', other: 'n' }[key]
31
31
  end
32
32
 
33
33
  # Convert R18n plural key to Rails I18n.
34
- def self.from_r18n(k)
35
- { 0 => :zero, 1 => :one, 2 => :few, 'n' => :other }[k]
34
+ def self.from_r18n(key)
35
+ { 0 => :zero, 1 => :one, 2 => :few, 'n' => :other }[key]
36
36
  end
37
37
  end
38
38
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module R18n
4
+ module Rails
5
+ module API
6
+ VERSION = '5.0.0'
7
+ end
8
+ end
9
+ end
metadata CHANGED
@@ -1,43 +1,184 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r18n-rails-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Sitnik
8
- autorequire:
8
+ - Alexander Popov
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2018-07-19 00:00:00.000000000 Z
12
+ date: 2021-03-03 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: i18n
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - ">="
18
+ - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: '0'
20
+ version: '1.0'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - ">="
25
+ - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: '0'
27
+ version: '1.0'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: r18n-core
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - '='
32
+ - - "~>"
32
33
  - !ruby/object:Gem::Version
33
- version: 3.0.5
34
+ version: '5.0'
34
35
  type: :runtime
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - '='
39
+ - - "~>"
39
40
  - !ruby/object:Gem::Version
40
- version: 3.0.5
41
+ version: '5.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: pry-byebug
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.9'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.9'
56
+ - !ruby/object:Gem::Dependency
57
+ name: gem_toys
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.7.1
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.7.1
70
+ - !ruby/object:Gem::Dependency
71
+ name: toys
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 0.11.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 0.11.0
84
+ - !ruby/object:Gem::Dependency
85
+ name: rubocop
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '1.6'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.6'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop-performance
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '1.9'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '1.9'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rubocop-rails
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '2.9'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '2.9'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rubocop-rake
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: 0.5.1
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 0.5.1
140
+ - !ruby/object:Gem::Dependency
141
+ name: codecov
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: 0.5.0
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: 0.5.0
154
+ - !ruby/object:Gem::Dependency
155
+ name: rspec
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: '3.10'
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: '3.10'
168
+ - !ruby/object:Gem::Dependency
169
+ name: simplecov
170
+ requirement: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - "~>"
173
+ - !ruby/object:Gem::Version
174
+ version: 0.21.0
175
+ type: :development
176
+ prerelease: false
177
+ version_requirements: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - "~>"
180
+ - !ruby/object:Gem::Version
181
+ version: 0.21.0
41
182
  description: |2
42
183
  R18n backend for Rails I18n and R18n filters and loader to support Rails
43
184
  translation format.
@@ -45,41 +186,34 @@ description: |2
45
186
  translation support for any classes, time and number localization, several
46
187
  user language support, agnostic core package with out-of-box support for
47
188
  Rails, Sinatra and desktop applications.
48
- email: andrey@sitnik.ru
189
+ email:
190
+ - andrey@sitnik.ru
191
+ - alex.wayfer@gmail.com
49
192
  executables: []
50
193
  extensions: []
51
194
  extra_rdoc_files:
52
195
  - README.md
53
196
  - LICENSE
54
197
  files:
55
- - ".rspec"
198
+ - ChangeLog.md
56
199
  - LICENSE
57
200
  - README.md
58
- - Rakefile
59
201
  - lib/r18n-rails-api.rb
60
202
  - lib/r18n-rails-api/backend.rb
61
203
  - lib/r18n-rails-api/filters.rb
62
204
  - lib/r18n-rails-api/loader.rb
63
205
  - lib/r18n-rails-api/rails_plural.rb
64
- - r18n-rails-api.gemspec
65
- - spec/backend_spec.rb
66
- - spec/data/general/en.yml
67
- - spec/data/general/ru.yml
68
- - spec/data/other/en.yml
69
- - spec/data/other/ru.yml
70
- - spec/data/pl/ru.yml
71
- - spec/data/simple/de-CH.yml
72
- - spec/data/simple/en.yml
73
- - spec/data/simple/ru.rb
74
- - spec/data/simple/russian.yml
75
- - spec/filters_spec.rb
76
- - spec/loader_spec.rb
77
- - spec/spec_helper.rb
78
- homepage: https://github.com/ai/r18n/tree/master/r18n-rails-api
206
+ - lib/r18n-rails-api/version.rb
207
+ homepage: https://github.com/r18n/r18n-rails-api
79
208
  licenses:
80
- - LGPL-3
81
- metadata: {}
82
- post_install_message:
209
+ - LGPL-3.0
210
+ metadata:
211
+ bug_tracker_uri: https://github.com/r18n/r18n-rails-api/issues
212
+ changelog_uri: https://github.com/r18n/r18n-rails-api/blob/5.0.0/ChangeLog.md
213
+ documentation_uri: http://www.rubydoc.info/gems/r18n-rails-api/5.0.0
214
+ homepage_uri: https://github.com/r18n/r18n-rails-api
215
+ source_code_uri: https://github.com/r18n/r18n-rails-api
216
+ post_install_message:
83
217
  rdoc_options: []
84
218
  require_paths:
85
219
  - lib
@@ -87,29 +221,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
221
  requirements:
88
222
  - - ">="
89
223
  - !ruby/object:Gem::Version
90
- version: '0'
224
+ version: '2.5'
225
+ - - "<"
226
+ - !ruby/object:Gem::Version
227
+ version: '4'
91
228
  required_rubygems_version: !ruby/object:Gem::Requirement
92
229
  requirements:
93
230
  - - ">="
94
231
  - !ruby/object:Gem::Version
95
232
  version: '0'
96
233
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.7.6
99
- signing_key:
234
+ rubygems_version: 3.2.3
235
+ signing_key:
100
236
  specification_version: 4
101
237
  summary: Rails I18n compatibility for R18n
102
- test_files:
103
- - spec/backend_spec.rb
104
- - spec/data/general/en.yml
105
- - spec/data/general/ru.yml
106
- - spec/data/other/en.yml
107
- - spec/data/other/ru.yml
108
- - spec/data/pl/ru.yml
109
- - spec/data/simple/de-CH.yml
110
- - spec/data/simple/en.yml
111
- - spec/data/simple/ru.rb
112
- - spec/data/simple/russian.yml
113
- - spec/filters_spec.rb
114
- - spec/loader_spec.rb
115
- - spec/spec_helper.rb
238
+ test_files: []