singleton-client-test 0.7.7.1 → 0.7.7.4
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 +0 -1
- data/lib/sgtn-client/api/translation.rb +41 -29
- data/lib/sgtn-client/common/data.rb +1 -1
- data/lib/sgtn-client/common/single_operation.rb +7 -8
- data/lib/sgtn-client/core/cache.rb +11 -14
- data/lib/sgtn-client/core/config.rb +56 -170
- data/lib/sgtn-client/i18n_backend.rb +7 -5
- data/lib/sgtn-client/loader/cache.rb +23 -36
- data/lib/sgtn-client/loader/loader_factory.rb +5 -4
- data/lib/sgtn-client/loader/local_translation.rb +3 -3
- data/lib/sgtn-client/loader/server.rb +4 -4
- data/lib/sgtn-client/loader/single_loader.rb +22 -19
- data/lib/sgtn-client/loader/source.rb +3 -3
- data/lib/sgtn-client/loader/source_comparer.rb +1 -1
- data/lib/sgtn-client/loader.rb +1 -0
- data/lib/sgtn-client/sgtn-client.rb +44 -86
- data/lib/sgtn-client/util/cache-util.rb +1 -9
- data/lib/sgtn-client/util/locale-util.rb +60 -27
- data/lib/sgtn-client/util/string-util.rb +5 -2
- data/lib/sgtn-client/util/validate-util.rb +9 -19
- data/lib/singleton-client.rb +12 -2
- metadata +20 -55
- data/lib/sgtn-client/core/logging.rb +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b1a0ab0b7bfa8a2fef42c229aae91032941499667f0885882f924d6a3f75694
|
4
|
+
data.tar.gz: be51ed5dbdf30cbc7f67353b5d96ba49e4e20a5de61ea9bd7d99d4f27785e1a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3a9b167165b5d6f6726ed8be0b07fadc7ef4aeb0c8ecb8f941bc77e5ff7ada988557f97098c33d1907fb7f24b1cac17f7df4fa3de3b96cce2e64a4240633c58
|
7
|
+
data.tar.gz: 44e84a38eb2e592793546c812817b751b0366b5af2ed0591eeaed1e4dd5b014af70b33674b7da69cac2873e16fb9ab3b47ff85d7d5f463b3a3bcfd987930ee7e
|
@@ -8,19 +8,19 @@ module SgtnClient
|
|
8
8
|
module Implementation
|
9
9
|
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
10
10
|
def getString(component, key, locale)
|
11
|
-
SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
|
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
15
|
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
16
16
|
def getString_p(component, key, plural_args, locale)
|
17
|
-
SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
|
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
21
|
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
22
22
|
def getString_f(component, key, args, locale, *_optionals)
|
23
|
-
SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
|
23
|
+
SgtnClient.logger.debug { "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}" }
|
24
24
|
s = translate(key, component, locale) { nil }
|
25
25
|
return nil if s.nil?
|
26
26
|
|
@@ -39,16 +39,16 @@ module SgtnClient
|
|
39
39
|
get_translations(component, locale)
|
40
40
|
end
|
41
41
|
|
42
|
-
# raise error when translation is not found
|
43
42
|
def translate(key, component, locale = nil, **kwargs)
|
44
|
-
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}"
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
43
|
+
SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}" }
|
44
|
+
|
45
|
+
begin
|
46
|
+
best_match_locale = LocaleUtil.get_best_locale(locale || self.locale, component)
|
47
|
+
messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
|
48
|
+
result = messages&.fetch(key, nil)
|
49
|
+
rescue StandardError => e
|
50
|
+
SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] translation is missing. {#{key}, #{component}, #{locale}}. #{e}" }
|
51
|
+
result = nil
|
52
52
|
end
|
53
53
|
|
54
54
|
if result.nil?
|
@@ -61,23 +61,21 @@ module SgtnClient
|
|
61
61
|
if kwargs.empty?
|
62
62
|
result
|
63
63
|
else
|
64
|
-
|
65
|
-
result.localize(locale) % kwargs
|
64
|
+
result.localize(actual_locale) % kwargs
|
66
65
|
end
|
67
66
|
end
|
68
67
|
alias t translate
|
69
68
|
|
70
69
|
def get_translations(component, locale = nil)
|
71
|
-
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}"
|
70
|
+
SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}" }
|
72
71
|
|
73
|
-
|
74
|
-
|
75
|
-
if items.nil? && !LocaleUtil.is_source_locale(locale)
|
76
|
-
items = get_bundle(component, LocaleUtil.get_source_locale)
|
77
|
-
locale = LocaleUtil.get_source_locale
|
78
|
-
end
|
72
|
+
best_match_locale = LocaleUtil.get_best_locale(locale || self.locale, component)
|
73
|
+
messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
|
79
74
|
|
80
|
-
{ 'component' => component, 'locale' =>
|
75
|
+
{ 'component' => component, 'locale' => actual_locale, 'messages' => messages } if messages
|
76
|
+
rescue StandardError => e
|
77
|
+
SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] translations are missing. {#{component}, #{locale}}. #{e}"
|
78
|
+
nil
|
81
79
|
end
|
82
80
|
|
83
81
|
def locale
|
@@ -85,7 +83,7 @@ module SgtnClient
|
|
85
83
|
end
|
86
84
|
|
87
85
|
def locale=(value)
|
88
|
-
RequestStore.store[:locale] =
|
86
|
+
RequestStore.store[:locale] = value
|
89
87
|
end
|
90
88
|
|
91
89
|
private
|
@@ -99,15 +97,29 @@ module SgtnClient
|
|
99
97
|
end
|
100
98
|
|
101
99
|
def get_bundle!(component, locale)
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
100
|
+
SgtnClient.config.loader.get_bundle(component, locale)
|
101
|
+
rescue StandardError
|
102
|
+
# delete the locale from the available_bundles of component to avoid repeated calls to server
|
103
|
+
SgtnClient.config.available_bundles.delete(Common::BundleID.new(component, locale))
|
104
|
+
SgtnClient.config.available_locales(component)&.delete(locale)
|
105
|
+
SgtnClient.config.changed
|
106
|
+
SgtnClient.config.notify_observers(:available_locales, component)
|
107
|
+
raise
|
108
|
+
end
|
109
|
+
|
110
|
+
def get_bundle_with_fallback(component, locale)
|
111
|
+
messages = get_bundle(component, locale)
|
112
|
+
return messages, locale if messages
|
113
|
+
|
114
|
+
LocaleUtil.locale_fallbacks.each do |l|
|
115
|
+
next if l == locale
|
107
116
|
|
108
|
-
|
117
|
+
messages = get_bundle(component, l)
|
118
|
+
return messages, l if messages
|
119
|
+
end
|
109
120
|
end
|
110
121
|
end
|
122
|
+
|
111
123
|
extend Implementation
|
112
124
|
end
|
113
125
|
end
|
@@ -3,25 +3,24 @@
|
|
3
3
|
|
4
4
|
module SgtnClient
|
5
5
|
class SingleOperation
|
6
|
-
def initialize(*conditions, &
|
7
|
-
raise 'no way to create a new obj' unless
|
6
|
+
def initialize(*conditions, &creator)
|
7
|
+
raise 'no way to create a new obj' unless creator
|
8
8
|
|
9
9
|
@lock = Mutex.new
|
10
10
|
@hash = {}
|
11
11
|
|
12
12
|
@conditions = conditions
|
13
|
-
@creator =
|
13
|
+
@creator = creator
|
14
14
|
end
|
15
15
|
|
16
|
-
# return new created object
|
17
|
-
def operate(id,
|
16
|
+
# return new created object (return nil possibly)
|
17
|
+
def operate(id, &block)
|
18
18
|
@lock.synchronize do
|
19
19
|
obj = @hash[id]
|
20
20
|
@conditions.each do |con|
|
21
|
-
return obj unless con.call(id, obj
|
21
|
+
return obj unless con.call(id, obj)
|
22
22
|
end
|
23
|
-
|
24
|
-
@hash[id] = @creator.call(id, obj, *args)
|
23
|
+
@hash[id] = @creator.call(id, &block)
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
@@ -7,27 +7,24 @@ module SgtnClient::Core
|
|
7
7
|
class Cache
|
8
8
|
Entry = Struct.new(:expiry, :items)
|
9
9
|
|
10
|
-
def self.initialize(disabled=false, opts={})
|
11
|
-
@@opts = opts
|
12
|
-
SgtnClient.logger.debug "[Cache][initialize] Disable cache? #{disabled}"
|
13
|
-
@@data = Hash.new
|
14
|
-
end
|
15
|
-
|
16
10
|
def self.get(key)
|
17
|
-
SgtnClient.logger.debug "[Cache][get]get cache for key: "
|
18
|
-
return
|
11
|
+
SgtnClient.logger.debug { "[Cache][get]get cache for key: #{key}" }
|
12
|
+
return data&.dig(key)
|
19
13
|
end
|
20
14
|
|
21
|
-
def self.put(key, items, ttl
|
22
|
-
ttl ||= @@opts[:ttl]
|
15
|
+
def self.put(key, items, ttl)
|
23
16
|
# hours from new
|
24
|
-
SgtnClient.logger.debug "[Cache][put]put cache for key '
|
25
|
-
|
17
|
+
SgtnClient.logger.debug { "[Cache][put]put cache for key '#{key}' with expired time at'" + (Time.now + ttl*60).to_s }
|
18
|
+
data[key] = Entry.new(Time.now + ttl*60, items)
|
26
19
|
end
|
27
20
|
|
28
21
|
def self.clear
|
29
|
-
SgtnClient.logger.debug "[Cache][clear]clear cache!"
|
30
|
-
|
22
|
+
SgtnClient.logger.debug { "[Cache][clear]clear cache!" }
|
23
|
+
@data = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.data
|
27
|
+
@data ||= {}
|
31
28
|
end
|
32
29
|
end
|
33
30
|
|
@@ -1,200 +1,86 @@
|
|
1
1
|
# Copyright 2022 VMware, Inc.
|
2
2
|
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
-
require '
|
5
|
-
require 'yaml'
|
6
|
-
require 'set'
|
4
|
+
require 'concurrent/map'
|
7
5
|
require 'observer'
|
6
|
+
require 'set'
|
7
|
+
require 'singleton'
|
8
8
|
|
9
9
|
module SgtnClient
|
10
|
-
|
10
|
+
class Config # :nodoc:
|
11
|
+
include Observable
|
12
|
+
include Singleton
|
13
|
+
|
14
|
+
attr_accessor :product_name, :version, :vip_server, :translation_bundle, :source_bundle, :cache_expiry_period, :log_file, :log_level
|
15
|
+
|
16
|
+
attr_writer :logger
|
17
|
+
|
18
|
+
def logger
|
19
|
+
@logger ||= if log_file
|
20
|
+
puts "create log file: '#{log_file}', level: #{log_level}"
|
21
|
+
require 'lumberjack'
|
22
|
+
Lumberjack::Logger.new(log_file, level: log_level, max_size: '1M', keep: 4)
|
23
|
+
else
|
24
|
+
require 'logger'
|
25
|
+
Logger.new(STDOUT, level: log_level || Logger::INFO)
|
26
|
+
end
|
27
|
+
end
|
11
28
|
|
12
|
-
def
|
13
|
-
|
29
|
+
def loader
|
30
|
+
@loader ||= TranslationLoader::LoaderFactory.create(self)
|
14
31
|
end
|
15
32
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
begin
|
23
|
-
config.dup.merge!(env)
|
24
|
-
rescue Errno::ENOENT => error
|
25
|
-
Config.new(env)
|
26
|
-
end
|
27
|
-
else
|
28
|
-
Config.config(env, override_configurations)
|
29
|
-
end
|
33
|
+
def available_bundles
|
34
|
+
loader.available_bundles
|
35
|
+
rescue StandardError => e
|
36
|
+
SgtnClient.logger.error 'failed to get available bundles'
|
37
|
+
SgtnClient.logger.error e
|
38
|
+
Set.new
|
30
39
|
end
|
31
40
|
|
32
|
-
|
41
|
+
def available_components
|
42
|
+
bundles = available_bundles
|
43
|
+
return Set.new if bundles.nil? || bundles.empty?
|
33
44
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
:mode, :endpoint, :merchant_endpoint, :platform_endpoint, :ipn_endpoint,
|
45
|
-
:rest_endpoint, :rest_token_endpoint, :client_id, :client_secret,
|
46
|
-
:openid_endpoint, :openid_redirect_uri, :openid_client_id, :openid_client_secret,
|
47
|
-
:verbose_logging, :product_name, :version, :vip_server,
|
48
|
-
:translation_bundle, :source_bundle, :cache_expiry_period, :disable_cache, :default_language
|
49
|
-
|
50
|
-
|
51
|
-
# Create Config object
|
52
|
-
# === Options(Hash)
|
53
|
-
# * <tt>username</tt> -- Username
|
54
|
-
# * <tt>password</tt> -- Password
|
55
|
-
# * <tt>signature</tt> (Optional if certificate present) -- Signature
|
56
|
-
# * <tt>app_id</tt> -- Application ID
|
57
|
-
# * <tt>cert_path</tt> (Optional if signature present) -- Certificate file path
|
58
|
-
def initialize(options)
|
59
|
-
merge!(options)
|
45
|
+
define_bundles_methods(bundles) unless bundles.respond_to?(:components)
|
46
|
+
bundles.components
|
47
|
+
end
|
48
|
+
|
49
|
+
def available_locales(component)
|
50
|
+
bundles = available_bundles
|
51
|
+
return Set.new if bundles.nil? || bundles.empty?
|
52
|
+
|
53
|
+
define_bundles_methods(bundles) unless bundles.respond_to?(:locales)
|
54
|
+
bundles.locales(component)
|
60
55
|
end
|
61
56
|
|
62
|
-
|
63
|
-
def merge!(options)
|
57
|
+
def update(options)
|
64
58
|
options.each do |key, value|
|
65
59
|
send("#{key}=", value)
|
66
60
|
end
|
67
|
-
self
|
68
61
|
end
|
69
|
-
|
70
|
-
class << self
|
71
|
-
|
72
|
-
@@config_cache = {}
|
73
|
-
def load(file_name, default_env = default_environment)
|
74
|
-
@@config_cache = {}
|
75
|
-
@@configurations = read_configurations(file_name)
|
76
|
-
@@default_environment = default_env
|
77
|
-
config
|
78
|
-
end
|
79
|
-
|
80
|
-
|
81
|
-
# Get default environment name
|
82
|
-
def default_environment
|
83
|
-
@@default_environment ||= ENV['SGTN_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development"
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
# Set default environment
|
88
|
-
def default_environment=(env)
|
89
|
-
@@default_environment = env.to_s
|
90
|
-
end
|
91
|
-
|
92
|
-
def configure(options = {}, &block)
|
93
|
-
begin
|
94
|
-
self.config.merge!(options)
|
95
|
-
rescue Errno::ENOENT
|
96
|
-
self.configurations = { default_environment => options }
|
97
|
-
end
|
98
|
-
block.call(self.config) if block
|
99
|
-
self.config
|
100
|
-
end
|
101
|
-
alias_method :set_config, :configure
|
102
|
-
|
103
|
-
# Create or Load Config object based on given environment and configurations.
|
104
|
-
# === Attributes
|
105
|
-
# * <tt>env</tt> (Optional) -- Environment name
|
106
|
-
# * <tt>override_configuration</tt> (Optional) -- Override the configuration given in file.
|
107
|
-
# === Example
|
108
|
-
# Config.config
|
109
|
-
# Config.config(:development)
|
110
|
-
# Config.config(:development, { :app_id => "XYZ" })
|
111
|
-
def config(env = default_environment, override_configuration = {})
|
112
|
-
if env.is_a? Hash
|
113
|
-
override_configuration = env
|
114
|
-
env = default_environment
|
115
|
-
end
|
116
|
-
if override_configuration.nil? or override_configuration.empty?
|
117
|
-
default_config(env)
|
118
|
-
else
|
119
|
-
default_config(env).dup.merge!(override_configuration)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
def default_config(env = nil)
|
124
|
-
env = (env || default_environment).to_s
|
125
|
-
if configurations[env]
|
126
|
-
@@config_cache[env] ||= new(configurations[env])
|
127
|
-
else
|
128
|
-
raise Exceptions::MissingConfig.new("Configuration[#{env}] NotFound")
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
# Get raw configurations in Hash format.
|
133
|
-
def configurations
|
134
|
-
@@configurations ||= read_configurations
|
135
|
-
end
|
136
|
-
|
137
|
-
# Set configuration
|
138
|
-
def configurations=(configs)
|
139
|
-
@@config_cache = {}
|
140
|
-
@@configurations = configs && Hash[configs.map{|k,v| [k.to_s, v] }]
|
141
|
-
end
|
142
|
-
|
143
|
-
# Set logger
|
144
|
-
def logger=(logger)
|
145
|
-
Logging.logger = logger
|
146
|
-
end
|
147
62
|
|
148
|
-
|
149
|
-
def logger
|
150
|
-
if @@configurations[:mode] == 'live' and Logging.logger.level == Logger::DEBUG
|
151
|
-
Logging.logger.warn "DEBUG log level not allowed in live mode for security of confidential information. Changing log level to INFO..."
|
152
|
-
Logging.logger.level = Logger::INFO
|
153
|
-
end
|
154
|
-
Logging.logger
|
155
|
-
end
|
63
|
+
private
|
156
64
|
|
65
|
+
def define_bundles_methods(bundles)
|
66
|
+
bundles.instance_eval do |_|
|
67
|
+
@component_locales ||= Concurrent::Map.new
|
157
68
|
|
158
|
-
def
|
159
|
-
@
|
160
|
-
config = Config.configurations[Config.default_environment]
|
161
|
-
TranslationLoader::LoaderFactory.create(config)
|
162
|
-
end
|
69
|
+
def components
|
70
|
+
@components ||= reduce(Set.new) { |components, id| components << id.component }
|
163
71
|
end
|
164
72
|
|
165
|
-
def
|
166
|
-
|
167
|
-
|
168
|
-
SgtnClient.logger.error 'failed to get available bundles'
|
169
|
-
SgtnClient.logger.error e
|
170
|
-
Set.new
|
171
|
-
end
|
73
|
+
def locales(component)
|
74
|
+
@component_locales[component] ||= begin
|
75
|
+
return Set.new unless components.include?(component)
|
172
76
|
|
173
|
-
|
174
|
-
bundles = available_bundles
|
175
|
-
return Set.new if bundles.nil? || bundles.empty?
|
176
|
-
|
177
|
-
unless bundles.respond_to?(:locales)
|
178
|
-
def bundles.locales
|
179
|
-
@locales ||= reduce(Set.new) { |locales, id| locales << id.locale }
|
180
|
-
end
|
181
|
-
changed
|
182
|
-
notify_observers(:available_locales)
|
77
|
+
each_with_object(Set.new) { |id, locales| locales << id.locale if id.component == component }
|
183
78
|
end
|
184
|
-
bundles.locales
|
185
79
|
end
|
80
|
+
end
|
186
81
|
|
187
|
-
|
188
|
-
|
189
|
-
# === Arguments
|
190
|
-
# * <tt>file_name</tt> (Optional) -- Configuration file path
|
191
|
-
def read_configurations(file_name = "config/sgtnclient.yml")
|
192
|
-
erb = ERB.new(File.read(file_name))
|
193
|
-
erb.filename = file_name
|
194
|
-
YAML.load(erb.result)
|
195
|
-
end
|
196
|
-
|
82
|
+
changed
|
83
|
+
notify_observers(:available_locales)
|
197
84
|
end
|
198
85
|
end
|
199
|
-
|
200
86
|
end
|
@@ -16,7 +16,7 @@ module SgtnClient # :nodoc:
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def initialized?
|
19
|
-
@initialized
|
19
|
+
@initialized ||= true
|
20
20
|
end
|
21
21
|
|
22
22
|
def load_translations(*) end
|
@@ -24,7 +24,7 @@ module SgtnClient # :nodoc:
|
|
24
24
|
def store_translations(*) end
|
25
25
|
|
26
26
|
def available_locales
|
27
|
-
SgtnClient
|
27
|
+
SgtnClient.config.available_locales(@component).to_a
|
28
28
|
end
|
29
29
|
|
30
30
|
def reload!; end
|
@@ -33,14 +33,16 @@ module SgtnClient # :nodoc:
|
|
33
33
|
|
34
34
|
def translations; end
|
35
35
|
|
36
|
-
def exists?(locale, key)
|
37
|
-
!!(translate(locale, key) { nil })
|
36
|
+
def exists?(locale, key, options)
|
37
|
+
!!(translate(locale, key, options) { nil })
|
38
38
|
end
|
39
39
|
|
40
40
|
def translate(locale, key, options)
|
41
41
|
flat_key = I18n::Backend::Flatten.normalize_flat_keys(locale, key, options[:scope], '.')
|
42
42
|
values = options.except(*I18n::RESERVED_KEYS)
|
43
|
-
|
43
|
+
Translation.translate(flat_key, @component, locale, **values) { nil }
|
44
44
|
end
|
45
|
+
|
46
|
+
def localize(locale, object, format, options) end
|
45
47
|
end
|
46
48
|
end
|
@@ -8,58 +8,45 @@ module SgtnClient
|
|
8
8
|
module Cache # :nodoc:
|
9
9
|
# get from cache, return expired data immediately
|
10
10
|
def get_bundle(component, locale)
|
11
|
-
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
|
11
|
+
SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}" }
|
12
12
|
|
13
|
-
key =
|
13
|
+
key = Common::BundleID.new(component, locale)
|
14
14
|
cache_item = CacheUtil.get_cache(key)
|
15
15
|
if cache_item
|
16
|
-
if CacheUtil.is_expired(cache_item)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
rescue StandardError => e
|
21
|
-
SgtnClient.logger.error "an error occured while loading bundle: component=#{component}, locale=#{locale}"
|
22
|
-
SgtnClient.logger.error e
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
return cache_item.dig(:items)
|
16
|
+
load_bundle(component, locale, sync: false) if CacheUtil.is_expired(cache_item)
|
17
|
+
cache_item[:items]
|
18
|
+
else
|
19
|
+
load_bundle(component, locale)
|
27
20
|
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def available_bundles
|
24
|
+
SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}]" }
|
28
25
|
|
29
|
-
|
26
|
+
cache_item = CacheUtil.get_cache(CONSTS::AVAILABLE_BUNDLES_KEY)
|
27
|
+
if cache_item
|
28
|
+
super(sync: false) if CacheUtil.is_expired(cache_item)
|
29
|
+
cache_item[:items]
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
30
33
|
end
|
34
|
+
end
|
31
35
|
|
32
|
-
|
36
|
+
module CacheFiller
|
33
37
|
def load_bundle(component, locale)
|
34
|
-
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
|
38
|
+
SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}" }
|
35
39
|
|
36
|
-
key = CacheUtil.get_cachekey(component, locale)
|
37
40
|
item = super
|
38
|
-
CacheUtil.write_cache(
|
41
|
+
CacheUtil.write_cache(Common::BundleID.new(component, locale), item) if item
|
39
42
|
item
|
40
43
|
end
|
41
44
|
|
42
45
|
def available_bundles
|
43
|
-
SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}]"
|
44
|
-
|
45
|
-
cache_item = CacheUtil.get_cache(CONSTS::AVAILABLE_BUNDLES_KEY)
|
46
|
-
if cache_item
|
47
|
-
if CacheUtil.is_expired(cache_item)
|
48
|
-
Thread.new do # TODO: Use one thread
|
49
|
-
begin
|
50
|
-
item = super
|
51
|
-
CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
|
52
|
-
rescue StandardError => e
|
53
|
-
SgtnClient.logger.error 'an error occured while loading available bundles.'
|
54
|
-
SgtnClient.logger.error e
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
return cache_item.dig(:items)
|
59
|
-
end
|
46
|
+
SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}]" }
|
60
47
|
|
61
48
|
item = super
|
62
|
-
CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
|
49
|
+
CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item # TODO: don't save when empty
|
63
50
|
item
|
64
51
|
end
|
65
52
|
end
|
@@ -5,16 +5,17 @@ module SgtnClient
|
|
5
5
|
module TranslationLoader
|
6
6
|
module LoaderFactory
|
7
7
|
def self.create(config)
|
8
|
-
SgtnClient.logger.info "[#{method(__callee__).owner}.#{__callee__}] config=#{config}"
|
8
|
+
SgtnClient.logger.info "[#{method(__callee__).owner}.#{__callee__}] config=#{config.inspect}"
|
9
9
|
|
10
10
|
loaders = []
|
11
|
-
loaders << Source.new(config) if config
|
12
|
-
loaders << SgtnServer.new(config) if config
|
13
|
-
loaders << LocalTranslation.new(config) if config
|
11
|
+
loaders << Source.new(config) if config.source_bundle
|
12
|
+
loaders << SgtnServer.new(config) if config.vip_server
|
13
|
+
loaders << LocalTranslation.new(config) if config.translation_bundle
|
14
14
|
raise SingletonError, 'no translation is available!' if loaders.empty?
|
15
15
|
|
16
16
|
chain_loader = Class.new(Chain)
|
17
17
|
chain_loader.include SourceComparer
|
18
|
+
chain_loader.include CacheFiller
|
18
19
|
chain_loader.include SingleLoader
|
19
20
|
chain_loader.include Cache
|
20
21
|
|
@@ -12,11 +12,11 @@ module SgtnClient
|
|
12
12
|
BUNDLE_SUFFIX = '.json'.freeze
|
13
13
|
|
14
14
|
def initialize(config)
|
15
|
-
@base_path = Pathname.new(config
|
15
|
+
@base_path = Pathname.new(config.translation_bundle) + config.product_name + config.version.to_s
|
16
16
|
end
|
17
17
|
|
18
18
|
def load_bundle(component, locale)
|
19
|
-
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component=#{component}, locale=#{locale}"
|
19
|
+
SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] component=#{component}, locale=#{locale}" }
|
20
20
|
|
21
21
|
file_name = BUNDLE_PREFIX + locale + BUNDLE_SUFFIX
|
22
22
|
file_path = @base_path + component + file_name
|
@@ -30,7 +30,7 @@ module SgtnClient
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def available_bundles
|
33
|
-
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}]"
|
33
|
+
SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}]" }
|
34
34
|
|
35
35
|
@available_bundles ||= begin
|
36
36
|
@base_path.glob('*/*.json').reduce(Set.new) do |bundles, f|
|