singleton-client 0.7.2 → 0.7.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-client/api/source.rb +15 -13
- data/lib/sgtn-client/api/t.rb +4 -1
- data/lib/sgtn-client/api/translation.rb +61 -36
- data/lib/sgtn-client/cldr/core_ext.rb +4 -1
- data/lib/sgtn-client/cldr/localized_date.rb +4 -1
- data/lib/sgtn-client/cldr/localized_datetime.rb +4 -1
- data/lib/sgtn-client/cldr/localized_str.rb +3 -1
- data/lib/sgtn-client/cldr/localized_time.rb +4 -1
- data/lib/sgtn-client/core/cache.rb +47 -36
- data/lib/sgtn-client/core/config.rb +5 -2
- data/lib/sgtn-client/core/exceptions.rb +3 -0
- data/lib/sgtn-client/core/logging.rb +3 -1
- data/lib/sgtn-client/core/request.rb +5 -2
- data/lib/sgtn-client/formatters/plurals/plural_formatter.rb +4 -1
- data/lib/sgtn-client/sgtn-client.rb +9 -2
- data/lib/sgtn-client/util/cache-util.rb +13 -4
- data/lib/sgtn-client/util/file-util.rb +6 -1
- data/lib/sgtn-client/util/locale-util.rb +16 -1
- data/lib/sgtn-client/util/validate-util.rb +4 -3
- data/lib/singleton-ruby.rb +3 -0
- data/lib/version.rb +2 -0
- 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: 662845b2e8dcbe69030049114857506ba0e2d5fd0ab98fdf886ddfd07ed1c7a8
|
4
|
+
data.tar.gz: bafa7b6a9f9d68e735ad1c830f37ea7f770381cafd7ff4ecda142d1673cec9c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3249cfcdce192e2e04670aefb03fd090c9b436171edeb80bec82d1d62816e8406d7aaaa92e3b85aec0d9e625babbab136d61e3e0d4ecf12d582c0fd99ad5a5c5
|
7
|
+
data.tar.gz: c694298fc391a1395ea62b4b3fe7595c5deae8fd1909d01af1b6097a03848583a81e5b0d52ef16339645bc74035f180feae9110cad56f0b67d082a95c72bb3e1
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
1
3
|
|
2
4
|
module SgtnClient
|
3
5
|
|
@@ -6,18 +8,18 @@ module SgtnClient
|
|
6
8
|
class Source
|
7
9
|
|
8
10
|
def self.getSource(component, key, locale)
|
11
|
+
SgtnClient.logger.debug "[Source][getSource]component=#{component}, key=#{key}, locale=#{locale}"
|
9
12
|
cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
|
10
|
-
items = SgtnClient::CacheUtil.get_cache(cache_key)
|
13
|
+
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
11
14
|
if items.nil?
|
12
|
-
items = getBundle(component, locale)
|
13
|
-
SgtnClient.logger.debug "Putting sources items into cache with key: " + cache_key
|
15
|
+
items = getBundle(component, locale)
|
14
16
|
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
15
17
|
else
|
16
|
-
SgtnClient.logger.debug "
|
18
|
+
SgtnClient.logger.debug "[Source][getSource]getting sources from cache with key: " + cache_key
|
17
19
|
end
|
18
|
-
s = items[locale][key]
|
20
|
+
s = (items.nil? || items[locale].nil?)? nil : items[locale][key]
|
19
21
|
if items.nil? || s.nil?
|
20
|
-
SgtnClient.logger.debug "Source not found, return key: " + key
|
22
|
+
SgtnClient.logger.debug "[Source][getSource]source not found, return key: " + key
|
21
23
|
#return key
|
22
24
|
return nil
|
23
25
|
else
|
@@ -26,23 +28,23 @@ module SgtnClient
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def self.getSources(component, locale)
|
31
|
+
SgtnClient.logger.debug "[Source][getSources]component=#{component}, locale=#{locale}"
|
29
32
|
cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
|
30
|
-
items = SgtnClient::CacheUtil.get_cache(cache_key)
|
31
|
-
if items.nil?
|
33
|
+
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
34
|
+
if items.nil? || expired
|
32
35
|
items = getBundle(component, locale)
|
33
|
-
SgtnClient.logger.debug "Putting sources items into cache with key: " + cache_key
|
34
36
|
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
35
37
|
else
|
36
|
-
SgtnClient.logger.debug "
|
38
|
+
SgtnClient.logger.debug "[Source][getSources]getting sources from cache with key: " + cache_key
|
37
39
|
end
|
38
40
|
return items
|
39
41
|
end
|
40
42
|
|
41
43
|
def self.loadBundles(locale)
|
44
|
+
SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
|
42
45
|
env = SgtnClient::Config.default_environment
|
43
46
|
SgtnClient::Config.configurations.default = locale
|
44
47
|
source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
|
45
|
-
SgtnClient.logger.debug "Loading [" + locale + "] source bundles from path: " + source_bundle
|
46
48
|
Dir.foreach(source_bundle) do |component|
|
47
49
|
next if component == '.' || component == '..'
|
48
50
|
yamlfile = File.join(source_bundle, component + "/" + locale + ".yml")
|
@@ -54,10 +56,10 @@ module SgtnClient
|
|
54
56
|
|
55
57
|
private
|
56
58
|
def self.getBundle(component, locale)
|
59
|
+
SgtnClient.logger.debug "[Source][getBundle]component=#{component}, locale=#{locale}"
|
57
60
|
env = SgtnClient::Config.default_environment
|
58
61
|
source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
|
59
62
|
bundlepath = source_bundle + "/" + component + "/" + locale + ".yml"
|
60
|
-
SgtnClient.logger.debug "Getting source from bundle: " + bundlepath
|
61
63
|
begin
|
62
64
|
bundle = SgtnClient::FileUtil.read_yml(bundlepath)
|
63
65
|
rescue => exception
|
@@ -68,4 +70,4 @@ module SgtnClient
|
|
68
70
|
|
69
71
|
end
|
70
72
|
|
71
|
-
end
|
73
|
+
end
|
data/lib/sgtn-client/api/t.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'multi_json'
|
2
5
|
|
3
6
|
module SgtnClient
|
@@ -12,17 +15,26 @@ module SgtnClient
|
|
12
15
|
class Translation
|
13
16
|
|
14
17
|
def self.getString(component, key, locale)
|
18
|
+
SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
|
15
19
|
str = getTranslation(component, key, locale)
|
16
20
|
if str.nil?
|
17
21
|
str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
|
22
|
+
if str.nil?
|
23
|
+
SgtnClient.logger.debug "[Translation][getString] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
|
24
|
+
end
|
18
25
|
end
|
19
26
|
str
|
20
27
|
end
|
21
28
|
|
22
29
|
def self.getString_p(component, key, plural_args, locale)
|
30
|
+
SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
|
23
31
|
str = getTranslation(component, key, locale)
|
24
32
|
if str.nil?
|
25
33
|
str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
|
34
|
+
if str.nil?
|
35
|
+
SgtnClient.logger.debug "[Translation][getString_p] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
|
36
|
+
return nil
|
37
|
+
end
|
26
38
|
str.to_plural_s(:en, plural_args)
|
27
39
|
else
|
28
40
|
str.to_plural_s(locale, plural_args)
|
@@ -30,7 +42,11 @@ module SgtnClient
|
|
30
42
|
end
|
31
43
|
|
32
44
|
def self.getString_f(component, key, args, locale, *optionals)
|
45
|
+
SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
|
33
46
|
s = getString(component, key, locale, *optionals)
|
47
|
+
if s.nil?
|
48
|
+
return nil
|
49
|
+
end
|
34
50
|
if args.is_a?(Hash)
|
35
51
|
args.each do |source, arg|
|
36
52
|
s.gsub! "{#{source}}", arg
|
@@ -42,24 +58,21 @@ module SgtnClient
|
|
42
58
|
end
|
43
59
|
|
44
60
|
def self.getStrings(component, locale)
|
45
|
-
|
46
|
-
|
47
|
-
items =
|
48
|
-
if items.nil?
|
49
|
-
items = getTranslations(component, flocale)
|
50
|
-
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
51
|
-
else
|
52
|
-
SgtnClient.logger.debug "Getting translations from cache with key: " + cache_key
|
53
|
-
end
|
54
|
-
|
61
|
+
SgtnClient.logger.debug "[Translation][getStrings]component=#{component}, locale=#{locale}"
|
62
|
+
locale = SgtnClient::LocaleUtil.get_best_locale(locale)
|
63
|
+
items = get_cs(component, locale)
|
55
64
|
default = SgtnClient::Config.configurations.default
|
56
65
|
if items.nil? || items["messages"] == nil
|
57
66
|
items = {}
|
58
67
|
s = SgtnClient::Source.getSources(component, default)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
68
|
+
if s.nil?
|
69
|
+
SgtnClient.logger.error "[Translation][getStrings] Missing component: #{component}, locale: #{locale}"
|
70
|
+
else
|
71
|
+
default_component, value = s.first
|
72
|
+
items["component"] = component
|
73
|
+
items["messages"] = value
|
74
|
+
items["locale"] = 'source'
|
75
|
+
end
|
63
76
|
end
|
64
77
|
return items
|
65
78
|
end
|
@@ -68,11 +81,28 @@ module SgtnClient
|
|
68
81
|
private
|
69
82
|
|
70
83
|
def self.getTranslation(component, key, locale)
|
71
|
-
|
72
|
-
|
73
|
-
items
|
74
|
-
|
75
|
-
|
84
|
+
locale = SgtnClient::LocaleUtil.get_best_locale(locale)
|
85
|
+
items = get_cs(component, locale)
|
86
|
+
if items.nil? || items["messages"] == nil
|
87
|
+
nil
|
88
|
+
else
|
89
|
+
items["messages"][key]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.get_cs(component, locale)
|
94
|
+
# source locale always return source bundle
|
95
|
+
if locale == LocaleUtil.get_source_locale
|
96
|
+
sources = SgtnClient::Source.getSources(component, SgtnClient::Config.configurations.default)
|
97
|
+
messages = sources&.first&.last
|
98
|
+
return {'locale' => locale, 'component' => component, 'messages' => messages} if messages
|
99
|
+
end
|
100
|
+
|
101
|
+
cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
|
102
|
+
SgtnClient.logger.debug "[Translation][get_cs]cache_key=#{cache_key}"
|
103
|
+
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
104
|
+
if items.nil? || expired
|
105
|
+
items = load(component, locale)
|
76
106
|
if items.nil?
|
77
107
|
items = SgtnClient::Source.getSources(component, SgtnClient::Config.configurations.default)
|
78
108
|
SgtnClient::Core::Cache.put(cache_key, items, 60)
|
@@ -80,43 +110,38 @@ module SgtnClient
|
|
80
110
|
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
81
111
|
end
|
82
112
|
else
|
83
|
-
SgtnClient.logger.debug "
|
84
|
-
end
|
85
|
-
if items.nil? || items["messages"] == nil
|
86
|
-
nil
|
87
|
-
else
|
88
|
-
items["messages"][key]
|
113
|
+
SgtnClient.logger.debug "[Translation]get translations from cache with key: " + cache_key
|
89
114
|
end
|
90
|
-
|
91
|
-
|
92
|
-
|
115
|
+
|
116
|
+
return items
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.load(component, locale)
|
93
120
|
env = SgtnClient::Config.default_environment
|
94
121
|
mode = SgtnClient::Config.configurations[env]["bundle_mode"]
|
122
|
+
SgtnClient.logger.debug "[Translation][load]mode=#{mode}"
|
95
123
|
if mode == 'offline'
|
96
|
-
return
|
124
|
+
return load_o(component, locale)
|
97
125
|
else
|
98
|
-
return
|
126
|
+
return load_s(component, locale)
|
99
127
|
end
|
100
128
|
end
|
101
129
|
|
102
|
-
def self.
|
130
|
+
def self.load_o(component, locale)
|
103
131
|
env = SgtnClient::Config.default_environment
|
104
132
|
product_name = SgtnClient::Config.configurations[env]["product_name"]
|
105
133
|
version = SgtnClient::Config.configurations[env]["version"].to_s
|
106
134
|
translation_bundle = SgtnClient::Config.configurations[env]["translation_bundle"]
|
107
135
|
bundlepath = translation_bundle + "/" + product_name + "/" + version + "/" + component + "/messages_" + locale + ".json"
|
108
|
-
SgtnClient.logger.debug "Getting translations from offline bundle: " + bundlepath
|
109
136
|
SgtnClient::FileUtil.read_json(bundlepath)
|
110
137
|
end
|
111
138
|
|
112
|
-
def self.
|
139
|
+
def self.load_s(component, locale)
|
113
140
|
env = SgtnClient::Config.default_environment
|
114
141
|
product_name = SgtnClient::Config.configurations[env]["product_name"]
|
115
142
|
vip_server = SgtnClient::Config.configurations[env]["vip_server"]
|
116
|
-
SgtnClient.logger.debug "Getting translations from server: " + vip_server
|
117
143
|
version = SgtnClient::Config.configurations[env]["version"].to_s
|
118
144
|
url = vip_server + "/i18n/api/v2/translation/products/" + product_name + "/versions/" + version + "/locales/" + locale + "/components/" + component+ "?checkTranslationStatus=false&machineTranslation=false&pseudo=false"
|
119
|
-
SgtnClient.logger.debug url
|
120
145
|
begin
|
121
146
|
obj = SgtnClient::Core::Request.get(url)
|
122
147
|
rescue => exception
|
@@ -130,4 +155,4 @@ module SgtnClient
|
|
130
155
|
|
131
156
|
end
|
132
157
|
|
133
|
-
end
|
158
|
+
end
|
@@ -1,4 +1,7 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'sgtn-client/cldr/localized_datetime'
|
2
5
|
require 'sgtn-client/cldr/localized_date'
|
3
6
|
require 'sgtn-client/cldr/localized_time'
|
4
|
-
require 'sgtn-client/cldr/localized_str'
|
7
|
+
require 'sgtn-client/cldr/localized_str'
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'date'
|
2
5
|
require 'time'
|
3
6
|
|
@@ -24,4 +27,4 @@ Date.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
|
24
27
|
def l_short_s(locale = TwitterCldr.locale)
|
25
28
|
self.to_datetime().localize(locale).to_date().to_short_s
|
26
29
|
end
|
27
|
-
LOCALIZE
|
30
|
+
LOCALIZE
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'date'
|
2
5
|
require 'time'
|
3
6
|
|
@@ -60,4 +63,4 @@ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
|
60
63
|
tz.display_name_for(self, display_name)
|
61
64
|
end
|
62
65
|
end
|
63
|
-
LOCALIZE
|
66
|
+
LOCALIZE
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
1
3
|
|
2
4
|
String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
3
5
|
def to_plural_s(locale, arg)
|
@@ -8,4 +10,4 @@ String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
|
8
10
|
num_str
|
9
11
|
end
|
10
12
|
end
|
11
|
-
LOCALIZE
|
13
|
+
LOCALIZE
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'date'
|
2
5
|
require 'time'
|
3
6
|
|
@@ -24,4 +27,4 @@ Time.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
|
24
27
|
def l_short_s(locale = TwitterCldr.locale)
|
25
28
|
self.localize(locale).to_short_s
|
26
29
|
end
|
27
|
-
LOCALIZE
|
30
|
+
LOCALIZE
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'date'
|
2
5
|
|
3
6
|
module SgtnClient::Core
|
@@ -5,88 +8,96 @@ module SgtnClient::Core
|
|
5
8
|
Entry = Struct.new(:expiry, :value)
|
6
9
|
|
7
10
|
def self.initialize(disabled=false, opts={})
|
8
|
-
|
11
|
+
@@opts = opts
|
9
12
|
@mutex = Mutex.new
|
10
|
-
SgtnClient.logger.debug "Initialize cache......"
|
11
13
|
if disabled == false
|
12
|
-
|
13
|
-
SgtnClient.logger.debug "Cache is enabled!"
|
14
|
+
@@data = Hash.new
|
15
|
+
SgtnClient.logger.debug "[Cache][initialize]cache is enabled!"
|
14
16
|
else
|
15
|
-
|
17
|
+
@@data = nil
|
18
|
+
SgtnClient.logger.debug "[Cache][initialize]cache is disabled!"
|
16
19
|
end
|
17
20
|
end
|
18
21
|
|
19
22
|
def self.keys
|
20
|
-
if
|
23
|
+
if @@data == nil
|
21
24
|
return nil
|
22
25
|
end
|
23
|
-
SgtnClient.logger.debug "
|
24
|
-
|
26
|
+
SgtnClient.logger.debug "[Cache][keys]get cache keys"
|
27
|
+
@@data.keys
|
25
28
|
end
|
26
29
|
|
27
30
|
def self.get(key)
|
28
|
-
if
|
29
|
-
return nil
|
31
|
+
if @@data == nil
|
32
|
+
return nil, nil
|
30
33
|
end
|
31
|
-
SgtnClient.logger.debug "
|
32
|
-
invalidate
|
33
|
-
$data[key][:value] if has(key)
|
34
|
+
SgtnClient.logger.debug "[Cache][get]get cache for key: " + key
|
35
|
+
invalidate(key)
|
34
36
|
end
|
35
37
|
|
36
38
|
def self.has(key)
|
37
|
-
if
|
39
|
+
if @@data == nil
|
38
40
|
return nil
|
39
41
|
end
|
40
|
-
SgtnClient.logger.debug "
|
41
|
-
|
42
|
+
SgtnClient.logger.debug "[Cache][has]check if the cache has key: #{(@@data.has_key? key)}"
|
43
|
+
@@data.has_key? key
|
42
44
|
end
|
43
45
|
|
44
46
|
def self.put(key, value, ttl=nil)
|
45
47
|
@mutex.synchronize do
|
46
|
-
if
|
48
|
+
if @@data == nil || value == nil
|
47
49
|
return nil
|
48
50
|
end
|
49
|
-
ttl ||=
|
51
|
+
ttl ||= @@opts[:ttl]
|
50
52
|
# hours from new
|
51
|
-
SgtnClient.logger.debug "
|
52
|
-
|
53
|
+
SgtnClient.logger.debug "[Cache][put]put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
|
54
|
+
@@data[key] = Entry.new(Time.now + ttl*60, value)
|
53
55
|
end
|
54
56
|
end
|
55
57
|
|
56
58
|
def self.delete(key)
|
57
59
|
@mutex.synchronize do
|
58
|
-
if
|
60
|
+
if @@data == nil
|
59
61
|
return nil
|
60
62
|
end
|
61
|
-
SgtnClient.logger.debug "
|
62
|
-
|
63
|
+
SgtnClient.logger.debug "[Cache][delete]delete cache for key: " + key
|
64
|
+
@@data.delete key
|
63
65
|
end
|
64
66
|
end
|
65
67
|
|
66
68
|
def self.clear
|
67
69
|
@mutex.synchronize do
|
68
|
-
if
|
70
|
+
if @@data == nil
|
69
71
|
return nil
|
70
72
|
end
|
71
|
-
SgtnClient.logger.debug "
|
72
|
-
|
73
|
+
SgtnClient.logger.debug "[Cache][clear]clear cache!"
|
74
|
+
@@data = Hash.new
|
73
75
|
end
|
74
76
|
end
|
75
77
|
|
76
|
-
def self.invalidate
|
78
|
+
def self.invalidate(key)
|
77
79
|
@mutex.synchronize do
|
78
|
-
if
|
79
|
-
return nil
|
80
|
+
if @@data == nil
|
81
|
+
return nil, nil
|
80
82
|
end
|
81
|
-
SgtnClient.logger.debug "
|
83
|
+
SgtnClient.logger.debug "[Cache][invalidate]invalidate expired cache......"
|
82
84
|
now = Time.now
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
}
|
87
|
-
|
85
|
+
if has(key)
|
86
|
+
v = @@data[key]
|
87
|
+
expired = false
|
88
|
+
SgtnClient.logger.debug "[Cache][invalidate]check cache: key=#{key}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
|
89
|
+
if v[:expiry] < now
|
90
|
+
SgtnClient.logger.debug "[Cache][invalidate]before deleting the cache: data=#{@@data}"
|
91
|
+
@@data.delete(key)
|
92
|
+
SgtnClient.logger.debug "[Cache][invalidate]after deleting the cache: data=#{@@data}"
|
93
|
+
expired = true
|
94
|
+
end
|
95
|
+
return expired, v[:value]
|
96
|
+
else
|
97
|
+
return nil, nil
|
98
|
+
end
|
88
99
|
end
|
89
100
|
end
|
90
101
|
end
|
91
102
|
|
92
|
-
end
|
103
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'erb'
|
2
5
|
require 'yaml'
|
3
6
|
|
@@ -40,7 +43,7 @@ module SgtnClient
|
|
40
43
|
:rest_endpoint, :rest_token_endpoint, :client_id, :client_secret,
|
41
44
|
:openid_endpoint, :openid_redirect_uri, :openid_client_id, :openid_client_secret,
|
42
45
|
:verbose_logging, :product_name, :version, :vip_server, :bundle_mode,
|
43
|
-
:translation_bundle, :source_bundle, :cache_expiry_period, :disable_cache
|
46
|
+
:translation_bundle, :source_bundle, :cache_expiry_period, :disable_cache, :default_language
|
44
47
|
|
45
48
|
|
46
49
|
# Create Config object
|
@@ -163,4 +166,4 @@ module SgtnClient
|
|
163
166
|
end
|
164
167
|
end
|
165
168
|
|
166
|
-
end
|
169
|
+
end
|
@@ -1,10 +1,13 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'rest-client'
|
2
5
|
require 'multi_json'
|
3
6
|
|
4
7
|
module SgtnClient::Core
|
5
8
|
class Request
|
6
9
|
def self.get(url)
|
7
|
-
|
10
|
+
SgtnClient.logger.debug "[Request][get]url=#{url}"
|
8
11
|
res = RestClient::Resource.new(
|
9
12
|
url,
|
10
13
|
:verify_ssl => false
|
@@ -18,4 +21,4 @@ module SgtnClient::Core
|
|
18
21
|
return obj
|
19
22
|
end
|
20
23
|
end
|
21
|
-
end
|
24
|
+
end
|
@@ -1,4 +1,9 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
module SgtnClient
|
5
|
+
LOGFILE_SHIFT_AGE = 4
|
6
|
+
|
2
7
|
module Core
|
3
8
|
autoload :Cache, "sgtn-client/core/cache"
|
4
9
|
end
|
@@ -38,17 +43,18 @@ module SgtnClient
|
|
38
43
|
|
39
44
|
# create log file
|
40
45
|
file = './sgtnclient_d.log'
|
46
|
+
SgtnClient.logger.debug "[Client][load]create log file=#{file}"
|
41
47
|
if args[2] != nil
|
42
48
|
file = args[2]
|
43
49
|
end
|
44
50
|
file = File.open(file, 'a')
|
45
51
|
file.sync = true
|
46
|
-
SgtnClient.logger = Logger.new(file)
|
52
|
+
SgtnClient.logger = Logger.new(file, LOGFILE_SHIFT_AGE)
|
47
53
|
|
48
54
|
# Set log level for sandbox mode
|
49
55
|
env = SgtnClient::Config.default_environment
|
50
56
|
mode = SgtnClient::Config.configurations[env]["mode"]
|
51
|
-
SgtnClient.logger.
|
57
|
+
SgtnClient.logger.debug "[Client][load]set log level, mode=#{mode}"
|
52
58
|
if mode == 'sandbox'
|
53
59
|
SgtnClient.logger.level = Logger::DEBUG
|
54
60
|
else
|
@@ -57,6 +63,7 @@ module SgtnClient
|
|
57
63
|
|
58
64
|
# initialize cache
|
59
65
|
disable_cache = SgtnClient::Config.configurations[env]["disable_cache"]
|
66
|
+
SgtnClient.logger.debug "[Client][load]cache initialize, disable_cache=#{disable_cache}"
|
60
67
|
if disable_cache != nil
|
61
68
|
SgtnClient::Core::Cache.initialize(disable_cache)
|
62
69
|
else
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'erb'
|
2
5
|
require 'yaml'
|
3
6
|
|
@@ -10,21 +13,27 @@ module SgtnClient
|
|
10
13
|
class CacheUtil
|
11
14
|
|
12
15
|
def self.get_cache(cache_key)
|
13
|
-
items = SgtnClient::Core::Cache.get(cache_key)
|
14
|
-
|
16
|
+
expired, items = SgtnClient::Core::Cache.get(cache_key)
|
17
|
+
SgtnClient.logger.debug "[CacheUtil]get cache with key #{cache_key}, expired #{expired}"
|
18
|
+
return expired, items
|
15
19
|
end
|
16
20
|
|
17
21
|
def self.clear_cache()
|
18
22
|
SgtnClient::Core::Cache.clear()
|
23
|
+
SgtnClient.logger.debug "[CacheUtil]clear cache"
|
19
24
|
end
|
20
25
|
|
21
26
|
def self.write_cache(cache_key, items)
|
27
|
+
if items.nil?
|
28
|
+
return nil
|
29
|
+
end
|
22
30
|
env = SgtnClient::Config.default_environment
|
23
31
|
cache_expiry_period = SgtnClient::Config.configurations[env]["cache_expiry_period"]
|
24
32
|
# expired after 24 hours
|
25
33
|
if cache_expiry_period == nil
|
26
34
|
cache_expiry_period = 24*60
|
27
35
|
end
|
36
|
+
SgtnClient.logger.debug "[CacheUtil]write cache with key #{cache_key}, cache_expiry_period #{cache_expiry_period}, itmes #{items}"
|
28
37
|
SgtnClient::Core::Cache.put(cache_key, items, cache_expiry_period)
|
29
38
|
end
|
30
39
|
|
@@ -32,8 +41,8 @@ module SgtnClient
|
|
32
41
|
env = SgtnClient::Config.default_environment
|
33
42
|
product_name = SgtnClient::Config.configurations[env]["product_name"]
|
34
43
|
version = SgtnClient::Config.configurations[env]["version"].to_s
|
35
|
-
|
44
|
+
product_name + "_" + version + "_" + component + "_" + locale
|
36
45
|
end
|
37
46
|
end
|
38
47
|
|
39
|
-
end
|
48
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
require 'erb'
|
2
5
|
require 'yaml'
|
3
6
|
|
@@ -8,6 +11,7 @@ module SgtnClient
|
|
8
11
|
@mutex = Mutex.new
|
9
12
|
|
10
13
|
def self.read_json(bundlepath)
|
14
|
+
SgtnClient.logger.debug "[FileUtil]read json file from: " + bundlepath
|
11
15
|
@mutex.synchronize do
|
12
16
|
data_hash = nil
|
13
17
|
begin
|
@@ -21,6 +25,7 @@ module SgtnClient
|
|
21
25
|
end
|
22
26
|
|
23
27
|
def self.read_yml(file_name)
|
28
|
+
SgtnClient.logger.debug "[FileUtil]read yml file from: " + file_name
|
24
29
|
@mutex.synchronize do
|
25
30
|
erb = ERB.new(File.read(file_name))
|
26
31
|
erb.filename = file_name
|
@@ -29,4 +34,4 @@ module SgtnClient
|
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
|
-
end
|
37
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
1
4
|
module SgtnClient
|
2
5
|
|
3
6
|
DEFAULT_LOCALES = ['en', 'de', 'es', 'fr', 'ko', 'ja', 'zh-Hans', 'zh-Hant']
|
@@ -10,6 +13,13 @@ module SgtnClient
|
|
10
13
|
}
|
11
14
|
|
12
15
|
class LocaleUtil
|
16
|
+
def self.get_best_locale(locale)
|
17
|
+
fallback(process_locale(locale))
|
18
|
+
end
|
19
|
+
def self.process_locale(locale=nil)
|
20
|
+
locale ||= SgtnClient::Config.configurations.default
|
21
|
+
locale.to_s
|
22
|
+
end
|
13
23
|
def self.fallback(locale)
|
14
24
|
found = SgtnClient::DEFAULT_LOCALES.select {|e| e == locale}
|
15
25
|
if !found.empty?
|
@@ -27,5 +37,10 @@ module SgtnClient
|
|
27
37
|
end
|
28
38
|
return locale
|
29
39
|
end
|
40
|
+
def self.get_source_locale
|
41
|
+
env = SgtnClient::Config.default_environment
|
42
|
+
source_locale = SgtnClient::Config.configurations[env]["default_language"]
|
43
|
+
source_locale || 'en'
|
44
|
+
end
|
30
45
|
end
|
31
|
-
end
|
46
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
1
3
|
|
2
4
|
module SgtnClient
|
3
5
|
|
4
6
|
class ValidateUtil
|
5
7
|
|
6
8
|
def self.validate_config()
|
7
|
-
SgtnClient.logger.debug "-----------Start to validate configuration's setting itmes-----------"
|
8
9
|
env = SgtnClient::Config.default_environment
|
10
|
+
SgtnClient.logger.debug "[ValidateUtil][validate_config] env = #{env}"
|
9
11
|
messages = "\n"
|
10
12
|
|
11
13
|
mode = SgtnClient::Config.configurations[env]["mode"]
|
@@ -36,9 +38,8 @@ module SgtnClient
|
|
36
38
|
if messages != "\n"
|
37
39
|
raise SgtnClient::Exceptions::MissingConfig.new(messages)
|
38
40
|
end
|
39
|
-
SgtnClient.logger.debug "-----------End to validate configuration's setting itmes-----------"
|
40
41
|
end
|
41
42
|
|
42
43
|
end
|
43
44
|
|
44
|
-
end
|
45
|
+
end
|
data/lib/singleton-ruby.rb
CHANGED
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: singleton-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.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: 2022-
|
11
|
+
date: 2022-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|