singleton-cldr-rb 0.0.4 → 0.0.5
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 +4 -4
- data/lib/sgtn-cldr/cldr/localized_str.rb +6 -1
- data/lib/sgtn-cldr/formatters/plurals/plural_formatter.rb +36 -0
- data/lib/sgtn-cldr/sgtn-cldr.rb +4 -2
- data/lib/version.rb +1 -1
- data/spec/unit/plural_spec.rb +10 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e286f95c4967909d4db326ac5154172cedd2cce3f1ba88348dfd1fbd9380b05
|
4
|
+
data.tar.gz: 74c2ad9a574505937819db57c63aac57c07b33c62d8919189b086bb056dfa43f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 400aca5fffdef80b9f9632a608c4e1fe77b083110d54338e210c9deb84654933cadbb444dddc5ecb6489aae255caf2a537a1b7aeb7987b6d912034665d37358a
|
7
|
+
data.tar.gz: 1fb8abfa5be0e2c246bbf5dcc43a7734a5c5073b29515082b0192a40d453bed0b2fbbc98b1d3ce98cdfe0ef9580774cf0d287543fbc035fbf66f094f95d728fb
|
@@ -1,6 +1,11 @@
|
|
1
1
|
|
2
2
|
String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
3
3
|
def to_plural_s(locale, arg)
|
4
|
-
|
4
|
+
num_str = SgtnCldr::Formatters::PluralFormatter.new(locale).num_s(self, arg)
|
5
|
+
if num_str.nil? || num_str.empty?
|
6
|
+
self.localize(locale) % arg
|
7
|
+
else
|
8
|
+
num_str
|
9
|
+
end
|
5
10
|
end
|
6
11
|
LOCALIZE
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module SgtnCldr
|
2
|
+
module Formatters
|
3
|
+
class PluralFormatter
|
4
|
+
|
5
|
+
attr_reader :locale
|
6
|
+
|
7
|
+
def initialize(locale = TwitterCldr.locale)
|
8
|
+
@locale = TwitterCldr.convert_locale(locale)
|
9
|
+
end
|
10
|
+
|
11
|
+
def num_s(string, replacements)
|
12
|
+
reg = Regexp.union(
|
13
|
+
/%<(\{.*?\})>/
|
14
|
+
)
|
15
|
+
string.gsub(reg) do |match|
|
16
|
+
count_placeholder, patterns = if $1
|
17
|
+
pluralization_hash = JSON.parse($1)
|
18
|
+
if pluralization_hash.is_a?(Hash) && pluralization_hash.size == 1
|
19
|
+
pluralization_hash.first
|
20
|
+
else
|
21
|
+
raise ArgumentError.new('expected a Hash with a single key')
|
22
|
+
end
|
23
|
+
else
|
24
|
+
raise ArgumentError.new('invalide format')
|
25
|
+
end
|
26
|
+
count = replacements[count_placeholder.to_sym].to_s
|
27
|
+
if patterns.is_a?(Hash)
|
28
|
+
return TwitterCldr::Utils.deep_symbolize_keys(patterns)[count.to_sym]
|
29
|
+
else
|
30
|
+
raise ArgumentError.new('expected patterns to be a Hash')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/sgtn-cldr/sgtn-cldr.rb
CHANGED
@@ -3,12 +3,14 @@ module SgtnCldr
|
|
3
3
|
autoload :Cache, "sgtn-cldr/core/cache"
|
4
4
|
end
|
5
5
|
|
6
|
-
autoload :Translation, "sgtn-cldr/api/translation"
|
7
|
-
autoload :Source, "sgtn-cldr/api/source"
|
8
6
|
autoload :Config, "sgtn-cldr/core/config"
|
9
7
|
autoload :Logging, "sgtn-cldr/core/logging"
|
10
8
|
autoload :Exceptions, "sgtn-cldr/core/exceptions"
|
11
9
|
autoload :ValidateUtil, "sgtn-cldr/util/validate-util"
|
10
|
+
|
11
|
+
module Formatters
|
12
|
+
autoload :PluralFormatter, "sgtn-cldr/formatters/plurals/plural_formatter"
|
13
|
+
end
|
12
14
|
|
13
15
|
class << self
|
14
16
|
def configure(options = {}, &block)
|
data/lib/version.rb
CHANGED
data/spec/unit/plural_spec.rb
CHANGED
@@ -7,13 +7,16 @@ describe SgtnCldr do
|
|
7
7
|
before :each do
|
8
8
|
|
9
9
|
end
|
10
|
-
|
11
|
-
it "
|
12
|
-
str = 'there %<{ "cat_count": { "one": "is one cat", "few": "
|
13
|
-
expect(str.to_plural_s(:
|
14
|
-
expect(str.to_plural_s(:
|
15
|
-
|
10
|
+
|
11
|
+
it "plural" do
|
12
|
+
str = 'there %<{ "cat_count": { "one": "is one cat", "zero":"is no cat", "two": "are two cats", "few": "are some cats", "many": "are many cats", "other": "are %{cat_count} cats" } }> in the room'
|
13
|
+
expect(str.to_plural_s(:cy, { :cat_count => 1 })).to eq 'there is one cat in the room'
|
14
|
+
expect(str.to_plural_s(:cy, { :cat_count => 0 })).to eq 'there is no cat in the room'
|
15
|
+
expect(str.to_plural_s(:cy, { :cat_count => 5 })).to eq 'there are 5 cats in the room'
|
16
|
+
expect(str.to_plural_s(:cy, { :cat_count => 3 })).to eq 'there are some cats in the room'
|
17
|
+
expect(str.to_plural_s(:cy, { :cat_count => 2 })).to eq 'there are two cats in the room'
|
18
|
+
s = '%<{"count": {"0": "no horse", "one": "one horse", "other": "%{count} horses"}}>'
|
19
|
+
expect(s.to_plural_s(:cy, { :count => 0 })).to eq 'no horse'
|
16
20
|
end
|
17
21
|
end
|
18
|
-
|
19
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: singleton-cldr-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- VMware G11n Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -250,6 +250,7 @@ files:
|
|
250
250
|
- lib/sgtn-cldr/core/exceptions.rb
|
251
251
|
- lib/sgtn-cldr/core/logging.rb
|
252
252
|
- lib/sgtn-cldr/core/request.rb
|
253
|
+
- lib/sgtn-cldr/formatters/plurals/plural_formatter.rb
|
253
254
|
- lib/sgtn-cldr/sgtn-cldr.rb
|
254
255
|
- lib/sgtn-cldr/util/cache-util.rb
|
255
256
|
- lib/sgtn-cldr/util/validate-util.rb
|