mini_i18n 0.7.0 → 0.9.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: aaf36c6e857bd3a2b63925d82b9757f6f550c3fc
4
- data.tar.gz: ea8e88fdbe0299a90367eb1b92f92e04b2089ab1
2
+ SHA256:
3
+ metadata.gz: c9f1764e408c5192196c7630a410f22139768cbfb72a315e662e0c66911eec89
4
+ data.tar.gz: 4410a333ad7565dcf86f6c85186b6e557343a17d2f3b4b71ef727ed2f891f048
5
5
  SHA512:
6
- metadata.gz: 68d29e18fb17542eb1801d95c743625b5732bd050123e5e1dd17c0db5fcb076aa4f32aed00f8b40243d0076461675ad8d727d92fc136f09c64f4dbb789c23527
7
- data.tar.gz: db0a08f461d7204aad83e88bebf9ca29935acb9b4434fc228d30b341c7f2ddc9b2cea483a5c15f434a302f1b96a2618a377e60858997e7c034f73a4e0abed238
6
+ metadata.gz: 685f079c4337705b59026bf88beb330c88d662ec111c1af8ea5a029638d143c47c2521975c9d8fa8e1db6796ddf7e0a9b6a0d15693a9ae1077cde466e3cc69c5
7
+ data.tar.gz: cc0357f872d83ab2cd0523d8c3a8f7c265e59f30a4b82d97afeb1ad71f6b85eaf7f73c02154f42edb327904883ee710eede3ddb3e78b549f963728a59edb425b
@@ -0,0 +1,23 @@
1
+ module MiniI18n
2
+ module Pluralization
3
+ def self.pluralize(mappings, count, locale = MiniI18n.locale)
4
+ rule = MiniI18n.pluralization_rules.fetch(locale.to_sym, default_rule)
5
+ mappings[rule.call(count)]
6
+ end
7
+
8
+ private
9
+
10
+ def self.default_rule
11
+ -> (n) {
12
+ case n
13
+ when 0
14
+ 'zero'
15
+ when 1
16
+ 'one'
17
+ else
18
+ 'other'
19
+ end
20
+ }
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module MiniI18n
2
- VERSION = "0.7.0"
2
+ VERSION = "0.9.0"
3
3
  end
data/lib/mini_i18n.rb CHANGED
@@ -2,6 +2,7 @@ require "yaml"
2
2
  require "mini_i18n/version"
3
3
  require "mini_i18n/utils"
4
4
  require "mini_i18n/localization"
5
+ require "mini_i18n/pluralization"
5
6
 
6
7
  module MiniI18n
7
8
  class << self
@@ -48,12 +49,20 @@ module MiniI18n
48
49
  @@separator = new_separator || DEFAULT_SEPARATOR
49
50
  end
50
51
 
52
+ def pluralization_rules
53
+ @@pluralization_rules ||= {}
54
+ end
55
+
56
+ def pluralization_rules=(new_rules)
57
+ @@pluralization_rules = new_rules
58
+ end
59
+
51
60
  def configure
52
61
  yield(self) if block_given?
53
62
  end
54
63
 
55
64
  def load_translations(path)
56
- Dir[path.to_s].each do |file|
65
+ Dir[path.to_s].sort.each do |file|
57
66
  YAML.load_file(file).each do |locale, new_translations|
58
67
  add_translations(locale.to_s, new_translations)
59
68
  end
@@ -62,6 +71,7 @@ module MiniI18n
62
71
 
63
72
  def translate(key, options = {})
64
73
  return if key.empty? || translations.empty?
74
+
65
75
  return multiple_translate(key, options) if key.is_a?(Array)
66
76
  return multiple_locales(key, options) if options[:locale].is_a?(Array)
67
77
 
@@ -76,7 +86,7 @@ module MiniI18n
76
86
  result = lookup(*keys)
77
87
 
78
88
  result = with_fallbacks(result, keys)
79
- result = with_pluralization(result, options)
89
+ result = with_pluralization(result, options, _locale)
80
90
  result = with_interpolation(result, options)
81
91
 
82
92
  result || options[:default]
@@ -123,18 +133,11 @@ module MiniI18n
123
133
  result
124
134
  end
125
135
 
126
- def with_pluralization(result, options)
136
+ def with_pluralization(result, options, locale)
127
137
  count = options[:count]
128
138
 
129
139
  if count && result.is_a?(Hash)
