singleton-client-test 0.7.0.27 → 0.7.0.31
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 +12 -12
- data/lib/sgtn-client/api/translation.rb +28 -10
- data/lib/sgtn-client/core/cache.rb +29 -21
- data/lib/sgtn-client/core/request.rb +1 -1
- data/lib/sgtn-client/sgtn-client.rb +3 -1
- data/lib/sgtn-client/util/cache-util.rb +9 -2
- data/lib/sgtn-client/util/file-util.rb +2 -0
- data/lib/sgtn-client/util/locale-util.rb +4 -0
- data/lib/sgtn-client/util/validate-util.rb +1 -2
- 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: d36c7df9764afccdd5c3e9ca3dac5123bd9feaedd3ada0a38e386836860f8885
|
4
|
+
data.tar.gz: 9cc9248d0b25744fa3168711040e740f9c07124e60a55a9f0b73150a630ca1c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7d9c3d03d862fa00b6e75ad17e3d6ebf6382750bdf41630708136439980ee0ee9cbb07768f19f71f0b23ba2c2f3c2dba5e50e4d9afc136b26aa96f326076d89
|
7
|
+
data.tar.gz: 0c3b4dbe2f24634090f997b3d980aa7b0a85c41485bd553020fd3e41e196947a3f2a0b27a12da3d87439ecaaee7b903542df4fbbbb4b23ee0021d01ceda96eb5
|
@@ -6,18 +6,18 @@ module SgtnClient
|
|
6
6
|
class Source
|
7
7
|
|
8
8
|
def self.getSource(component, key, locale)
|
9
|
+
SgtnClient.logger.debug "[Source][getSource]component=#{component}, key=#{key}, locale=#{locale}"
|
9
10
|
cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
|
10
|
-
items = SgtnClient::CacheUtil.get_cache(cache_key)
|
11
|
+
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
11
12
|
if items.nil?
|
12
|
-
items = getBundle(component, locale)
|
13
|
-
SgtnClient.logger.debug "Putting sources items into cache with key: " + cache_key
|
13
|
+
items = getBundle(component, locale)
|
14
14
|
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
15
15
|
else
|
16
|
-
SgtnClient.logger.debug "
|
16
|
+
SgtnClient.logger.debug "[Source][getSource]getting sources from cache with key: " + cache_key
|
17
17
|
end
|
18
|
-
s = items[locale][key]
|
18
|
+
s = items.nil?? nil : items[locale][key]
|
19
19
|
if items.nil? || s.nil?
|
20
|
-
SgtnClient.logger.debug "Source not found, return key: " + key
|
20
|
+
SgtnClient.logger.debug "[Source][getSource]source not found, return key: " + key
|
21
21
|
#return key
|
22
22
|
return nil
|
23
23
|
else
|
@@ -26,23 +26,23 @@ module SgtnClient
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def self.getSources(component, locale)
|
29
|
+
SgtnClient.logger.debug "[Source][getSources]component=#{component}, locale=#{locale}"
|
29
30
|
cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
|
30
|
-
items = SgtnClient::CacheUtil.get_cache(cache_key)
|
31
|
-
if items.nil?
|
31
|
+
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
32
|
+
if items.nil? || expired
|
32
33
|
items = getBundle(component, locale)
|
33
|
-
SgtnClient.logger.debug "Putting sources items into cache with key: " + cache_key
|
34
34
|
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
35
35
|
else
|
36
|
-
SgtnClient.logger.debug "
|
36
|
+
SgtnClient.logger.debug "[Source][getSources]getting sources from cache with key: " + cache_key
|
37
37
|
end
|
38
38
|
return items
|
39
39
|
end
|
40
40
|
|
41
41
|
def self.loadBundles(locale)
|
42
|
+
SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
|
42
43
|
env = SgtnClient::Config.default_environment
|
43
44
|
SgtnClient::Config.configurations.default = locale
|
44
45
|
source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
|
45
|
-
SgtnClient.logger.debug "Loading [" + locale + "] source bundles from path: " + source_bundle
|
46
46
|
Dir.foreach(source_bundle) do |component|
|
47
47
|
next if component == '.' || component == '..'
|
48
48
|
yamlfile = File.join(source_bundle, component + "/" + locale + ".yml")
|
@@ -54,10 +54,10 @@ module SgtnClient
|
|
54
54
|
|
55
55
|
private
|
56
56
|
def self.getBundle(component, locale)
|
57
|
+
SgtnClient.logger.debug "[Source][getBundle]component=#{component}, locale=#{locale}"
|
57
58
|
env = SgtnClient::Config.default_environment
|
58
59
|
source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
|
59
60
|
bundlepath = source_bundle + "/" + component + "/" + locale + ".yml"
|
60
|
-
SgtnClient.logger.debug "Getting source from bundle: " + bundlepath
|
61
61
|
begin
|
62
62
|
bundle = SgtnClient::FileUtil.read_yml(bundlepath)
|
63
63
|
rescue => exception
|
@@ -12,17 +12,26 @@ module SgtnClient
|
|
12
12
|
class Translation
|
13
13
|
|
14
14
|
def self.getString(component, key, locale)
|
15
|
+
SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
|
15
16
|
str = getTranslation(component, key, locale)
|
16
17
|
if str.nil?
|
17
18
|
str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
|
19
|
+
if str.nil?
|
20
|
+
SgtnClient.logger.debug "[Translation][getString] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
|
21
|
+
end
|
18
22
|
end
|
19
23
|
str
|
20
24
|
end
|
21
25
|
|
22
26
|
def self.getString_p(component, key, plural_args, locale)
|
27
|
+
SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
|
23
28
|
str = getTranslation(component, key, locale)
|
24
29
|
if str.nil?
|
25
30
|
str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
|
31
|
+
if str.nil?
|
32
|
+
SgtnClient.logger.debug "[Translation][getString_p] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
|
33
|
+
return nil
|
34
|
+
end
|
26
35
|
str.to_plural_s(:en, plural_args)
|
27
36
|
else
|
28
37
|
str.to_plural_s(locale, plural_args)
|
@@ -30,7 +39,11 @@ module SgtnClient
|
|
30
39
|
end
|
31
40
|
|
32
41
|
def self.getString_f(component, key, args, locale, *optionals)
|
42
|
+
SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
|
33
43
|
s = getString(component, key, locale, *optionals)
|
44
|
+
if s.nil?
|
45
|
+
return nil
|
46
|
+
end
|
34
47
|
if args.is_a?(Hash)
|
35
48
|
args.each do |source, arg|
|
36
49
|
s.gsub! "{#{source}}", arg
|
@@ -42,15 +55,20 @@ module SgtnClient
|
|
42
55
|
end
|
43
56
|
|
44
57
|
def self.getStrings(component, locale)
|
58
|
+
SgtnClient.logger.debug "[Translation][getStrings]component=#{component}, locale=#{locale}"
|
45
59
|
items = get_cs(component, locale)
|
46
60
|
default = SgtnClient::Config.configurations.default
|
47
61
|
if items.nil? || items["messages"] == nil
|
48
62
|
items = {}
|
49
63
|
s = SgtnClient::Source.getSources(component, default)
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
64
|
+
if s.nil?
|
65
|
+
SgtnClient.logger.error "[Translation][getStrings] Missing component: #{component}, locale: #{locale}"
|
66
|
+
else
|
67
|
+
default_component, value = s.first
|
68
|
+
items["component"] = component
|
69
|
+
items["messages"] = value
|
70
|
+
items["locale"] = 'source'
|
71
|
+
end
|
54
72
|
end
|
55
73
|
return items
|
56
74
|
end
|
@@ -68,10 +86,12 @@ module SgtnClient
|
|
68
86
|
end
|
69
87
|
|
70
88
|
def self.get_cs(component, locale)
|
89
|
+
locale = SgtnClient::LocaleUtil.process_locale(locale)
|
71
90
|
flocale = SgtnClient::LocaleUtil.fallback(locale)
|
72
91
|
cache_key = SgtnClient::CacheUtil.get_cachekey(component, flocale)
|
73
|
-
|
74
|
-
|
92
|
+
SgtnClient.logger.debug "[Translation][get_cs]cache_key=#{cache_key}"
|
93
|
+
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
94
|
+
if items.nil? || expired
|
75
95
|
items = load(component, flocale)
|
76
96
|
if items.nil?
|
77
97
|
items = SgtnClient::Source.getSources(component, SgtnClient::Config.configurations.default)
|
@@ -80,7 +100,7 @@ module SgtnClient
|
|
80
100
|
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
81
101
|
end
|
82
102
|
else
|
83
|
-
SgtnClient.logger.debug "
|
103
|
+
SgtnClient.logger.debug "[Translation]get translations from cache with key: " + cache_key
|
84
104
|
end
|
85
105
|
|
86
106
|
return items
|
@@ -89,6 +109,7 @@ module SgtnClient
|
|
89
109
|
def self.load(component, locale)
|
90
110
|
env = SgtnClient::Config.default_environment
|
91
111
|
mode = SgtnClient::Config.configurations[env]["bundle_mode"]
|
112
|
+
SgtnClient.logger.debug "[Translation][load]mode=#{mode}"
|
92
113
|
if mode == 'offline'
|
93
114
|
return load_o(component, locale)
|
94
115
|
else
|
@@ -102,7 +123,6 @@ module SgtnClient
|
|
102
123
|
version = SgtnClient::Config.configurations[env]["version"].to_s
|
103
124
|
translation_bundle = SgtnClient::Config.configurations[env]["translation_bundle"]
|
104
125
|
bundlepath = translation_bundle + "/" + product_name + "/" + version + "/" + component + "/messages_" + locale + ".json"
|
105
|
-
SgtnClient.logger.debug "Getting translations from offline bundle: " + bundlepath
|
106
126
|
SgtnClient::FileUtil.read_json(bundlepath)
|
107
127
|
end
|
108
128
|
|
@@ -110,10 +130,8 @@ module SgtnClient
|
|
110
130
|
env = SgtnClient::Config.default_environment
|
111
131
|
product_name = SgtnClient::Config.configurations[env]["product_name"]
|
112
132
|
vip_server = SgtnClient::Config.configurations[env]["vip_server"]
|
113
|
-
SgtnClient.logger.debug "Getting translations from server: " + vip_server
|
114
133
|
version = SgtnClient::Config.configurations[env]["version"].to_s
|
115
134
|
url = vip_server + "/i18n/api/v2/translation/products/" + product_name + "/versions/" + version + "/locales/" + locale + "/components/" + component+ "?checkTranslationStatus=false&machineTranslation=false&pseudo=false"
|
116
|
-
SgtnClient.logger.debug url
|
117
135
|
begin
|
118
136
|
obj = SgtnClient::Core::Request.get(url)
|
119
137
|
rescue => exception
|
@@ -7,12 +7,12 @@ module SgtnClient::Core
|
|
7
7
|
def self.initialize(disabled=false, opts={})
|
8
8
|
@@opts = opts
|
9
9
|
@mutex = Mutex.new
|
10
|
-
SgtnClient.logger.debug "Initialize cache......"
|
11
10
|
if disabled == false
|
12
11
|
@@data = Hash.new
|
13
|
-
SgtnClient.logger.debug "Cache is enabled!"
|
12
|
+
SgtnClient.logger.debug "[Cache][initialize]cache is enabled!"
|
14
13
|
else
|
15
|
-
|
14
|
+
@@data = nil
|
15
|
+
SgtnClient.logger.debug "[Cache][initialize]cache is disabled!"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,35 +20,34 @@ module SgtnClient::Core
|
|
20
20
|
if @@data == nil
|
21
21
|
return nil
|
22
22
|
end
|
23
|
-
SgtnClient.logger.debug "
|
23
|
+
SgtnClient.logger.debug "[Cache][keys]get cache keys"
|
24
24
|
@@data.keys
|
25
25
|
end
|
26
26
|
|
27
27
|
def self.get(key)
|
28
28
|
if @@data == nil
|
29
|
-
return nil
|
29
|
+
return nil, nil
|
30
30
|
end
|
31
|
-
SgtnClient.logger.debug "
|
32
|
-
invalidate
|
33
|
-
@@data[key][:value] if has(key)
|
31
|
+
SgtnClient.logger.debug "[Cache][get]get cache for key: " + key
|
32
|
+
invalidate(key)
|
34
33
|
end
|
35
34
|
|
36
35
|
def self.has(key)
|
37
36
|
if @@data == nil
|
38
37
|
return nil
|
39
38
|
end
|
40
|
-
SgtnClient.logger.debug "
|
39
|
+
SgtnClient.logger.debug "[Cache][has]check if the cache has key: #{(@@data.has_key? key)}"
|
41
40
|
@@data.has_key? key
|
42
41
|
end
|
43
42
|
|
44
43
|
def self.put(key, value, ttl=nil)
|
45
44
|
@mutex.synchronize do
|
46
|
-
if @@data == nil
|
45
|
+
if @@data == nil || value == nil
|
47
46
|
return nil
|
48
47
|
end
|
49
48
|
ttl ||= @@opts[:ttl]
|
50
49
|
# hours from new
|
51
|
-
SgtnClient.logger.debug "
|
50
|
+
SgtnClient.logger.debug "[Cache][put]put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
|
52
51
|
@@data[key] = Entry.new(Time.now + ttl*60, value)
|
53
52
|
end
|
54
53
|
end
|
@@ -58,7 +57,7 @@ module SgtnClient::Core
|
|
58
57
|
if @@data == nil
|
59
58
|
return nil
|
60
59
|
end
|
61
|
-
SgtnClient.logger.debug "
|
60
|
+
SgtnClient.logger.debug "[Cache][delete]delete cache for key: " + key
|
62
61
|
@@data.delete key
|
63
62
|
end
|
64
63
|
end
|
@@ -68,23 +67,32 @@ module SgtnClient::Core
|
|
68
67
|
if @@data == nil
|
69
68
|
return nil
|
70
69
|
end
|
71
|
-
SgtnClient.logger.debug "
|
70
|
+
SgtnClient.logger.debug "[Cache][clear]clear cache!"
|
72
71
|
@@data = Hash.new
|
73
72
|
end
|
74
73
|
end
|
75
74
|
|
76
|
-
def self.invalidate
|
75
|
+
def self.invalidate(key)
|
77
76
|
@mutex.synchronize do
|
78
77
|
if @@data == nil
|
79
|
-
return nil
|
78
|
+
return nil, nil
|
80
79
|
end
|
81
|
-
SgtnClient.logger.debug "
|
80
|
+
SgtnClient.logger.debug "[Cache][invalidate]invalidate expired cache......"
|
82
81
|
now = Time.now
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
}
|
87
|
-
|
82
|
+
if has(key)
|
83
|
+
v = @@data[key]
|
84
|
+
expired = false
|
85
|
+
SgtnClient.logger.debug "[Cache][invalidate]check cache: key=#{key}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
|
86
|
+
if v[:expiry] < now
|
87
|
+
SgtnClient.logger.debug "[Cache][invalidate]before deleting the cache: data=#{@@data}"
|
88
|
+
@@data.delete(key)
|
89
|
+
SgtnClient.logger.debug "[Cache][invalidate]after deleting the cache: data=#{@@data}"
|
90
|
+
expired = true
|
91
|
+
end
|
92
|
+
return expired, v[:value]
|
93
|
+
else
|
94
|
+
return nil, nil
|
95
|
+
end
|
88
96
|
end
|
89
97
|
end
|
90
98
|
end
|
@@ -38,6 +38,7 @@ module SgtnClient
|
|
38
38
|
|
39
39
|
# create log file
|
40
40
|
file = './sgtnclient_d.log'
|
41
|
+
SgtnClient.logger.debug "[Client][load]create log file=#{file}"
|
41
42
|
if args[2] != nil
|
42
43
|
file = args[2]
|
43
44
|
end
|
@@ -48,7 +49,7 @@ module SgtnClient
|
|
48
49
|
# Set log level for sandbox mode
|
49
50
|
env = SgtnClient::Config.default_environment
|
50
51
|
mode = SgtnClient::Config.configurations[env]["mode"]
|
51
|
-
SgtnClient.logger.
|
52
|
+
SgtnClient.logger.debug "[Client][load]set log level, mode=#{mode}"
|
52
53
|
if mode == 'sandbox'
|
53
54
|
SgtnClient.logger.level = Logger::DEBUG
|
54
55
|
else
|
@@ -57,6 +58,7 @@ module SgtnClient
|
|
57
58
|
|
58
59
|
# initialize cache
|
59
60
|
disable_cache = SgtnClient::Config.configurations[env]["disable_cache"]
|
61
|
+
SgtnClient.logger.debug "[Client][load]cache initialize, disable_cache=#{disable_cache}"
|
60
62
|
if disable_cache != nil
|
61
63
|
SgtnClient::Core::Cache.initialize(disable_cache)
|
62
64
|
else
|
@@ -10,21 +10,27 @@ module SgtnClient
|
|
10
10
|
class CacheUtil
|
11
11
|
|
12
12
|
def self.get_cache(cache_key)
|
13
|
-
items = SgtnClient::Core::Cache.get(cache_key)
|
14
|
-
|
13
|
+
expired, items = SgtnClient::Core::Cache.get(cache_key)
|
14
|
+
SgtnClient.logger.debug "[CacheUtil]get cache with key #{cache_key}, expired #{expired}"
|
15
|
+
return expired, items
|
15
16
|
end
|
16
17
|
|
17
18
|
def self.clear_cache()
|
18
19
|
SgtnClient::Core::Cache.clear()
|
20
|
+
SgtnClient.logger.debug "[CacheUtil]clear cache"
|
19
21
|
end
|
20
22
|
|
21
23
|
def self.write_cache(cache_key, items)
|
24
|
+
if items.nil?
|
25
|
+
return nil
|
26
|
+
end
|
22
27
|
env = SgtnClient::Config.default_environment
|
23
28
|
cache_expiry_period = SgtnClient::Config.configurations[env]["cache_expiry_period"]
|
24
29
|
# expired after 24 hours
|
25
30
|
if cache_expiry_period == nil
|
26
31
|
cache_expiry_period = 24*60
|
27
32
|
end
|
33
|
+
SgtnClient.logger.debug "[CacheUtil]write cache with key #{cache_key}, cache_expiry_period #{cache_expiry_period}, itmes #{items}"
|
28
34
|
SgtnClient::Core::Cache.put(cache_key, items, cache_expiry_period)
|
29
35
|
end
|
30
36
|
|
@@ -37,6 +43,7 @@ module SgtnClient
|
|
37
43
|
default_l = 'en'
|
38
44
|
end
|
39
45
|
lc = locale == default_l ? SgtnClient::Config.configurations.default: locale
|
46
|
+
SgtnClient.logger.debug "[CacheUtil]get cache key: #{lc}"
|
40
47
|
return product_name + "_" + version + "_" + component + "_" + lc
|
41
48
|
end
|
42
49
|
end
|
@@ -8,6 +8,7 @@ module SgtnClient
|
|
8
8
|
@mutex = Mutex.new
|
9
9
|
|
10
10
|
def self.read_json(bundlepath)
|
11
|
+
SgtnClient.logger.debug "[FileUtil]read json file from: " + bundlepath
|
11
12
|
@mutex.synchronize do
|
12
13
|
data_hash = nil
|
13
14
|
begin
|
@@ -21,6 +22,7 @@ module SgtnClient
|
|
21
22
|
end
|
22
23
|
|
23
24
|
def self.read_yml(file_name)
|
25
|
+
SgtnClient.logger.debug "[FileUtil]read yml file from: " + file_name
|
24
26
|
@mutex.synchronize do
|
25
27
|
erb = ERB.new(File.read(file_name))
|
26
28
|
erb.filename = file_name
|
@@ -10,6 +10,10 @@ module SgtnClient
|
|
10
10
|
}
|
11
11
|
|
12
12
|
class LocaleUtil
|
13
|
+
def self.process_locale(locale=nil)
|
14
|
+
locale ||= SgtnClient::Config.configurations.default
|
15
|
+
locale.to_s
|
16
|
+
end
|
13
17
|
def self.fallback(locale)
|
14
18
|
found = SgtnClient::DEFAULT_LOCALES.select {|e| e == locale}
|
15
19
|
if !found.empty?
|
@@ -4,8 +4,8 @@ module SgtnClient
|
|
4
4
|
class ValidateUtil
|
5
5
|
|
6
6
|
def self.validate_config()
|
7
|
-
SgtnClient.logger.debug "-----------Start to validate configuration's setting itmes-----------"
|
8
7
|
env = SgtnClient::Config.default_environment
|
8
|
+
SgtnClient.logger.debug "[ValidateUtil][validate_config] env = #{env}"
|
9
9
|
messages = "\n"
|
10
10
|
|
11
11
|
mode = SgtnClient::Config.configurations[env]["mode"]
|
@@ -36,7 +36,6 @@ module SgtnClient
|
|
36
36
|
if messages != "\n"
|
37
37
|
raise SgtnClient::Exceptions::MissingConfig.new(messages)
|
38
38
|
end
|
39
|
-
SgtnClient.logger.debug "-----------End to validate configuration's setting itmes-----------"
|
40
39
|
end
|
41
40
|
|
42
41
|
end
|
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.31
|
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-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|