singleton-client-test 0.7.0.37 → 0.7.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sgtn-client/api/source.rb +1 -3
- data/lib/sgtn-client/api/t.rb +2 -0
- data/lib/sgtn-client/api/translation.rb +9 -9
- data/lib/sgtn-client/common.rb +10 -0
- data/lib/sgtn-client/core/cache.rb +2 -2
- data/lib/sgtn-client/core/config.rb +4 -9
- data/lib/sgtn-client/core/logging.rb +1 -0
- data/lib/sgtn-client/i18n_backend.rb +46 -0
- data/lib/sgtn-client/loader/cache.rb +9 -13
- data/lib/sgtn-client/loader/chain_loader.rb +3 -1
- data/lib/sgtn-client/loader/loader_factory.rb +2 -10
- data/lib/sgtn-client/loader/local_translation.rb +2 -9
- data/lib/sgtn-client/loader/server.rb +5 -10
- data/lib/sgtn-client/loader/single_loader.rb +1 -6
- data/lib/sgtn-client/loader/source.rb +5 -10
- data/lib/sgtn-client/loader/source_comparer.rb +0 -5
- data/lib/sgtn-client/loader.rb +18 -0
- data/lib/sgtn-client/sgtn-client.rb +23 -17
- data/lib/sgtn-client/util/cache-util.rb +10 -12
- data/lib/sgtn-client/util/locale-util.rb +3 -5
- data/lib/sgtn-client/util/validate-util.rb +6 -6
- data/lib/singleton-client.rb +6 -6
- data/lib/singleton-ruby.rb +2 -3
- metadata +14 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66531dc1471879c17d807694ff65b64e60bfd35a093d7eb739b728d7e0ae3321
|
4
|
+
data.tar.gz: 200e2f4b3ab09a0cfeefe1131ed61494fbec0a1f63974d36a9ce9379d117d066
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d85cd8e706659abb1ec1a6e1364b5fcddf2b2c14bca9d616e49d0d28a7657984e26752e48b310763e51f536cbaefb56d99585fe09d46a68cc8cc75f1504e784b
|
7
|
+
data.tar.gz: 924846467b0707fb90b95f77a604546822e2e1ae3dbbc79cb3541fdbd1dcc99fd17df0b00762dd5ee2073a08a8f2044cbb260f986ded95e6ca42ce089cf35046
|
@@ -4,12 +4,10 @@
|
|
4
4
|
require 'sgtn-client/loader/source'
|
5
5
|
|
6
6
|
module SgtnClient
|
7
|
-
autoload :CacheUtil, 'sgtn-client/util/cache-util'
|
8
|
-
|
9
7
|
class Source
|
10
8
|
def self.loadBundles(locale)
|
11
9
|
SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
|
12
|
-
|
10
|
+
Config.configurations.default = locale
|
13
11
|
end
|
14
12
|
end
|
15
13
|
end
|
data/lib/sgtn-client/api/t.rb
CHANGED
@@ -43,7 +43,7 @@ module SgtnClient
|
|
43
43
|
def translate(key, component, locale = nil, **kwargs)
|
44
44
|
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}"
|
45
45
|
|
46
|
-
locale = locale.nil? ? self.locale :
|
46
|
+
locale = locale.nil? ? self.locale : LocaleUtil.get_best_locale(locale)
|
47
47
|
|
48
48
|
result = get_bundle(component, locale)&.fetch(key, nil)
|
49
49
|
if result.nil? && !LocaleUtil.is_source_locale(locale)
|
@@ -61,7 +61,7 @@ module SgtnClient
|
|
61
61
|
if kwargs.empty?
|
62
62
|
result
|
63
63
|
else
|
64
|
-
locale = result.locale if result.is_a?(
|
64
|
+
locale = result.locale if result.is_a?(StringUtil)
|
65
65
|
result.localize(locale) % kwargs
|
66
66
|
end
|
67
67
|
end
|
@@ -70,7 +70,7 @@ module SgtnClient
|
|
70
70
|
def get_translations(component, locale = nil)
|
71
71
|
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}"
|
72
72
|
|
73
|
-
locale = locale.nil? ? self.locale :
|
73
|
+
locale = locale.nil? ? self.locale : LocaleUtil.get_best_locale(locale)
|
74
74
|
items = get_bundle(component, locale)
|
75
75
|
if items.nil? && !LocaleUtil.is_source_locale(locale)
|
76
76
|
items = get_bundle(component, LocaleUtil.get_source_locale)
|
@@ -81,11 +81,11 @@ module SgtnClient
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def locale
|
84
|
-
RequestStore.store[:locale] ||=
|
84
|
+
RequestStore.store[:locale] ||= LocaleUtil.get_fallback_locale
|
85
85
|
end
|
86
86
|
|
87
87
|
def locale=(value)
|
88
|
-
RequestStore.store[:locale] =
|
88
|
+
RequestStore.store[:locale] = LocaleUtil.get_best_locale(value)
|
89
89
|
end
|
90
90
|
|
91
91
|
private
|
@@ -99,13 +99,13 @@ module SgtnClient
|
|
99
99
|
end
|
100
100
|
|
101
101
|
def get_bundle!(component, locale)
|
102
|
-
id =
|
103
|
-
bundles =
|
102
|
+
id = Common::BundleID.new(component, locale)
|
103
|
+
bundles = Config.available_bundles
|
104
104
|
unless bundles.nil? || bundles.empty? || bundles.include?(id)
|
105
|
-
raise
|
105
|
+
raise SingletonError, 'bundle is unavailable.'
|
106
106
|
end
|
107
107
|
|
108
|
-
|
108
|
+
Config.loader.get_bundle(component, locale)
|
109
109
|
end
|
110
110
|
end
|
111
111
|
extend Implementation
|
@@ -3,15 +3,10 @@
|
|
3
3
|
|
4
4
|
require 'erb'
|
5
5
|
require 'yaml'
|
6
|
+
require 'set'
|
6
7
|
require 'observer'
|
7
8
|
|
8
9
|
module SgtnClient
|
9
|
-
#include Exceptions
|
10
|
-
|
11
|
-
module TranslationLoader
|
12
|
-
autoload :LoaderFactory, 'sgtn-client/loader/loader_factory'
|
13
|
-
end
|
14
|
-
|
15
10
|
module Configuration
|
16
11
|
|
17
12
|
def config
|
@@ -130,7 +125,7 @@ module SgtnClient
|
|
130
125
|
if configurations[env]
|
131
126
|
@@config_cache[env] ||= new(configurations[env])
|
132
127
|
else
|
133
|
-
raise
|
128
|
+
raise Exceptions::MissingConfig.new("Configuration[#{env}] NotFound")
|
134
129
|
end
|
135
130
|
end
|
136
131
|
|
@@ -162,8 +157,8 @@ module SgtnClient
|
|
162
157
|
|
163
158
|
def loader
|
164
159
|
@loader ||= begin
|
165
|
-
config =
|
166
|
-
|
160
|
+
config = Config.configurations[Config.default_environment]
|
161
|
+
TranslationLoader::LoaderFactory.create(config)
|
167
162
|
end
|
168
163
|
end
|
169
164
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
4
|
+
require 'i18n'
|
5
|
+
|
6
|
+
module SgtnClient # :nodoc:
|
7
|
+
# When integrating Singleton in a client application that is already using [I18n::Backend (https://www.rubydoc.info/github/svenfuchs/i18n/master/I18n/Backend/), it would be useful to have Singleton override the said module in order to minimize necessary changes. Here is a common usage:
|
8
|
+
#
|
9
|
+
# I18n::Backend::Simple.include(I18n::Backend::Fallbacks) # add fallbacks behavior to current backend
|
10
|
+
# I18n.backend = I18n::Backend::Chain.new(Sgtn::I18nBackend.new(component_name), I18n.backend)
|
11
|
+
# I18n.enforce_available_locales=false # disable available locales check
|
12
|
+
# I18n.default_locale = :en
|
13
|
+
class I18nBackend
|
14
|
+
def initialize(component)
|
15
|
+
@component = component
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialized?
|
19
|
+
@initialized = true
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_translations(*) end
|
23
|
+
|
24
|
+
def store_translations(*) end
|
25
|
+
|
26
|
+
def available_locales
|
27
|
+
SgtnClient::Config.available_locales.to_a
|
28
|
+
end
|
29
|
+
|
30
|
+
def reload!; end
|
31
|
+
|
32
|
+
def eager_load!; end
|
33
|
+
|
34
|
+
def translations; end
|
35
|
+
|
36
|
+
def exists?(locale, key)
|
37
|
+
!!(translate(locale, key) { nil })
|
38
|
+
end
|
39
|
+
|
40
|
+
def translate(locale, key, options)
|
41
|
+
flat_key = I18n::Backend::Flatten.normalize_flat_keys(locale, key, options[:scope], '.')
|
42
|
+
values = options.except(*I18n::RESERVED_KEYS)
|
43
|
+
SgtnClient::Translation.translate(flat_key, @component, locale, **values) { nil }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -4,20 +4,16 @@
|
|
4
4
|
# SPDX-License-Identifier: EPL-2.0
|
5
5
|
|
6
6
|
module SgtnClient
|
7
|
-
autoload :CacheUtil, 'sgtn-client/util/cache-util'
|
8
|
-
|
9
7
|
module TranslationLoader
|
10
|
-
autoload :CONSTS, 'sgtn-client/loader/consts'
|
11
|
-
|
12
8
|
module Cache # :nodoc:
|
13
9
|
# get from cache, return expired data immediately
|
14
10
|
def get_bundle(component, locale)
|
15
11
|
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
|
16
12
|
|
17
|
-
key =
|
18
|
-
cache_item =
|
13
|
+
key = CacheUtil.get_cachekey(component, locale)
|
14
|
+
cache_item = CacheUtil.get_cache(key)
|
19
15
|
if cache_item
|
20
|
-
if
|
16
|
+
if CacheUtil.is_expired(cache_item)
|
21
17
|
Thread.new do # TODO: Use one thread # refresh in background
|
22
18
|
begin
|
23
19
|
load_bundle(component, locale)
|
@@ -37,22 +33,22 @@ module SgtnClient
|
|
37
33
|
def load_bundle(component, locale)
|
38
34
|
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
|
39
35
|
|
40
|
-
key =
|
36
|
+
key = CacheUtil.get_cachekey(component, locale)
|
41
37
|
item = super
|
42
|
-
|
38
|
+
CacheUtil.write_cache(key, item) if item
|
43
39
|
item
|
44
40
|
end
|
45
41
|
|
46
42
|
def available_bundles
|
47
43
|
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}]"
|
48
44
|
|
49
|
-
cache_item =
|
45
|
+
cache_item = CacheUtil.get_cache(CONSTS::AVAILABLE_BUNDLES_KEY)
|
50
46
|
if cache_item
|
51
|
-
if
|
47
|
+
if CacheUtil.is_expired(cache_item)
|
52
48
|
Thread.new do # TODO: Use one thread
|
53
49
|
begin
|
54
50
|
item = super
|
55
|
-
|
51
|
+
CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
|
56
52
|
rescue StandardError => e
|
57
53
|
SgtnClient.logger.error 'an error occured while loading available bundles.'
|
58
54
|
SgtnClient.logger.error e
|
@@ -63,7 +59,7 @@ module SgtnClient
|
|
63
59
|
end
|
64
60
|
|
65
61
|
item = super
|
66
|
-
|
62
|
+
CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
|
67
63
|
item
|
68
64
|
end
|
69
65
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# Copyright 2022 VMware, Inc.
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
+
require 'set'
|
5
|
+
|
4
6
|
module SgtnClient
|
5
7
|
module TranslationLoader
|
6
8
|
class Chain
|
@@ -23,7 +25,7 @@ module SgtnClient
|
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
26
|
-
raise exception ||
|
28
|
+
raise exception || SingletonError.new("can't load component: #{component}, locale: #{locale}")
|
27
29
|
end
|
28
30
|
|
29
31
|
def available_bundles
|
@@ -3,14 +3,6 @@
|
|
3
3
|
|
4
4
|
module SgtnClient
|
5
5
|
module TranslationLoader
|
6
|
-
autoload :Source, 'sgtn-client/loader/source'
|
7
|
-
autoload :SgtnServer, 'sgtn-client/loader/server'
|
8
|
-
autoload :LocalTranslation, 'sgtn-client/loader/local_translation'
|
9
|
-
autoload :Chain, 'sgtn-client/loader/chain_loader'
|
10
|
-
autoload :SourceComparer, 'sgtn-client/loader/source_comparer'
|
11
|
-
autoload :SingleLoader, 'sgtn-client/loader/single_loader'
|
12
|
-
autoload :Cache, 'sgtn-client/loader/cache'
|
13
|
-
|
14
6
|
module LoaderFactory
|
15
7
|
def self.create(config)
|
16
8
|
SgtnClient.logger.info "[#{method(__callee__).owner}.#{__callee__}] config=#{config}"
|
@@ -19,10 +11,10 @@ module SgtnClient
|
|
19
11
|
loaders << Source.new(config) if config['source_bundle']
|
20
12
|
loaders << SgtnServer.new(config) if config['vip_server']
|
21
13
|
loaders << LocalTranslation.new(config) if config['translation_bundle']
|
22
|
-
raise
|
14
|
+
raise SingletonError, 'no translation is available!' if loaders.empty?
|
23
15
|
|
24
16
|
chain_loader = Class.new(Chain)
|
25
|
-
chain_loader.include SourceComparer
|
17
|
+
chain_loader.include SourceComparer
|
26
18
|
chain_loader.include SingleLoader
|
27
19
|
chain_loader.include Cache
|
28
20
|
|
@@ -3,15 +3,10 @@
|
|
3
3
|
|
4
4
|
require 'json'
|
5
5
|
require 'pathname'
|
6
|
+
require 'set'
|
6
7
|
|
7
8
|
module SgtnClient
|
8
|
-
module Common
|
9
|
-
autoload :BundleID, 'sgtn-client/common/data'
|
10
|
-
end
|
11
|
-
|
12
9
|
module TranslationLoader
|
13
|
-
autoload :CONSTS, 'sgtn-client/loader/consts'
|
14
|
-
|
15
10
|
class LocalTranslation
|
16
11
|
BUNDLE_PREFIX = 'messages_'.freeze
|
17
12
|
BUNDLE_SUFFIX = '.json'.freeze
|
@@ -21,8 +16,6 @@ module SgtnClient
|
|
21
16
|
end
|
22
17
|
|
23
18
|
def load_bundle(component, locale)
|
24
|
-
return if locale == CONSTS::REAL_SOURCE_LOCALE # return when querying source
|
25
|
-
|
26
19
|
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component=#{component}, locale=#{locale}"
|
27
20
|
|
28
21
|
file_name = BUNDLE_PREFIX + locale + BUNDLE_SUFFIX
|
@@ -31,7 +24,7 @@ module SgtnClient
|
|
31
24
|
bundle_data = JSON.parse(File.read(file_path))
|
32
25
|
messages = bundle_data['messages']
|
33
26
|
|
34
|
-
raise
|
27
|
+
raise SingletonError, "no messages in local bundle file: #{file_path}." unless messages
|
35
28
|
|
36
29
|
messages
|
37
30
|
end
|
@@ -5,15 +5,10 @@
|
|
5
5
|
|
6
6
|
require 'faraday'
|
7
7
|
require 'faraday_middleware'
|
8
|
+
require 'set'
|
8
9
|
|
9
10
|
module SgtnClient
|
10
|
-
module Common
|
11
|
-
autoload :BundleID, 'sgtn-client/common/data'
|
12
|
-
end
|
13
|
-
|
14
11
|
module TranslationLoader
|
15
|
-
autoload :CONSTS, 'sgtn-client/loader/consts'
|
16
|
-
|
17
12
|
class SgtnServer
|
18
13
|
ERROR_ILLEGAL_DATA = 'server returned illegal data.'
|
19
14
|
ERROR_BUSINESS_ERROR = 'server returned business error.'
|
@@ -23,7 +18,7 @@ module SgtnClient
|
|
23
18
|
def initialize(config)
|
24
19
|
@server_url = config['vip_server']
|
25
20
|
|
26
|
-
product_root = format('
|
21
|
+
product_root = format('i18n/api/v2/translation/products/%s/versions/%s', config['product_name'], config['version'])
|
27
22
|
|
28
23
|
@bundle_url = "#{product_root}/locales/%s/components/%s"
|
29
24
|
@locales_url = "#{product_root}/localelist"
|
@@ -73,7 +68,7 @@ module SgtnClient
|
|
73
68
|
|
74
69
|
def extract_data(parsedbody, path_to_data)
|
75
70
|
data = parsedbody.dig('data', *path_to_data)
|
76
|
-
raise
|
71
|
+
raise SingletonError, "no expected data in response. Body is: #{parsedbody}" unless data
|
77
72
|
|
78
73
|
data
|
79
74
|
end
|
@@ -81,13 +76,13 @@ module SgtnClient
|
|
81
76
|
def process_business_error(parsedbody)
|
82
77
|
b_code = parsedbody.dig('response', 'code')
|
83
78
|
unless b_code >= 200 && b_code < 300 || b_code >= 600 && b_code < 700
|
84
|
-
raise
|
79
|
+
raise SingletonError, "#{ERROR_BUSINESS_ERROR} #{parsedbody['response']}"
|
85
80
|
end
|
86
81
|
|
87
82
|
# 600 means a successful response, 6xx means partial successful.
|
88
83
|
SgtnClient.logger.warn "#{ERROR_BUSINESS_ERROR} #{parsedbody['response']}" if b_code > 600
|
89
84
|
rescue TypeError, ArgumentError, NoMethodError => e
|
90
|
-
raise
|
85
|
+
raise SingletonError, "#{ERROR_ILLEGAL_DATA} #{e}. Body is: #{parsedbody}"
|
91
86
|
end
|
92
87
|
end
|
93
88
|
end
|
@@ -2,12 +2,7 @@
|
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
4
|
module SgtnClient
|
5
|
-
autoload :SingleOperation, 'sgtn-client/common/single_operation'
|
6
|
-
autoload :CacheUtil, 'sgtn-client/util/cache-util'
|
7
|
-
|
8
5
|
module TranslationLoader
|
9
|
-
autoload :CONSTS, 'sgtn-client/loader/consts'
|
10
|
-
|
11
6
|
module SingleLoader
|
12
7
|
def load_bundle(component, locale)
|
13
8
|
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
|
@@ -41,7 +36,7 @@ module SgtnClient
|
|
41
36
|
end
|
42
37
|
end
|
43
38
|
|
44
|
-
loader =
|
39
|
+
loader = SingleOperation.new(none_alive, &creator)
|
45
40
|
end
|
46
41
|
end
|
47
42
|
end
|
@@ -2,16 +2,11 @@
|
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
4
|
require 'pathname'
|
5
|
-
require '
|
5
|
+
require 'set'
|
6
|
+
require 'yaml'
|
6
7
|
|
7
8
|
module SgtnClient
|
8
|
-
module Common
|
9
|
-
autoload :BundleID, 'sgtn-client/common/data'
|
10
|
-
end
|
11
|
-
|
12
9
|
module TranslationLoader
|
13
|
-
autoload :CONSTS, 'sgtn-client/loader/consts'
|
14
|
-
|
15
10
|
class Source
|
16
11
|
def initialize(config)
|
17
12
|
@source_bundle_path = Pathname.new(config['source_bundle'])
|
@@ -25,7 +20,7 @@ module SgtnClient
|
|
25
20
|
total_messages = {}
|
26
21
|
|
27
22
|
(@source_bundle_path + component).glob('**/*.{yml, yaml}') do |f|
|
28
|
-
bundle =
|
23
|
+
bundle = YAML.load(File.read(f))
|
29
24
|
messages = bundle&.first&.last # TODO: Warn about inconsistent source locale
|
30
25
|
if messages.is_a?(Hash)
|
31
26
|
total_messages.merge!(messages)
|
@@ -34,7 +29,7 @@ module SgtnClient
|
|
34
29
|
end
|
35
30
|
end
|
36
31
|
|
37
|
-
raise
|
32
|
+
raise SingletonError, "no local source messages for component #{component}" if total_messages.empty?
|
38
33
|
|
39
34
|
total_messages
|
40
35
|
end
|
@@ -45,7 +40,7 @@ module SgtnClient
|
|
45
40
|
@available_bundles ||= begin
|
46
41
|
@source_bundle_path.children.select(&:directory?).reduce(Set.new) do |bundles, component|
|
47
42
|
component.glob('**/*.{yml, yaml}') do |_|
|
48
|
-
bundles << Common::BundleID.new(component.basename.to_s,
|
43
|
+
bundles << Common::BundleID.new(component.basename.to_s, LocaleUtil.get_source_locale)
|
49
44
|
break bundles
|
50
45
|
end || bundles
|
51
46
|
end
|
@@ -2,12 +2,7 @@
|
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
4
|
module SgtnClient
|
5
|
-
autoload :StringUtil, 'sgtn-client/util/string-util'
|
6
|
-
autoload :LocaleUtil, 'sgtn-client/util/locale-util'
|
7
|
-
|
8
5
|
module TranslationLoader
|
9
|
-
autoload :CONSTS, 'sgtn-client/loader/consts'
|
10
|
-
|
11
6
|
module SourceComparer
|
12
7
|
def load_bundle(component, locale)
|
13
8
|
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2022 VMware, Inc.
|
4
|
+
# SPDX-License-Identifier: EPL-2.0
|
5
|
+
|
6
|
+
module SgtnClient
|
7
|
+
module TranslationLoader # :nodoc:
|
8
|
+
autoload :Cache, 'sgtn-client/loader/cache'
|
9
|
+
autoload :Chain, 'sgtn-client/loader/chain_loader'
|
10
|
+
autoload :CONSTS, 'sgtn-client/loader/consts'
|
11
|
+
autoload :LoaderFactory, 'sgtn-client/loader/loader_factory'
|
12
|
+
autoload :LocalTranslation, 'sgtn-client/loader/local_translation'
|
13
|
+
autoload :SgtnServer, 'sgtn-client/loader/server'
|
14
|
+
autoload :SingleLoader, 'sgtn-client/loader/single_loader'
|
15
|
+
autoload :SourceComparer, 'sgtn-client/loader/source_comparer'
|
16
|
+
autoload :Source, 'sgtn-client/loader/source'
|
17
|
+
end
|
18
|
+
end
|
@@ -4,6 +4,10 @@
|
|
4
4
|
module SgtnClient
|
5
5
|
LOGFILE_SHIFT_AGE = 4
|
6
6
|
|
7
|
+
autoload :Common, 'sgtn-client/common'
|
8
|
+
autoload :TranslationLoader, 'sgtn-client/loader'
|
9
|
+
autoload :SingleOperation, 'sgtn-client/common/single_operation'
|
10
|
+
|
7
11
|
module Core
|
8
12
|
autoload :Cache, "sgtn-client/core/cache"
|
9
13
|
end
|
@@ -16,8 +20,10 @@ module SgtnClient
|
|
16
20
|
autoload :Exceptions, "sgtn-client/core/exceptions"
|
17
21
|
autoload :ValidateUtil, "sgtn-client/util/validate-util"
|
18
22
|
autoload :LocaleUtil, "sgtn-client/util/locale-util"
|
19
|
-
autoload :FileUtil, "sgtn-client/util/file-util"
|
20
23
|
autoload :CacheUtil, "sgtn-client/util/cache-util"
|
24
|
+
autoload :StringUtil, "sgtn-client/util/string-util"
|
25
|
+
autoload :SingletonError, 'sgtn-client/exceptions'
|
26
|
+
autoload :I18nBackend, "sgtn-client/i18n_backend"
|
21
27
|
|
22
28
|
module Formatters
|
23
29
|
autoload :PluralFormatter, "sgtn-client/formatters/plurals/plural_formatter"
|
@@ -26,15 +32,15 @@ module SgtnClient
|
|
26
32
|
|
27
33
|
class << self
|
28
34
|
def configure(options = {}, &block)
|
29
|
-
|
35
|
+
Config.configure(options, &block)
|
30
36
|
end
|
31
37
|
|
32
38
|
include Logging
|
33
39
|
def load(*args)
|
34
40
|
# load configuration file
|
35
41
|
begin
|
36
|
-
|
37
|
-
|
42
|
+
Config.load(args[0], args[1])
|
43
|
+
ValidateUtil.validate_config()
|
38
44
|
rescue => exception
|
39
45
|
file = File.open('./error.log', 'a')
|
40
46
|
file.sync = true
|
@@ -44,40 +50,40 @@ module SgtnClient
|
|
44
50
|
|
45
51
|
# create log file
|
46
52
|
file = './sgtnclient_d.log'
|
47
|
-
|
53
|
+
logger.debug "[Client][load]create log file=#{file}"
|
48
54
|
if args[2] != nil
|
49
55
|
file = args[2]
|
50
56
|
end
|
51
57
|
file = File.open(file, 'a')
|
52
58
|
file.sync = true
|
53
|
-
|
59
|
+
logger = Logger.new(file, LOGFILE_SHIFT_AGE)
|
54
60
|
|
55
61
|
# Set log level for sandbox mode
|
56
|
-
env =
|
57
|
-
mode =
|
58
|
-
|
62
|
+
env = Config.default_environment
|
63
|
+
mode = Config.configurations[env]["mode"]
|
64
|
+
logger.debug "[Client][load]set log level, mode=#{mode}"
|
59
65
|
if mode == 'sandbox'
|
60
|
-
|
66
|
+
logger.level = Logger::DEBUG
|
61
67
|
else
|
62
|
-
|
68
|
+
logger.level = Logger::INFO
|
63
69
|
end
|
64
70
|
|
65
71
|
# initialize cache
|
66
|
-
disable_cache =
|
67
|
-
|
72
|
+
disable_cache = Config.configurations[env]["disable_cache"]
|
73
|
+
logger.debug "[Client][load]cache initialize, disable_cache=#{disable_cache}"
|
68
74
|
if disable_cache != nil
|
69
|
-
|
75
|
+
Core::Cache.initialize(disable_cache)
|
70
76
|
else
|
71
|
-
|
77
|
+
Core::Cache.initialize()
|
72
78
|
end
|
73
79
|
end
|
74
80
|
|
75
81
|
def logger
|
76
|
-
|
82
|
+
Config.logger
|
77
83
|
end
|
78
84
|
|
79
85
|
def logger=(log)
|
80
|
-
|
86
|
+
Config.logger = log
|
81
87
|
end
|
82
88
|
end
|
83
89
|
|
@@ -1,34 +1,32 @@
|
|
1
1
|
# Copyright 2022 VMware, Inc.
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
-
|
5
|
-
module Core
|
6
|
-
autoload :Cache, 'sgtn-client/core/cache'
|
7
|
-
end
|
4
|
+
require 'time'
|
8
5
|
|
6
|
+
module SgtnClient
|
9
7
|
class CacheUtil
|
10
8
|
def self.get_cache(cache_key)
|
11
|
-
|
9
|
+
Core::Cache.get(cache_key)
|
12
10
|
end
|
13
11
|
|
14
12
|
def self.clear_cache
|
15
|
-
|
13
|
+
Core::Cache.clear
|
16
14
|
end
|
17
15
|
|
18
16
|
def self.write_cache(cache_key, items)
|
19
17
|
return nil if items.nil? || items.empty?
|
20
18
|
|
21
|
-
env =
|
22
|
-
cache_expiry_period =
|
19
|
+
env = Config.default_environment
|
20
|
+
cache_expiry_period = Config.configurations[env]['cache_expiry_period']
|
23
21
|
# expired after 24 hours
|
24
22
|
cache_expiry_period = 24 * 60 if cache_expiry_period.nil?
|
25
|
-
|
23
|
+
Core::Cache.put(cache_key, items, cache_expiry_period)
|
26
24
|
end
|
27
25
|
|
28
26
|
def self.get_cachekey(component, locale)
|
29
|
-
env =
|
30
|
-
product_name =
|
31
|
-
version =
|
27
|
+
env = Config.default_environment
|
28
|
+
product_name = Config.configurations[env]['product_name']
|
29
|
+
version = Config.configurations[env]['version'].to_s
|
32
30
|
product_name + '_' + version + '_' + component + '_' + locale
|
33
31
|
end
|
34
32
|
|
@@ -3,8 +3,6 @@
|
|
3
3
|
# Copyright 2022 VMware, Inc.
|
4
4
|
# SPDX-License-Identifier: EPL-2.0
|
5
5
|
|
6
|
-
require 'set'
|
7
|
-
|
8
6
|
module SgtnClient
|
9
7
|
class LocaleUtil
|
10
8
|
MAP_LOCALES = {
|
@@ -45,8 +43,8 @@ module SgtnClient
|
|
45
43
|
end
|
46
44
|
|
47
45
|
def self.get_default_locale
|
48
|
-
env =
|
49
|
-
|
46
|
+
env = Config.default_environment
|
47
|
+
Config.configurations[env]['default_language']
|
50
48
|
end
|
51
49
|
|
52
50
|
def self.get_fallback_locale
|
@@ -63,7 +61,7 @@ module SgtnClient
|
|
63
61
|
@lowercase_locales_map = nil if type == :available_locales
|
64
62
|
end
|
65
63
|
|
66
|
-
|
64
|
+
Config.add_observer(self, :reset_available_locales)
|
67
65
|
|
68
66
|
private_class_method :get_best_match, :lowercase_locales_map, :reset_available_locales
|
69
67
|
end
|
@@ -6,32 +6,32 @@ module SgtnClient
|
|
6
6
|
class ValidateUtil
|
7
7
|
|
8
8
|
def self.validate_config()
|
9
|
-
env =
|
9
|
+
env = Config.default_environment
|
10
10
|
SgtnClient.logger.debug "[ValidateUtil][validate_config] env = #{env}"
|
11
11
|
messages = "\n"
|
12
12
|
|
13
|
-
mode =
|
13
|
+
mode = Config.configurations[env]["mode"]
|
14
14
|
if mode != 'sandbox' && mode != 'live'
|
15
15
|
messages = messages + "Configuration[mode] has to be 'sandbox' or 'live'!\n"
|
16
16
|
end
|
17
17
|
|
18
|
-
#version =
|
18
|
+
#version = Config.configurations[env]["version"]
|
19
19
|
#if version.is_a? Integer
|
20
20
|
#messages = messages + "Configuration[version] has to be standard as '#.#.#, e.g '1.0.0'!\n"
|
21
21
|
#end
|
22
22
|
|
23
|
-
cache_expiry_period =
|
23
|
+
cache_expiry_period = Config.configurations[env]["cache_expiry_period"]
|
24
24
|
if cache_expiry_period != nil && (cache_expiry_period.is_a? Integer) == false
|
25
25
|
messages = messages + "Configuration[cache_expiry_period] has to be a number!\n"
|
26
26
|
end
|
27
27
|
|
28
|
-
disable_cache =
|
28
|
+
disable_cache = Config.configurations[env]["disable_cache"]
|
29
29
|
if disable_cache != nil && disable_cache != false && disable_cache != true
|
30
30
|
messages = messages + "Configuration[disable_cache] has to be a 'true' or 'false'!\n"
|
31
31
|
end
|
32
32
|
|
33
33
|
if messages != "\n"
|
34
|
-
raise
|
34
|
+
raise Exceptions::MissingConfig.new(messages)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
data/lib/singleton-client.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Copyright 2022 VMware, Inc.
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
+
require 'forwardable'
|
4
5
|
require_relative 'singleton-ruby'
|
5
6
|
|
6
7
|
module Sgtn # :nodoc:
|
7
|
-
|
8
|
-
|
9
|
-
SgtnClient
|
8
|
+
class << self
|
9
|
+
extend Forwardable
|
10
|
+
def_delegator SgtnClient, :load, :load_config
|
11
|
+
def_delegators SgtnClient::Translation, :translate, :t, :get_translations, :locale, :locale=
|
10
12
|
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
private_class_method :getString, :getString_p, :getString_f, :getStrings
|
14
|
+
I18nBackend = SgtnClient::I18nBackend
|
15
15
|
end
|
data/lib/singleton-ruby.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Copyright 2022 VMware, Inc.
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
-
require "sgtn-client/sgtn-client"
|
5
|
-
require 'sgtn-client/cldr/core_ext'
|
6
4
|
require 'twitter_cldr'
|
5
|
+
require 'sgtn-client/cldr/core_ext'
|
7
6
|
|
8
|
-
require 'sgtn-client/
|
7
|
+
require 'sgtn-client/sgtn-client'
|
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.
|
4
|
+
version: 0.7.7.1
|
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-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|
@@ -48,16 +48,16 @@ dependencies:
|
|
48
48
|
name: rubocop
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0
|
53
|
+
version: '0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - "
|
58
|
+
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '0
|
60
|
+
version: '0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: webmock
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,19 +143,19 @@ dependencies:
|
|
143
143
|
- !ruby/object:Gem::Version
|
144
144
|
version: '6.6'
|
145
145
|
- !ruby/object:Gem::Dependency
|
146
|
-
name:
|
146
|
+
name: i18n
|
147
147
|
requirement: !ruby/object:Gem::Requirement
|
148
148
|
requirements:
|
149
|
-
- - "
|
149
|
+
- - ">="
|
150
150
|
- !ruby/object:Gem::Version
|
151
|
-
version: '
|
151
|
+
version: '0'
|
152
152
|
type: :runtime
|
153
153
|
prerelease: false
|
154
154
|
version_requirements: !ruby/object:Gem::Requirement
|
155
155
|
requirements:
|
156
|
-
- - "
|
156
|
+
- - ">="
|
157
157
|
- !ruby/object:Gem::Version
|
158
|
-
version: '
|
158
|
+
version: '0'
|
159
159
|
description: Singleton Ruby client
|
160
160
|
email: g11n-vip-project@vmware.com
|
161
161
|
executables: []
|
@@ -172,6 +172,7 @@ files:
|
|
172
172
|
- lib/sgtn-client/cldr/localized_datetime.rb
|
173
173
|
- lib/sgtn-client/cldr/localized_str.rb
|
174
174
|
- lib/sgtn-client/cldr/localized_time.rb
|
175
|
+
- lib/sgtn-client/common.rb
|
175
176
|
- lib/sgtn-client/common/data.rb
|
176
177
|
- lib/sgtn-client/common/single_operation.rb
|
177
178
|
- lib/sgtn-client/core/cache.rb
|
@@ -180,6 +181,8 @@ files:
|
|
180
181
|
- lib/sgtn-client/core/logging.rb
|
181
182
|
- lib/sgtn-client/exceptions.rb
|
182
183
|
- lib/sgtn-client/formatters/plurals/plural_formatter.rb
|
184
|
+
- lib/sgtn-client/i18n_backend.rb
|
185
|
+
- lib/sgtn-client/loader.rb
|
183
186
|
- lib/sgtn-client/loader/cache.rb
|
184
187
|
- lib/sgtn-client/loader/chain_loader.rb
|
185
188
|
- lib/sgtn-client/loader/consts.rb
|