130
- case count
131
- when 0
132
- result = result["zero"]
133
- when 1
134
- result = result["one"]
135
- else
136
- result = result["many"]
137
- end
140
+ result = Pluralization.pluralize(result, count, locale)
138
141
  end
139
142
 
140
143
  result
@@ -0,0 +1,5 @@
1
+ {
2
+ "en": {
3
+ "from_json": "from JSON"
4
+ }
5
+ }
@@ -7,4 +7,4 @@ en:
7
7
  notifications:
8
8
  zero: 'no unread notifications'
9
9
  one: '1 unread notification'
10
- many: '%{count} unread notifications'
10
+ other: '%{count} unread notifications'
@@ -13,6 +13,9 @@ es:
13
13
  number:
14
14
  as:
15
15
  currency: '%{number} €'
16
+ notifications:
17
+ zero: 'no hay nuevas notificaciones'
18
+ other: 'tienes notificaciones por leer'
16
19
 
17
20
  fr:
18
21
  hello: "bonjour"
@@ -6,6 +6,10 @@ RSpec.describe MiniI18n do
6
6
  expect(MiniI18n.translations["en"]).to include 'bye'
7
7
  end
8
8
 
9
+ it "allows to load translations from JSON" do
10
+ expect(MiniI18n.t(:from_json)).to eq 'from JSON'
11
+ end
12
+
9
13
  it "does not raise if path is nil" do
10
14
  expect(MiniI18n.load_translations(nil)).to eq []
11
15
  end
@@ -42,8 +46,8 @@ RSpec.describe MiniI18n do
42
46
  MiniI18n.separator = ' '
43
47
  expect(MiniI18n.t('second_level hello')).to eq 'hello 2'
44
48
 
45
- MiniI18n.separator = '::'
46
- expect(MiniI18n.t('second_level::hello')).to eq 'hello 2'
49
+ MiniI18n.separator = '/'
50
+ expect(MiniI18n.t('second_level/hello')).to eq 'hello 2'
47
51
  end
48
52
  end
49
53
 
@@ -101,5 +105,16 @@ RSpec.describe MiniI18n do
101
105
  expect(MiniI18n.t('notifications', count: 1)).to eq '1 unread notification'
102
106
  expect(MiniI18n.t('notifications', count: 5)).to eq '5 unread notifications'
103
107
  end
108
+
109
+ it "pluralization with custom rules" do
110
+ MiniI18n.pluralization_rules = {
111
+ es: -> (n) {
112
+ n == 0 ? 'zero' : 'other'
113
+ }
114
+ }
115
+
116
+ expect(MiniI18n.t('notifications', count: 0, locale: :es)).to eq 'no hay nuevas notificaciones'
117
+ expect(MiniI18n.t('notifications', count: 1, locale: :es)).to eq 'tienes notificaciones por leer'
118
+ end
104
119
  end
105
120
  end
metadata CHANGED
@@ -1,59 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - markets
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-22 00:00:00.000000000 Z
11
+ date: 2022-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
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: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.7'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '3.7'
55
- - !ruby/object:Gem::Dependency
56
- name: byebug
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - ">="
@@ -90,8 +76,10 @@ extra_rdoc_files: []
90
76
  files:
91
77
  - lib/mini_i18n.rb
92
78
  - lib/mini_i18n/localization.rb
79
+ - lib/mini_i18n/pluralization.rb
93
80
  - lib/mini_i18n/utils.rb
94
81
  - lib/mini_i18n/version.rb
82
+ - spec/fixtures/locales/en.json
95
83
  - spec/fixtures/locales/en.yml
96
84
  - spec/fixtures/locales/localization.yml
97
85
  - spec/fixtures/locales/multiple.yml
@@ -110,19 +98,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
98
  requirements:
111
99
  - - ">="
112
100
  - !ruby/object:Gem::Version
113
- version: '0'
101
+ version: '2.3'
114
102
  required_rubygems_version: !ruby/object:Gem::Requirement
115
103
  requirements:
116
104
  - - ">="
117
105
  - !ruby/object:Gem::Version
118
106
  version: '0'
119
107
  requirements: []
120
- rubyforge_project:
121
- rubygems_version: 2.6.13
108
+ rubygems_version: 3.1.4
122
109
  signing_key:
123
110
  specification_version: 4
124
111
  summary: Minimalistic I18n library for Ruby
125
112
  test_files:
113
+ - spec/fixtures/locales/en.json
126
114
  - spec/fixtures/locales/en.yml
127
115
  - spec/fixtures/locales/localization.yml
128
116
  - spec/fixtures/locales/multiple.yml