singleton-client-test 0.7.0.34 → 0.7.0.37

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6722a7f77f8f9ee4fbf2f481d0c66e30b818c61746a9c7e8471fc1b225079f72
4
- data.tar.gz: ba0e6acade9bc655f97a731cf760e15097b713e99000cf8f2612e4975a357d2d
3
+ metadata.gz: 365e6be5a17d3f95f2529789f8632092e859c793c96d67b4f8ddd6492f3e3687
4
+ data.tar.gz: a2df815daeda06f52484e61ef7c64e2c5bc6ce5d46332a10b60187bbc2440a59
5
5
  SHA512:
6
- metadata.gz: 27e3c13e20185a08049c2d634bc11e3a6d4e05095802e3c76026b74c34acd3a327b2e49c79020b9143479747e9d25fbfe5eaf4c859621afdc23f512859efd0fc
7
- data.tar.gz: bed85d6578397cd90e0823574f552d7f7e98e19a0f49e35e12963eecc32d5f87d85b8c0a8ab2c50c5c66b3ea81e630334194f8d4600fdc4a0eec2bf391b2decb
6
+ metadata.gz: 64ef532c3de19ee72c7b0c28661c1192856eb9ebdef0a335d7d702d98b0d7bf56411691cd3ce2c8ceeae829d0d8d8eaad53981cde88721b49e606c657f053028
7
+ data.tar.gz: 27d63abbf9b7df87e20997664eebc97853ae1837f5f3c3853c1f77ce1ace43000d25482ae6f261a3d18788fbba9f026ca75374e1a14c078aa068dbdfec8a532a
data/README.md CHANGED
@@ -1,54 +1,53 @@
1
- # Singeleton client for Ruby
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:unit
8
+ `rake spec`
9
9
 
10
10
  ## Usage
11
11
 
12
12
  Basic Usage:
13
13
 
14
14
  ```ruby
15
- require 'singleton-ruby'
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
- SgtnClient::Translation.getString(component, key, locale)
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
- ## API Usage(with request_store)
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
- SgtnClient::T.s_f(key, args)
29
+ `result = Sgtn.translate(key, component, locale, **args)`
47
30
 
48
- ### Get a component's translations
49
- SgtnClient::T.c()
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
+ ```
@@ -6,19 +6,19 @@ require 'request_store'
6
6
  module SgtnClient
7
7
  module Translation
8
8
  module Implementation
9
- # <b>DEPRECATED:</b> Please use <tt>Singleton:translate</tt> instead.
9
+ # <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
10
10
  def getString(component, key, locale)
11
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
- # <b>DEPRECATED:</b> Please use <tt>Singleton:translate</tt> instead.
15
+ # <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
16
16
  def getString_p(component, key, plural_args, locale)
17
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
- # <b>DEPRECATED:</b> Please use <tt>Singleton:translate</tt> instead.
21
+ # <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
22
22
  def getString_f(component, key, args, locale, *_optionals)
23
23
  SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
24
24
  s = translate(key, component, locale) { nil }
@@ -34,7 +34,7 @@ module SgtnClient
34
34
  s
35
35
  end
36
36
 
37
- # <b>DEPRECATED:</b> Please use <tt>Singleton:get_translations</tt> instead.
37
+ # <b>DEPRECATED:</b> Please use <tt>Sgtn:get_translations</tt> instead.
38
38
  def getStrings(component, locale)
39
39
  get_translations(component, locale)
40
40
  end
@@ -2,6 +2,7 @@
2
2
  # SPDX-License-Identifier: EPL-2.0
3
3
 
4
4
  String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
5
+ # <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
5
6
  def to_plural_s(locale, arg)
6
7
  num_str = SgtnClient::Formatters::PluralFormatter.new(locale).num_s(self, arg)
7
8
  if num_str.nil? || num_str.empty?
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'erb'
5
5
  require 'yaml'
6
-
6
+ require 'observer'
7
7
 
8
8
  module SgtnClient
9
9
  #include Exceptions
@@ -40,6 +40,7 @@ module SgtnClient
40
40
 
41
41
 
42
42
  class Config
43
+ extend Observable
43
44
 
44
45
  attr_accessor :username, :password, :signature, :app_id, :cert_path,
45
46
  :token, :token_secret, :subject,
@@ -182,6 +183,8 @@ module SgtnClient
182
183
  def bundles.locales
183
184
  @locales ||= reduce(Set.new) { |locales, id| locales << id.locale }
184
185
  end
186
+ changed
187
+ notify_observers(:available_locales)
185
188
  end
186
189
  bundles.locales
187
190
  end
@@ -1,7 +1,8 @@
1
1
  # Copyright 2022 VMware, Inc.
2
2
  # SPDX-License-Identifier: EPL-2.0
3
3
 
4
- require 'multi_json'
4
+ require 'json'
5
+ require 'pathname'
5
6
 
6
7
  module SgtnClient
7
8
  module Common
@@ -12,12 +12,9 @@ module SgtnClient
12
12
  def load_bundle(component, locale)
13
13
  SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
14
14
 
15
- @single_bundle_loader ||= single_loader { |c,l| super(c,l) }
15
+ @single_bundle_loader ||= single_loader { |c, l| super(c, l) }
16
16
  id = CacheUtil.get_cachekey(component, locale)
17
17
  @single_bundle_loader.operate(id, component, locale)&.value
18
- ensure
19
- # delete thread from hash after finish
20
- @single_bundle_loader.remove_object(id)
21
18
  end
22
19
 
23
20
  def available_bundles
@@ -25,21 +22,26 @@ module SgtnClient
25
22
 
26
23
  @single_available_bundles_loader ||= single_loader { super }
