mini_i18n 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aaf36c6e857bd3a2b63925d82b9757f6f550c3fc
4
- data.tar.gz: ea8e88fdbe0299a90367eb1b92f92e04b2089ab1
3
+ metadata.gz: ef2a53042e3999db594b7dc77b9e7a90ad6b5f74
4
+ data.tar.gz: 2893c26ab80b3dca7e2605a1d012089b74c4ac32
5
5
  SHA512:
6
- metadata.gz: 68d29e18fb17542eb1801d95c743625b5732bd050123e5e1dd17c0db5fcb076aa4f32aed00f8b40243d0076461675ad8d727d92fc136f09c64f4dbb789c23527
7
- data.tar.gz: db0a08f461d7204aad83e88bebf9ca29935acb9b4434fc228d30b341c7f2ddc9b2cea483a5c15f434a302f1b96a2618a377e60858997e7c034f73a4e0abed238
6
+ metadata.gz: 0151a62d4cdd3dda83766ddc688ef7472b543fc4a88e2c46f7df6750346abdc5dbb24370383cdf01b2cd2644dad153e299f2215b7656b8770505e1bef20474b5
7
+ data.tar.gz: d71c64a9fa707378ea398b9e3fc93a2447596121df8dc9dc1fc53011612896997a43b85db78ca442d7e6122dbaacfa4d0aad783408cff7d35163cf6991f9e26e
@@ -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,6 +49,14 @@ 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
@@ -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,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.8.0"
3
3
  end
@@ -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
@@ -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,14 +1,14 @@
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.8.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: 2018-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,8 +90,10 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - lib/mini_i18n.rb
92
92
  - lib/mini_i18n/localization.rb
93
+ - lib/mini_i18n/pluralization.rb
93
94
  - lib/mini_i18n/utils.rb
94
95
  - lib/mini_i18n/version.rb
96
+ - spec/fixtures/locales/en.json
95
97
  - spec/fixtures/locales/en.yml
96
98
  - spec/fixtures/locales/localization.yml
97
99
  - spec/fixtures/locales/multiple.yml
@@ -110,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
112
  requirements:
111
113
  - - ">="
112
114
  - !ruby/object:Gem::Version
113
- version: '0'
115
+ version: '2.3'
114
116
  required_rubygems_version: !ruby/object:Gem::Requirement
115
117
  requirements:
116
118
  - - ">="
@@ -123,6 +125,7 @@ signing_key:
123
125
  specification_version: 4
124
126
  summary: Minimalistic I18n library for Ruby
125
127
  test_files:
128
+ - spec/fixtures/locales/en.json
126
129
  - spec/fixtures/locales/en.yml
127
130
  - spec/fixtures/locales/localization.yml
128
131
  - spec/fixtures/locales/multiple.yml