singleton-client-test 0.7.0.35 → 0.7.0.38
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -13
- data/lib/sgtn-client/api/translation.rb +4 -4
- data/lib/sgtn-client/cldr/localized_str.rb +1 -0
- data/lib/sgtn-client/core/config.rb +4 -1
- data/lib/sgtn-client/loader/loader_factory.rb +1 -1
- data/lib/sgtn-client/loader/local_translation.rb +1 -2
- data/lib/sgtn-client/loader/single_loader.rb +11 -9
- data/lib/sgtn-client/loader/source.rb +4 -1
- data/lib/sgtn-client/sgtn-client.rb +1 -0
- data/lib/sgtn-client/util/locale-util.rb +29 -11
- data/lib/singleton-client.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e419a8a647ed41eb47557a85011cf889a694878be67956d76be674c1203773e4
|
4
|
+
data.tar.gz: 50cb15bfe02c29cd7d5b22b86d0ac54efce58b63cbfa4fee2843aefa6f3d738c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 253990e2ab87782fb19abb9b13079a8f857027d747b7911f2d5ef2367d5f0e188903c5d52b9623642c2932ef72cb62b461ef59e49c590493f0c521b39fe1547f
|
7
|
+
data.tar.gz: 414dc10cdd0a5fc96ef9d14bf7effc1d63221426d1cdf21c0a70387576348589dcc476fff44f739aa88fcdd9f8e44668ff19dc3269f6a4da7377387c0916bcde
|
data/README.md
CHANGED
@@ -14,40 +14,40 @@ Basic Usage:
|
|
14
14
|
```ruby
|
15
15
|
require 'singleton-client'
|
16
16
|
|
17
|
-
|
18
|
-
result =
|
17
|
+
Sgtn.load_config(file, env)
|
18
|
+
result = Sgtn.translate(key, component, locale)
|
19
19
|
```
|
20
20
|
## API Usage
|
21
21
|
|
22
22
|
### Get a string's translation
|
23
|
-
`result =
|
23
|
+
`result = Sgtn.translate(key, component, locale)`
|
24
24
|
|
25
25
|
### Get a string's translation with default value when no translation
|
26
|
-
`result =
|
26
|
+
`result = Sgtn.translate(key, component, locale) { 'default value' }`
|
27
27
|
|
28
28
|
### Get a string's translation and format it with placeholders
|
29
|
-
`result =
|
29
|
+
`result = Sgtn.translate(key, component, locale, **args)`
|
30
30
|
|
31
31
|
### Get pluralized translation
|
32
|
-
`result =
|
32
|
+
`result = Sgtn.translate(key, component, locale, **args)`
|
33
33
|
|
34
34
|
### Get translations of a bundle
|
35
|
-
`result =
|
35
|
+
`result = Sgtn.get_translations(component, locale)`
|
36
36
|
|
37
37
|
### Set locale for a request
|
38
|
-
`
|
38
|
+
`Sgtn.locale = 'en'`
|
39
39
|
|
40
40
|
### Get locale of the request
|
41
|
-
`result =
|
41
|
+
`result = Sgtn.locale`
|
42
42
|
|
43
43
|
### Get a string's translation with locale set
|
44
44
|
```ruby
|
45
|
-
|
46
|
-
result =
|
45
|
+
Sgtn.locale = 'en'
|
46
|
+
result = Sgtn.translate(key, component)
|
47
47
|
```
|
48
48
|
|
49
49
|
### Get translations of a bundle with locale set
|
50
50
|
```ruby
|
51
|
-
|
52
|
-
result =
|
51
|
+
Sgtn.locale = 'en'
|
52
|
+
result = Sgtn.get_translations(component)
|
53
53
|
```
|
@@ -6,19 +6,19 @@ require 'request_store'
|
|
6
6
|
module SgtnClient
|
7
7
|
module Translation
|
8
8
|
module Implementation
|
9
|
-
# <b>DEPRECATED:</b> Please use <tt>
|
9
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
10
10
|
def getString(component, key, locale)
|
11
11
|
SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
|
12
12
|
translate(key, component, locale) { nil }
|
13
13
|
end
|
14
14
|
|
15
|
-
# <b>DEPRECATED:</b> Please use <tt>
|
15
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
16
16
|
def getString_p(component, key, plural_args, locale)
|
17
17
|
SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
|
18
18
|
translate(key, component, locale, **plural_args) { nil }
|
19
19
|
end
|
20
20
|
|
21
|
-
# <b>DEPRECATED:</b> Please use <tt>
|
21
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
22
22
|
def getString_f(component, key, args, locale, *_optionals)
|
23
23
|
SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
|
24
24
|
s = translate(key, component, locale) { nil }
|
@@ -34,7 +34,7 @@ module SgtnClient
|
|
34
34
|
s
|
35
35
|
end
|
36
36
|
|
37
|
-
# <b>DEPRECATED:</b> Please use <tt>
|
37
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:get_translations</tt> instead.
|
38
38
|
def getStrings(component, locale)
|
39
39
|
get_translations(component, locale)
|
40
40
|
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
4
|
String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
5
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
5
6
|
def to_plural_s(locale, arg)
|
6
7
|
num_str = SgtnClient::Formatters::PluralFormatter.new(locale).num_s(self, arg)
|
7
8
|
if num_str.nil? || num_str.empty?
|
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
require 'erb'
|
5
5
|
require 'yaml'
|
6
|
-
|
6
|
+
require 'observer'
|
7
7
|
|
8
8
|
module SgtnClient
|
9
9
|
#include Exceptions
|
@@ -40,6 +40,7 @@ module SgtnClient
|
|
40
40
|
|
41
41
|
|
42
42
|
class Config
|
43
|
+
extend Observable
|
43
44
|
|
44
45
|
attr_accessor :username, :password, :signature, :app_id, :cert_path,
|
45
46
|
:token, :token_secret, :subject,
|
@@ -182,6 +183,8 @@ module SgtnClient
|
|
182
183
|
def bundles.locales
|
183
184
|
@locales ||= reduce(Set.new) { |locales, id| locales << id.locale }
|
184
185
|
end
|
186
|
+
changed
|
187
|
+
notify_observers(:available_locales)
|
185
188
|
end
|
186
189
|
bundles.locales
|
187
190
|
end
|
@@ -22,7 +22,7 @@ module SgtnClient
|
|
22
22
|
raise SgtnClient::SingletonError, 'no translation is available!' if loaders.empty?
|
23
23
|
|
24
24
|
chain_loader = Class.new(Chain)
|
25
|
-
chain_loader.include SourceComparer
|
25
|
+
chain_loader.include SourceComparer
|
26
26
|
chain_loader.include SingleLoader
|
27
27
|
chain_loader.include Cache
|
28
28
|
|
@@ -2,6 +2,7 @@
|
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
4
|
require 'json'
|
5
|
+
require 'pathname'
|
5
6
|
|
6
7
|
module SgtnClient
|
7
8
|
module Common
|
@@ -20,8 +21,6 @@ module SgtnClient
|
|
20
21
|
end
|
21
22
|
|
22
23
|
def load_bundle(component, locale)
|
23
|
-
return if locale == CONSTS::REAL_SOURCE_LOCALE # return when querying source
|
24
|
-
|
25
24
|
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component=#{component}, locale=#{locale}"
|
26
25
|
|
27
26
|
file_name = BUNDLE_PREFIX + locale + BUNDLE_SUFFIX
|
@@ -12,12 +12,9 @@ module SgtnClient
|
|
12
12
|
def load_bundle(component, locale)
|
13
13
|
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
|
14
14
|
|
15
|
-
@single_bundle_loader ||= single_loader { |c,l| super(c,l) }
|
15
|
+
@single_bundle_loader ||= single_loader { |c, l| super(c, l) }
|
16
16
|
id = CacheUtil.get_cachekey(component, locale)
|
17
17
|
@single_bundle_loader.operate(id, component, locale)&.value
|
18
|
-
ensure
|
19
|
-
# delete thread from hash after finish
|
20
|
-
@single_bundle_loader.remove_object(id)
|
21
18
|
end
|
22
19
|
|
23
20
|
def available_bundles
|
@@ -25,21 +22,26 @@ module SgtnClient
|
|
25
22
|
|
26
23
|
@single_available_bundles_loader ||= single_loader { super }
|
27
24
|
@single_available_bundles_loader.operate(CONSTS::AVAILABLE_BUNDLES_KEY)&.value
|
28
|
-
ensure
|
29
|
-
@single_available_bundles_loader.remove_object(CONSTS::AVAILABLE_BUNDLES_KEY)
|
30
25
|
end
|
31
26
|
|
32
27
|
private
|
28
|
+
|
33
29
|
def single_loader(&block)
|
34
|
-
|
30
|
+
loader = nil
|
31
|
+
none_alive = proc { |_, thread| thread.nil? }
|
35
32
|
creator = proc do |id, _, *args|
|
36
33
|
Thread.new do
|
37
34
|
SgtnClient.logger.debug "start single loading #{id}"
|
38
|
-
|
35
|
+
begin
|
36
|
+
block.call(*args)
|
37
|
+
ensure
|
38
|
+
# delete thread from hash after finish
|
39
|
+
loader.remove_object(id)
|
40
|
+
end
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
42
|
-
SgtnClient::SingleOperation.new(none_alive, &creator)
|
44
|
+
loader = SgtnClient::SingleOperation.new(none_alive, &creator)
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -1,6 +1,9 @@
|
|
1
1
|
# Copyright 2022 VMware, Inc.
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
+
require 'pathname'
|
5
|
+
require 'yaml'
|
6
|
+
|
4
7
|
module SgtnClient
|
5
8
|
module Common
|
6
9
|
autoload :BundleID, 'sgtn-client/common/data'
|
@@ -22,7 +25,7 @@ module SgtnClient
|
|
22
25
|
total_messages = {}
|
23
26
|
|
24
27
|
(@source_bundle_path + component).glob('**/*.{yml, yaml}') do |f|
|
25
|
-
bundle = YAML.
|
28
|
+
bundle = YAML.load(File.read(f))
|
26
29
|
messages = bundle&.first&.last # TODO: Warn about inconsistent source locale
|
27
30
|
if messages.is_a?(Hash)
|
28
31
|
total_messages.merge!(messages)
|
@@ -18,6 +18,7 @@ module SgtnClient
|
|
18
18
|
autoload :LocaleUtil, "sgtn-client/util/locale-util"
|
19
19
|
autoload :FileUtil, "sgtn-client/util/file-util"
|
20
20
|
autoload :CacheUtil, "sgtn-client/util/cache-util"
|
21
|
+
autoload :StringUtil, "sgtn-client/util/string-util"
|
21
22
|
|
22
23
|
module Formatters
|
23
24
|
autoload :PluralFormatter, "sgtn-client/formatters/plurals/plural_formatter"
|
@@ -1,23 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright 2022 VMware, Inc.
|
2
4
|
# SPDX-License-Identifier: EPL-2.0
|
5
|
+
|
3
6
|
require 'set'
|
4
7
|
|
5
8
|
module SgtnClient
|
6
9
|
class LocaleUtil
|
7
10
|
MAP_LOCALES = {
|
8
|
-
'zh-
|
9
|
-
'zh-
|
10
|
-
'zh-
|
11
|
-
'zh-
|
11
|
+
'zh-cn' => 'zh-hans',
|
12
|
+
'zh-tw' => 'zh-hant',
|
13
|
+
'zh-hans-cn' => 'zh-hans',
|
14
|
+
'zh-hant-tw' => 'zh-hant'
|
12
15
|
}.freeze
|
16
|
+
LOCALE_SEPARATOR = '-'
|
13
17
|
|
14
18
|
def self.get_best_locale(locale)
|
19
|
+
return locale if Config.available_locales.include?(locale)
|
20
|
+
|
15
21
|
return get_fallback_locale if locale.nil?
|
16
22
|
|
17
23
|
locale = locale.to_s
|
18
24
|
return get_fallback_locale if locale.empty?
|
19
25
|
|
20
|
-
get_best_match(locale.gsub('_',
|
26
|
+
get_best_match(locale.gsub('_', LOCALE_SEPARATOR).downcase)
|
21
27
|
end
|
22
28
|
|
23
29
|
def self.is_source_locale(locale = nil)
|
@@ -26,12 +32,12 @@ module SgtnClient
|
|
26
32
|
|
27
33
|
def self.get_best_match(locale)
|
28
34
|
locale = MAP_LOCALES[locale] || locale
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
return get_fallback_locale if index.nil?
|
35
|
+
lowercase_locales_map[locale] or begin
|
36
|
+
index = locale.rindex(LOCALE_SEPARATOR)
|
37
|
+
return get_fallback_locale if index.nil?
|
33
38
|
|
34
|
-
|
39
|
+
get_best_match(locale[0...index])
|
40
|
+
end
|
35
41
|
end
|
36
42
|
|
37
43
|
def self.get_source_locale
|
@@ -47,6 +53,18 @@ module SgtnClient
|
|
47
53
|
@fallback_locale ||= get_default_locale || get_source_locale || 'en'
|
48
54
|
end
|
49
55
|
|
50
|
-
|
56
|
+
def self.lowercase_locales_map
|
57
|
+
@lowercase_locales_map ||= Config.available_locales.each_with_object({}) do |locale, memo|
|
58
|
+
memo[locale.to_s.downcase] = locale
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.reset_available_locales(type)
|
63
|
+
@lowercase_locales_map = nil if type == :available_locales
|
64
|
+
end
|
65
|
+
|
66
|
+
SgtnClient::Config.add_observer(self, :reset_available_locales)
|
67
|
+
|
68
|
+
private_class_method :get_best_match, :lowercase_locales_map, :reset_available_locales
|
51
69
|
end
|
52
70
|
end
|
data/lib/singleton-client.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: singleton-client-test
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.0.
|
4
|
+
version: 0.7.0.38
|
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: 2022-
|
11
|
+
date: 2022-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|