27
24
  @single_available_bundles_loader.operate(CONSTS::AVAILABLE_BUNDLES_KEY)&.value
28
- ensure
29
- @single_available_bundles_loader.remove_object(CONSTS::AVAILABLE_BUNDLES_KEY)
30
25
  end
31
26
 
32
27
  private
28
+
33
29
  def single_loader(&block)
34
- none_alive = proc { |_, thread| thread.nil? || thread.alive? == false }
30
+ loader = nil
31
+ none_alive = proc { |_, thread| thread.nil? }
35
32
  creator = proc do |id, _, *args|
36
33
  Thread.new do
37
34
  SgtnClient.logger.debug "start single loading #{id}"
38
- block.call(*args)
35
+ begin
36
+ block.call(*args)
37
+ ensure
38
+ # delete thread from hash after finish
39
+ loader.remove_object(id)
40
+ end
39
41
  end
40
42
  end
41
43
 
42
- SgtnClient::SingleOperation.new(none_alive, &creator)
44
+ loader = SgtnClient::SingleOperation.new(none_alive, &creator)
43
45
  end
44
46
  end
45
47
  end
@@ -1,6 +1,9 @@
1
1
  # Copyright 2022 VMware, Inc.
2
2
  # SPDX-License-Identifier: EPL-2.0
3
3
 
4
+ require 'pathname'
5
+ require 'psych/simple'
6
+
4
7
  module SgtnClient
5
8
  module Common
6
9
  autoload :BundleID, 'sgtn-client/common/data'
@@ -22,7 +25,7 @@ module SgtnClient
22
25
  total_messages = {}
23
26
 
24
27
  (@source_bundle_path + component).glob('**/*.{yml, yaml}') do |f|
25
- bundle = YAML.safe_load(File.read(f))
28
+ bundle = Psych.simple_load(File.read(f))
26
29
  messages = bundle&.first&.last # TODO: Warn about inconsistent source locale
27
30
  if messages.is_a?(Hash)
28
31
  total_messages.merge!(messages)
@@ -1,23 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright 2022 VMware, Inc.
2
4
  # SPDX-License-Identifier: EPL-2.0
5
+
3
6
  require 'set'
4
7
 
5
8
  module SgtnClient
6
9
  class LocaleUtil
7
10
  MAP_LOCALES = {
8
- 'zh-CN' => 'zh-Hans',
9
- 'zh-TW' => 'zh-Hant',
10
- 'zh-Hans-CN' => 'zh-Hans',
11
- 'zh-Hant-TW' => 'zh-Hant'
11
+ 'zh-cn' => 'zh-hans',
12
+ 'zh-tw' => 'zh-hant',
13
+ 'zh-hans-cn' => 'zh-hans',
14
+ 'zh-hant-tw' => 'zh-hant'
12
15
  }.freeze
16
+ LOCALE_SEPARATOR = '-'
13
17
 
14
18
  def self.get_best_locale(locale)
19
+ return locale if Config.available_locales.include?(locale)
20
+
15
21
  return get_fallback_locale if locale.nil?
16
22
 
17
23
  locale = locale.to_s
18
24
  return get_fallback_locale if locale.empty?
19
25
 
20
- get_best_match(locale.gsub('_', '-'))
26
+ get_best_match(locale.gsub('_', LOCALE_SEPARATOR).downcase)
21
27
  end
22
28
 
23
29
  def self.is_source_locale(locale = nil)
@@ -26,12 +32,12 @@ module SgtnClient
26
32
 
27
33
  def self.get_best_match(locale)
28
34
  locale = MAP_LOCALES[locale] || locale
29
- return locale if Config.available_locales.include?(locale)
30
-
31
- index = locale.rindex('-')
32
- return get_fallback_locale if index.nil?
35
+ lowercase_locales_map[locale] or begin
36
+ index = locale.rindex(LOCALE_SEPARATOR)
37
+ return get_fallback_locale if index.nil?
33
38
 
34
- get_best_match(locale[0...index])
39
+ get_best_match(locale[0...index])
40
+ end
35
41
  end
36
42
 
37
43
  def self.get_source_locale
@@ -47,6 +53,18 @@ module SgtnClient
47
53
  @fallback_locale ||= get_default_locale || get_source_locale || 'en'
48
54
  end
49
55
 
50
- private_class_method :get_best_match
56
+ def self.lowercase_locales_map
57
+ @lowercase_locales_map ||= Config.available_locales.each_with_object({}) do |locale, memo|
58
+ memo[locale.to_s.downcase] = locale
59
+ end
60
+ end
61
+
62
+ def self.reset_available_locales(type)
63
+ @lowercase_locales_map = nil if type == :available_locales
64
+ end
65
+
66
+ SgtnClient::Config.add_observer(self, :reset_available_locales)
67
+
68
+ private_class_method :get_best_match, :lowercase_locales_map, :reset_available_locales
51
69
  end
52
70
  end
@@ -3,7 +3,7 @@
3
3
 
4
4
  require_relative 'singleton-ruby'
5
5
 
6
- module Singleton # :nodoc:
6
+ module Sgtn # :nodoc:
7
7
  # load configuration from a file
8
8
  def self.load_config(*args)
9
9
  SgtnClient.load(*args)
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.34
4
+ version: 0.7.0.37
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-04-27 00:00:00.000000000 Z
11
+ date: 2022-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -142,6 +142,20 @@ dependencies:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
144
  version: '6.6'
145
+ - !ruby/object:Gem::Dependency
146
+ name: psych-simple
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '2.0'
152
+ type: :runtime
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - "~>"
157
+ - !ruby/object:Gem::Version
158
+ version: '2.0'
145
159
  description: Singleton Ruby client
146
160
  email: g11n-vip-project@vmware.com
147
161
  executables: []