r18n-rails-api 3.2.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/lib/r18n-rails-api/backend.rb +17 -22
- data/lib/r18n-rails-api/filters.rb +1 -1
- data/lib/r18n-rails-api/loader.rb +13 -14
- data/lib/r18n-rails-api/rails_plural.rb +7 -7
- data/r18n-rails-api.gemspec +1 -1
- data/spec/backend_spec.rb +13 -16
- data/spec/filters_spec.rb +2 -0
- data/spec/loader_spec.rb +8 -8
- data/spec/spec_helper.rb +0 -4
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '099a10ed8e4a7e10242249f77a27092dae05fad11e08c3ce9753e9c78c5cbf1c'
|
4
|
+
data.tar.gz: d46f35406a6f561b1c86fbca7248b8faaa5978133fd8afd33ee6371775f2eab9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e203f84f913425ee61448023acad3bed063bc36b3844a588b61d75ce2c7ad0cb8882f00b939da0ac2ec0d077346ed8f36a888617a47693d39cd660fc5b782dc7
|
7
|
+
data.tar.gz: 6f9dcc914c984f762b11beadec8dc220435299f5fad7254bfe2be216dc7be5eef5b2ef071c351304d13c4beb7067eb6fbeed5ea03a0a51378972c015cb0da07b
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Rails I18n compatibility for R18n:
|
4
4
|
* R18n loader for Rails I18n translation format;
|
5
|
-
* R18n
|
5
|
+
* R18n back-end.
|
6
6
|
|
7
7
|
## How To
|
8
8
|
|
@@ -30,9 +30,9 @@ i18n = R18n::I18n.new('en', R18n::Loader::Rails)
|
|
30
30
|
i18n.posts(count: 5) #=> "5 posts"
|
31
31
|
```
|
32
32
|
|
33
|
-
###
|
33
|
+
### Back-end
|
34
34
|
|
35
|
-
You can use R18n as a
|
35
|
+
You can use R18n as a back-end for Rails I18n:
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
require 'r18n-rails-api'
|
@@ -58,8 +58,8 @@ I18n.t :users, count: 5 #=> "5 users"
|
|
58
58
|
## License
|
59
59
|
|
60
60
|
R18n is licensed under the GNU Lesser General Public License version 3.
|
61
|
-
You can read it in LICENSE file or in
|
61
|
+
You can read it in LICENSE file or in [www.gnu.org/licenses/lgpl-3.0.html](https://www.gnu.org/licenses/lgpl-3.0.html).
|
62
62
|
|
63
63
|
## Author
|
64
64
|
|
65
|
-
Andrey “A.I.” Sitnik
|
65
|
+
Andrey “A.I.” Sitnik [andrey@sitnik.ru](mailto:andrey@sitnik.ru)
|
@@ -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 didn
|
35
|
-
# R18n I18n object. Also it doesn
|
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
|
|
@@ -66,13 +66,13 @@ module R18n
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
# Convert
|
70
|
-
# R18n locale. It didn
|
71
|
-
# object. It support Integer
|
69
|
+
# Convert `object` to `String`, according to the rules of the current
|
70
|
+
# R18n locale. It didn't use `locale` argument, only current R18n I18n
|
71
|
+
# object. It support `Integer`, `Float`, `Time`, `Date` and `DateTime`.
|
72
72
|
#
|
73
|
-
# Support Rails I18n (
|
74
|
-
#
|
75
|
-
# and
|
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.
|
76
76
|
def localize(locale, object, format = :default, _options = {})
|
77
77
|
i18n = get_i18n(locale)
|
78
78
|
if format.is_a? Symbol
|
@@ -105,9 +105,7 @@ module R18n
|
|
105
105
|
if result.is_a? TranslatedString
|
106
106
|
result.to_s
|
107
107
|
elsif result.is_a? UnpluralizetedTranslation
|
108
|
-
|
109
|
-
[RailsPlural.from_r18n(key), value]
|
110
|
-
end
|
108
|
+
result.to_hash.map { |k, v| [RailsPlural.from_r18n(k), v] }.to_h
|
111
109
|
elsif result.is_a? Translation
|
112
110
|
translation_to_hash(result)
|
113
111
|
else
|
@@ -116,18 +114,15 @@ module R18n
|
|
116
114
|
end
|
117
115
|
|
118
116
|
def translation_to_hash(translation)
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
126
|
-
[key.to_sym, value]
|
127
|
-
end
|
117
|
+
translation.to_hash.map do |key, value|
|
118
|
+
[
|
119
|
+
key.to_sym,
|
120
|
+
value.is_a?(Hash) ? translation_to_hash(value) : format_value(value)
|
121
|
+
]
|
122
|
+
end.to_h
|
128
123
|
end
|
129
124
|
|
130
|
-
# Find translation by
|
125
|
+
# Find translation by `scope.key(params)` in current R18n I18n
|
131
126
|
# object.
|
132
127
|
def lookup(locale, scope, key, separator, params)
|
133
128
|
keys = (Array(scope) + Array(key))
|
@@ -51,7 +51,7 @@ module R18n
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
# Pluralization by named variable
|
54
|
+
# Pluralization by named variable `%{count}`.
|
55
55
|
R18n::Filters.add('pl', :named_pluralization) do |content, config, param|
|
56
56
|
if param.is_a?(Hash) && param.key?(:count)
|
57
57
|
hash = content.to_hash
|
@@ -34,44 +34,45 @@ module R18n
|
|
34
34
|
class Rails
|
35
35
|
include ::R18n::YamlMethods
|
36
36
|
|
37
|
-
# Create new loader for some
|
38
|
-
#
|
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
|
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
|
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
|
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
64
|
@translations =
|
64
|
-
|
65
|
+
@backend.send(:translations).map do |locale, values|
|
65
66
|
[R18n::Locale.sanitize_code(locale), transform(values)]
|
66
|
-
end
|
67
|
+
end.to_h
|
67
68
|
end
|
68
69
|
|
69
|
-
# Return hash for object and
|
70
|
+
# Return hash for object and `I18n.load_path`.
|
70
71
|
def hash
|
71
72
|
::I18n.load_path.hash
|
72
73
|
end
|
73
74
|
|
74
|
-
# Is another
|
75
|
+
# Is another `loader` is also load Rails translations.
|
75
76
|
def ==(other)
|
76
77
|
self.class == other.class
|
77
78
|
end
|
@@ -83,15 +84,13 @@ module R18n
|
|
83
84
|
if value.is_a? Hash
|
84
85
|
if value.empty?
|
85
86
|
value
|
86
|
-
elsif value.keys.inject(true) { |a, i| a && RailsPlural.
|
87
|
+
elsif value.keys.inject(true) { |a, i| a && RailsPlural.rails?(i) }
|
87
88
|
Typed.new(
|
88
89
|
'pl',
|
89
|
-
|
90
|
-
[RailsPlural.to_r18n(k), transform(v)]
|
91
|
-
end
|
90
|
+
value.map { |k, v| [RailsPlural.to_r18n(k), transform(v)] }.to_h
|
92
91
|
)
|
93
92
|
else
|
94
|
-
|
93
|
+
value.map { |k, v| [k.to_s, transform(v)] }.to_h
|
95
94
|
end
|
96
95
|
elsif defined?(@private_type_class) && value.is_a?(@private_type_class)
|
97
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
|
24
|
-
def self.
|
25
|
-
%i[zero one few many other].include?
|
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(
|
30
|
-
{ zero: 0, one: 1, few: 2, many: 'n', other: 'n' }[
|
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(
|
35
|
-
{ 0 => :zero, 1 => :one, 2 => :few, 'n' => :other }[
|
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
|
data/r18n-rails-api.gemspec
CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
|
26
26
|
s.author = 'Andrey Sitnik'
|
27
27
|
s.email = 'andrey@sitnik.ru'
|
28
|
-
s.homepage = 'https://github.com/
|
28
|
+
s.homepage = 'https://github.com/r18n/r18n/tree/master/r18n-rails-api'
|
29
29
|
s.license = 'LGPL-3.0'
|
30
30
|
|
31
31
|
s.add_dependency 'i18n', '~> 1'
|
data/spec/backend_spec.rb
CHANGED
@@ -55,17 +55,14 @@ describe R18n::Backend do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it 'raises error on no translation' do
|
58
|
-
expect(
|
59
|
-
|
60
|
-
}).to raise_error(::I18n::MissingTranslationData)
|
58
|
+
expect { I18n.backend.translate(:en, :missed) }
|
59
|
+
.to raise_error(::I18n::MissingTranslationData)
|
61
60
|
|
62
|
-
expect(
|
63
|
-
I18n.t(:missed)
|
64
|
-
}).to raise_error(::I18n::MissingTranslationData)
|
61
|
+
expect { I18n.t(:missed) }.to raise_error(::I18n::MissingTranslationData)
|
65
62
|
end
|
66
63
|
|
67
64
|
it 'reloads translations' do
|
68
|
-
expect
|
65
|
+
expect { I18n.t(:other) }.to raise_error(::I18n::MissingTranslationData)
|
69
66
|
I18n.load_path << OTHER
|
70
67
|
I18n.reload!
|
71
68
|
expect(I18n.t(:other)).to eq 'Other'
|
@@ -76,18 +73,19 @@ describe R18n::Backend do
|
|
76
73
|
expect(I18n.t('in.another').class).to eq Hash
|
77
74
|
end
|
78
75
|
|
76
|
+
## https://github.com/rubocop-hq/rubocop/issues/7436#issuecomment-578766498
|
77
|
+
# rubocop:disable Style/FormatStringToken
|
79
78
|
it 'returns correct unpluralized hash' do
|
80
79
|
expect(I18n.t('users')).to eq(one: '1 user', other: '%{count} users')
|
81
80
|
end
|
81
|
+
# rubocop:enable Style/FormatStringToken
|
82
82
|
|
83
83
|
it 'corrects detect untranslated, whem path is deeper than string' do
|
84
|
-
expect(
|
85
|
-
|
86
|
-
}).to raise_error(::I18n::MissingTranslationData)
|
84
|
+
expect { I18n.t('in.another.level.deeper') }
|
85
|
+
.to raise_error(::I18n::MissingTranslationData)
|
87
86
|
|
88
|
-
expect(
|
89
|
-
|
90
|
-
}).to raise_error(::I18n::MissingTranslationData)
|
87
|
+
expect { I18n.t('in.another.level.go.deeper') }
|
88
|
+
.to raise_error(::I18n::MissingTranslationData)
|
91
89
|
end
|
92
90
|
|
93
91
|
it "doesn't call String methods" do
|
@@ -95,9 +93,8 @@ describe R18n::Backend do
|
|
95
93
|
end
|
96
94
|
|
97
95
|
it "doesn't call object methods" do
|
98
|
-
expect(
|
99
|
-
|
100
|
-
}).to raise_error(::I18n::MissingTranslationData)
|
96
|
+
expect { I18n.t('in.another.level.to_sym') }
|
97
|
+
.to raise_error(::I18n::MissingTranslationData)
|
101
98
|
end
|
102
99
|
|
103
100
|
it 'works deeper pluralization' do
|
data/spec/filters_spec.rb
CHANGED
@@ -8,6 +8,7 @@ describe 'Rails filters' do
|
|
8
8
|
expect(i18n.echo(value: 'Old')).to eq 'Value is Old'
|
9
9
|
end
|
10
10
|
|
11
|
+
# rubocop:disable Style/FormatStringToken
|
11
12
|
it 'pluralizes by variable %{count}' do
|
12
13
|
i18n = R18n::Translation.new(
|
13
14
|
EN, '', locale: EN, translations: {
|
@@ -24,4 +25,5 @@ describe 'Rails filters' do
|
|
24
25
|
expect(i18n.users(count: 1)).to eq '1 user'
|
25
26
|
expect(i18n.users(count: 5)).to eq '5 users'
|
26
27
|
end
|
28
|
+
# rubocop:enable Style/FormatStringToken
|
27
29
|
end
|
data/spec/loader_spec.rb
CHANGED
@@ -22,9 +22,9 @@ describe R18n::Loader::Rails do
|
|
22
22
|
expect(@loader.load(EN)).to eq(
|
23
23
|
'users' => R18n::Typed.new(
|
24
24
|
'pl',
|
25
|
-
0
|
26
|
-
1
|
27
|
-
2
|
25
|
+
0 => 'Zero',
|
26
|
+
1 => 'One',
|
27
|
+
2 => 'Few',
|
28
28
|
'n' => 'Other'
|
29
29
|
)
|
30
30
|
)
|
@@ -35,9 +35,9 @@ describe R18n::Loader::Rails do
|
|
35
35
|
expect(@loader.load(RU)).to eq(
|
36
36
|
'users' => R18n::Typed.new(
|
37
37
|
'pl',
|
38
|
-
0
|
39
|
-
1
|
40
|
-
2
|
38
|
+
0 => 'Ноль',
|
39
|
+
1 => 'Один',
|
40
|
+
2 => 'Несколько',
|
41
41
|
'n' => 'Много'
|
42
42
|
)
|
43
43
|
)
|
@@ -46,8 +46,8 @@ describe R18n::Loader::Rails do
|
|
46
46
|
it 'reloads translations on load_path changes' do
|
47
47
|
I18n.load_path << OTHER
|
48
48
|
expect(@loader.load(RU)).to eq(
|
49
|
-
'one'
|
50
|
-
'two'
|
49
|
+
'one' => 'Один',
|
50
|
+
'two' => 'Два',
|
51
51
|
'three' => 'Три'
|
52
52
|
)
|
53
53
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: r18n-rails-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Sitnik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 4.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 4.0.0
|
41
41
|
description: |2
|
42
42
|
R18n backend for Rails I18n and R18n filters and loader to support Rails
|
43
43
|
translation format.
|
@@ -75,7 +75,7 @@ files:
|
|
75
75
|
- spec/filters_spec.rb
|
76
76
|
- spec/loader_spec.rb
|
77
77
|
- spec/spec_helper.rb
|
78
|
-
homepage: https://github.com/
|
78
|
+
homepage: https://github.com/r18n/r18n/tree/master/r18n-rails-api
|
79
79
|
licenses:
|
80
80
|
- LGPL-3.0
|
81
81
|
metadata: {}
|
@@ -94,8 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
|
-
|
98
|
-
rubygems_version: 2.7.6
|
97
|
+
rubygems_version: 3.1.2
|
99
98
|
signing_key:
|
100
99
|
specification_version: 4
|
101
100
|
summary: Rails I18n compatibility for R18n
|