singleton-client 0.7.4 → 0.7.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -28
- data/lib/sgtn-client/api/source.rb +10 -66
- data/lib/sgtn-client/api/t.rb +20 -18
- data/lib/sgtn-client/api/translation.rb +82 -116
- 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 +4 -1
- data/lib/sgtn-client/cldr/localized_time.rb +4 -1
- data/lib/sgtn-client/common/data.rb +30 -0
- data/lib/sgtn-client/common/single_operation.rb +34 -0
- data/lib/sgtn-client/core/cache.rb +14 -80
- data/lib/sgtn-client/core/config.rb +42 -3
- data/lib/sgtn-client/core/exceptions.rb +3 -0
- data/lib/sgtn-client/core/logging.rb +3 -1
- data/lib/sgtn-client/exceptions.rb +6 -0
- data/lib/sgtn-client/formatters/plurals/plural_formatter.rb +4 -1
- data/lib/sgtn-client/loader/cache.rb +71 -0
- data/lib/sgtn-client/loader/chain_loader.rb +49 -0
- data/lib/sgtn-client/loader/consts.rb +15 -0
- data/lib/sgtn-client/loader/loader_factory.rb +33 -0
- data/lib/sgtn-client/loader/local_translation.rb +49 -0
- data/lib/sgtn-client/loader/server.rb +94 -0
- data/lib/sgtn-client/loader/single_loader.rb +48 -0
- data/lib/sgtn-client/loader/source.rb +56 -0
- data/lib/sgtn-client/loader/source_comparer.rb +58 -0
- data/lib/sgtn-client/sgtn-client.rb +11 -1
- data/lib/sgtn-client/util/cache-util.rb +33 -44
- data/lib/sgtn-client/util/locale-util.rb +67 -28
- data/lib/sgtn-client/util/string-util.rb +12 -0
- data/lib/sgtn-client/util/validate-util.rb +4 -7
- data/lib/singleton-client.rb +15 -0
- data/lib/singleton-ruby.rb +5 -0
- data/lib/version.rb +2 -0
- metadata +43 -147
- data/lib/sgtn-client/core/request.rb +0 -21
- data/lib/sgtn-client/util/file-util.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f94c0be66f68113fc69f95dbf7d55dac5c21854acc5cf89381e92e0ce0a6abd1
|
4
|
+
data.tar.gz: d70dd2bbc7b19af02a70a4de9be14aa39d50a3e043b310074b8fa84fbeb50f2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3be87ccbb4b86a43e75bdc7b6cf2d3e989d640ee99a4140e0a84f372ab9a4bbd43966f6cbd264892435c02f0ddb1c41c07a6a9e4524bb1222e8eb2a0e206c487
|
7
|
+
data.tar.gz: adc2e6eca62d579fd134e3cf8b19e49368ecc79541faa6d5195c812482ab5bcc0b2aff4000d836cf007ef0b0afffb3f2c465f954c95d1a1b16378a51b1e31f99
|
data/README.md
CHANGED
@@ -1,54 +1,53 @@
|
|
1
|
-
#
|
1
|
+
# Singleton Client for Ruby
|
2
2
|
|
3
3
|
## Prerequisites
|
4
4
|
- Ruby version: 3.0.0 or above
|
5
5
|
- Bundler version: 2.2.3 or above
|
6
6
|
|
7
7
|
## Run Unit Test
|
8
|
-
rake spec
|
8
|
+
`rake spec`
|
9
9
|
|
10
10
|
## Usage
|
11
11
|
|
12
12
|
Basic Usage:
|
13
13
|
|
14
14
|
```ruby
|
15
|
-
require 'singleton-
|
16
|
-
|
17
|
-
include SgtnClient
|
18
|
-
|
19
|
-
SgtnClient.load(file, mode)
|
20
|
-
|
21
|
-
SgtnClient::Source.loadBundles(locale)
|
22
|
-
|
23
|
-
@Result = SgtnClient::Translation.getString(component, key, locale)
|
15
|
+
require 'singleton-client'
|
24
16
|
|
17
|
+
Sgtn.load_config(file, env)
|
18
|
+
result = Sgtn.translate(key, component, locale)
|
25
19
|
```
|
26
20
|
## API Usage
|
27
21
|
|
28
22
|
### Get a string's translation
|
29
|
-
|
30
|
-
|
31
|
-
### Get a string's translation and format it with placeholders
|
32
|
-
SgtnClient::Translation.getString_f(component, key, args, locale)
|
33
|
-
|
34
|
-
### Get a component's translations
|
35
|
-
SgtnClient::Translation.getStrings(component, locale)
|
36
|
-
|
23
|
+
`result = Sgtn.translate(key, component, locale)`
|
37
24
|
|
38
|
-
|
39
|
-
|
40
|
-
Before call below APIs(without locale and component arguments), it requires to set the locale and component in the initial codes.
|
41
|
-
|
42
|
-
### Get a string's translation
|
43
|
-
SgtnClient::T.s(key)
|
25
|
+
### Get a string's translation with default value when no translation
|
26
|
+
`result = Sgtn.translate(key, component, locale) { 'default value' }`
|
44
27
|
|
45
28
|
### Get a string's translation and format it with placeholders
|
46
|
-
|
29
|
+
`result = Sgtn.translate(key, component, locale, **args)`
|
47
30
|
|
48
|
-
### Get
|
49
|
-
|
31
|
+
### Get pluralized translation
|
32
|
+
`result = Sgtn.translate(key, component, locale, **args)`
|
50
33
|
|
34
|
+
### Get translations of a bundle
|
35
|
+
`result = Sgtn.get_translations(component, locale)`
|
51
36
|
|
37
|
+
### Set locale for a request
|
38
|
+
`Sgtn.locale = 'en'`
|
52
39
|
|
40
|
+
### Get locale of the request
|
41
|
+
`result = Sgtn.locale`
|
53
42
|
|
43
|
+
### Get a string's translation with locale set
|
44
|
+
```ruby
|
45
|
+
Sgtn.locale = 'en'
|
46
|
+
result = Sgtn.translate(key, component)
|
47
|
+
```
|
54
48
|
|
49
|
+
### Get translations of a bundle with locale set
|
50
|
+
```ruby
|
51
|
+
Sgtn.locale = 'en'
|
52
|
+
result = Sgtn.get_translations(component)
|
53
|
+
```
|
@@ -1,71 +1,15 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
4
|
+
require 'sgtn-client/loader/source'
|
1
5
|
|
2
6
|
module SgtnClient
|
3
|
-
|
4
|
-
autoload :CacheUtil, "sgtn-client/util/cache-util"
|
7
|
+
autoload :CacheUtil, 'sgtn-client/util/cache-util'
|
5
8
|
|
6
9
|
class Source
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
12
|
-
if items.nil?
|
13
|
-
items = getBundle(component, locale)
|
14
|
-
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
15
|
-
else
|
16
|
-
SgtnClient.logger.debug "[Source][getSource]getting sources from cache with key: " + cache_key
|
17
|
-
end
|
18
|
-
s = items.nil?? nil : items[locale][key]
|
19
|
-
if items.nil? || s.nil?
|
20
|
-
SgtnClient.logger.debug "[Source][getSource]source not found, return key: " + key
|
21
|
-
#return key
|
22
|
-
return nil
|
23
|
-
else
|
24
|
-
return s
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.getSources(component, locale)
|
29
|
-
SgtnClient.logger.debug "[Source][getSources]component=#{component}, locale=#{locale}"
|
30
|
-
cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
|
31
|
-
expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
|
32
|
-
if items.nil? || expired
|
33
|
-
items = getBundle(component, locale)
|
34
|
-
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
35
|
-
else
|
36
|
-
SgtnClient.logger.debug "[Source][getSources]getting sources from cache with key: " + cache_key
|
37
|
-
end
|
38
|
-
return items
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.loadBundles(locale)
|
42
|
-
SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
|
43
|
-
env = SgtnClient::Config.default_environment
|
44
|
-
SgtnClient::Config.configurations.default = locale
|
45
|
-
source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
|
46
|
-
Dir.foreach(source_bundle) do |component|
|
47
|
-
next if component == '.' || component == '..'
|
48
|
-
yamlfile = File.join(source_bundle, component + "/" + locale + ".yml")
|
49
|
-
bundle = SgtnClient::FileUtil.read_yml(yamlfile)
|
50
|
-
cachekey = SgtnClient::CacheUtil.get_cachekey(component, locale)
|
51
|
-
SgtnClient::CacheUtil.write_cache(cachekey,bundle)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
private
|
56
|
-
def self.getBundle(component, locale)
|
57
|
-
SgtnClient.logger.debug "[Source][getBundle]component=#{component}, locale=#{locale}"
|
58
|
-
env = SgtnClient::Config.default_environment
|
59
|
-
source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
|
60
|
-
bundlepath = source_bundle + "/" + component + "/" + locale + ".yml"
|
61
|
-
begin
|
62
|
-
bundle = SgtnClient::FileUtil.read_yml(bundlepath)
|
63
|
-
rescue => exception
|
64
|
-
SgtnClient.logger.error exception.message
|
65
|
-
end
|
66
|
-
return bundle
|
67
|
-
end
|
68
|
-
|
10
|
+
def self.loadBundles(locale)
|
11
|
+
SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
|
12
|
+
SgtnClient::Config.configurations.default = locale
|
13
|
+
end
|
69
14
|
end
|
70
|
-
|
71
|
-
end
|
15
|
+
end
|
data/lib/sgtn-client/api/t.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
module SgtnClient
|
5
|
+
class T
|
6
|
+
def self.s(key)
|
7
|
+
locale = RequestStore.store[:locale]
|
8
|
+
component = RequestStore.store[:component]
|
9
|
+
Translation.getString(component, key, locale)
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def self.s_f(key, args)
|
13
|
+
locale = RequestStore.store[:locale]
|
14
|
+
component = RequestStore.store[:component]
|
15
|
+
Translation.getString_f(component, key, args, locale)
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
18
|
+
def self.c
|
19
|
+
locale = RequestStore.store[:locale]
|
20
|
+
component = RequestStore.store[:component]
|
21
|
+
Translation.getStrings(component, locale)
|
21
22
|
end
|
22
|
-
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,147 +1,113 @@
|
|
1
|
-
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
module Core
|
6
|
-
autoload :Request, "sgtn-client/core/request"
|
7
|
-
autoload :Cache, "sgtn-client/core/cache"
|
8
|
-
autoload :CacheUtil, "sgtn-client/util/cache-util"
|
9
|
-
autoload :LocaleUtil, "sgtn-client/util/locale-util"
|
10
|
-
end
|
11
|
-
|
12
|
-
class Translation
|
4
|
+
require 'request_store'
|
13
5
|
|
14
|
-
|
6
|
+
module SgtnClient
|
7
|
+
module Translation
|
8
|
+
module Implementation
|
9
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
10
|
+
def getString(component, key, locale)
|
15
11
|
SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
|
16
|
-
|
17
|
-
if str.nil?
|
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
|
22
|
-
end
|
23
|
-
str
|
12
|
+
translate(key, component, locale) { nil }
|
24
13
|
end
|
25
14
|
|
26
|
-
|
15
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
16
|
+
def getString_p(component, key, plural_args, locale)
|
27
17
|
SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
|
28
|
-
|
29
|
-
if str.nil?
|
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
|
35
|
-
str.to_plural_s(:en, plural_args)
|
36
|
-
else
|
37
|
-
str.to_plural_s(locale, plural_args)
|
38
|
-
end
|
18
|
+
translate(key, component, locale, **plural_args) { nil }
|
39
19
|
end
|
40
20
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
21
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
22
|
+
def getString_f(component, key, args, locale, *_optionals)
|
23
|
+
SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
|
24
|
+
s = translate(key, component, locale) { nil }
|
25
|
+
return nil if s.nil?
|
26
|
+
|
27
|
+
if args.is_a?(Hash)
|
48
28
|
args.each do |source, arg|
|
49
29
|
s.gsub! "{#{source}}", arg
|
50
30
|
end
|
51
|
-
|
52
|
-
s =
|
53
|
-
|
54
|
-
|
31
|
+
elsif args.is_a?(Array)
|
32
|
+
s = s % args
|
33
|
+
end
|
34
|
+
s
|
55
35
|
end
|
56
36
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
37
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:get_translations</tt> instead.
|
38
|
+
def getStrings(component, locale)
|
39
|
+
get_translations(component, locale)
|
40
|
+
end
|
41
|
+
|
42
|
+
# raise error when translation is not found
|
43
|
+
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
|
+
locale = locale.nil? ? self.locale : SgtnClient::LocaleUtil.get_best_locale(locale)
|
47
|
+
|
48
|
+
result = get_bundle(component, locale)&.fetch(key, nil)
|
49
|
+
if result.nil? && !LocaleUtil.is_source_locale(locale)
|
50
|
+
locale = LocaleUtil.get_source_locale
|
51
|
+
result = get_bundle(component, locale)&.fetch(key, nil)
|
72
52
|
end
|
73
|
-
return items
|
74
|
-
end
|
75
53
|
|
54
|
+
if result.nil?
|
55
|
+
return key unless block_given?
|
76
56
|
|
77
|
-
|
57
|
+
result = yield
|
58
|
+
return if result.nil?
|
59
|
+
end
|
78
60
|
|
79
|
-
|
80
|
-
|
81
|
-
if items.nil? || items["messages"] == nil
|
82
|
-
nil
|
61
|
+
if kwargs.empty?
|
62
|
+
result
|
83
63
|
else
|
84
|
-
|
64
|
+
locale = result.locale if result.is_a?(SgtnClient::StringUtil)
|
65
|
+
result.localize(locale) % kwargs
|
85
66
|
end
|
86
67
|
end
|
68
|
+
alias t translate
|
87
69
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
if items.nil?
|
94
|
-
items =
|
95
|
-
|
96
|
-
items = SgtnClient::Source.getSources(component, SgtnClient::Config.configurations.default)
|
97
|
-
SgtnClient::Core::Cache.put(cache_key, items, 60)
|
98
|
-
else
|
99
|
-
SgtnClient::CacheUtil.write_cache(cache_key, items)
|
100
|
-
end
|
101
|
-
else
|
102
|
-
SgtnClient.logger.debug "[Translation]get translations from cache with key: " + cache_key
|
70
|
+
def get_translations(component, locale = nil)
|
71
|
+
SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}"
|
72
|
+
|
73
|
+
locale = locale.nil? ? self.locale : SgtnClient::LocaleUtil.get_best_locale(locale)
|
74
|
+
items = get_bundle(component, locale)
|
75
|
+
if items.nil? && !LocaleUtil.is_source_locale(locale)
|
76
|
+
items = get_bundle(component, LocaleUtil.get_source_locale)
|
77
|
+
locale = LocaleUtil.get_source_locale
|
103
78
|
end
|
104
79
|
|
105
|
-
|
106
|
-
|
80
|
+
{ 'component' => component, 'locale' => locale, 'messages' => items || {} } if items
|
81
|
+
end
|
107
82
|
|
108
|
-
def
|
109
|
-
|
110
|
-
mode = SgtnClient::Config.configurations[env]["bundle_mode"]
|
111
|
-
SgtnClient.logger.debug "[Translation][load]mode=#{mode}"
|
112
|
-
if mode == 'offline'
|
113
|
-
return load_o(component, locale)
|
114
|
-
else
|
115
|
-
return load_s(component, locale)
|
116
|
-
end
|
83
|
+
def locale
|
84
|
+
RequestStore.store[:locale] ||= SgtnClient::LocaleUtil.get_fallback_locale
|
117
85
|
end
|
118
86
|
|
119
|
-
def
|
120
|
-
|
121
|
-
product_name = SgtnClient::Config.configurations[env]["product_name"]
|
122
|
-
version = SgtnClient::Config.configurations[env]["version"].to_s
|
123
|
-
translation_bundle = SgtnClient::Config.configurations[env]["translation_bundle"]
|
124
|
-
bundlepath = translation_bundle + "/" + product_name + "/" + version + "/" + component + "/messages_" + locale + ".json"
|
125
|
-
SgtnClient::FileUtil.read_json(bundlepath)
|
87
|
+
def locale=(value)
|
88
|
+
RequestStore.store[:locale] = SgtnClient::LocaleUtil.get_best_locale(value)
|
126
89
|
end
|
127
90
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
rescue => exception
|
137
|
-
SgtnClient.logger.error exception.message
|
138
|
-
end
|
139
|
-
if obj != nil
|
140
|
-
obj = obj["data"]
|
141
|
-
end
|
142
|
-
return obj
|
91
|
+
private
|
92
|
+
|
93
|
+
def get_bundle(component, locale)
|
94
|
+
get_bundle!(component, locale)
|
95
|
+
rescue StandardError => e
|
96
|
+
SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] failed to get a bundle. component: #{component}, locale: #{locale}"
|
97
|
+
SgtnClient.logger.error e
|
98
|
+
nil
|
143
99
|
end
|
144
100
|
|
145
|
-
|
101
|
+
def get_bundle!(component, locale)
|
102
|
+
id = SgtnClient::Common::BundleID.new(component, locale)
|
103
|
+
bundles = SgtnClient::Config.available_bundles
|
104
|
+
unless bundles.nil? || bundles.empty? || bundles.include?(id)
|
105
|
+
raise SgtnClient::SingletonError, 'bundle is unavailable.'
|
106
|
+
end
|
146
107
|
|
147
|
-
|
108
|
+
SgtnClient::Config.loader.get_bundle(component, locale)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
extend Implementation
|
112
|
+
end
|
113
|
+
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,5 +1,8 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
1
3
|
|
2
4
|
String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
5
|
+
# <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
|
3
6
|
def to_plural_s(locale, arg)
|
4
7
|
num_str = SgtnClient::Formatters::PluralFormatter.new(locale).num_s(self, arg)
|
5
8
|
if num_str.nil? || num_str.empty?
|
@@ -8,4 +11,4 @@ String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
|
|
8
11
|
num_str
|
9
12
|
end
|
10
13
|
end
|
11
|
-
LOCALIZE
|
14
|
+
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
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
4
|
+
module SgtnClient
|
5
|
+
module Common
|
6
|
+
class BundleID
|
7
|
+
attr_reader :locale, :component
|
8
|
+
|
9
|
+
def initialize(component, locale)
|
10
|
+
@locale = locale
|
11
|
+
@component = component
|
12
|
+
@key = [@component, @locale].hash
|
13
|
+
end
|
14
|
+
|
15
|
+
def hash
|
16
|
+
@key
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(other)
|
20
|
+
(other.is_a? self.class) && @locale == other.locale && @component == other.component
|
21
|
+
end
|
22
|
+
|
23
|
+
alias eql? ==
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
"locale=#{@locale}, component=#{@component}}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
|
+
|
4
|
+
module SgtnClient
|
5
|
+
class SingleOperation
|
6
|
+
def initialize(*conditions, &block)
|
7
|
+
raise 'no way to create a new obj' unless block
|
8
|
+
|
9
|
+
@lock = Mutex.new
|
10
|
+
@hash = {}
|
11
|
+
|
12
|
+
@conditions = conditions
|
13
|
+
@creator = block
|
14
|
+
end
|
15
|
+
|
16
|
+
# return new created object
|
17
|
+
def operate(id, *args)
|
18
|
+
@lock.synchronize do
|
19
|
+
obj = @hash[id]
|
20
|
+
@conditions.each do |con|
|
21
|
+
return obj unless con.call(id, obj, *args)
|
22
|
+
end
|
23
|
+
# TODO: whatif returning nil
|
24
|
+
@hash[id] = @creator.call(id, obj, *args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def remove_object(id)
|
29
|
+
@lock.synchronize do
|
30
|
+
@hash.delete(id)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|