singleton-client-test 0.7.7.6 → 0.7.7.7

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: 3bd55bdad7f46bf9c0cffd42a710cfb6dcae97ecadbbb148af41d2b44be0afca
4
- data.tar.gz: 026ef3a390e9a88691decb97b1131ac31e358c9ede7628a7b698d38899a80465
3
+ metadata.gz: 71f20276ef709f5a183ab1e6572f171bfe9e5cdb0d0f09decea0fdb2e3aa2e88
4
+ data.tar.gz: 1eeebcc0cc3a964c90a00523fdda8aae09d3dc69ddc51af658c6218eb8e97c05
5
5
  SHA512:
6
- metadata.gz: 5792e7d5f543603927732321cf440be37266fdc5a65fdd6075017068bb98fd9e5b8b4c52e8421fb0508166997993117d6c151921243b642ea448fb5ceea2e542
7
- data.tar.gz: 7a8938ba9ffe0b6480a4b83e441b48d48eec98f5e2262019ff5db001e94fe50d404cc3c4277e464cab90ab7141076223b370d2c3a733058d95eecd940f8d8e88
6
+ metadata.gz: cd7cd9a1b977ddfd9110758b997c26bda3d8f2507f95036c87db24e130b7249f9c57871ce7f46c45827c810f0c7dd991b36dde656d3ce8a809e9ef4792554a65
7
+ data.tar.gz: 1d8c12a9f7dc6c65ebddcdf1eef50907cd8838a38a0c1a4c98c7001b4b9c77c55c5486d109fc7d9aca01c2c85c99e658ab381e8f2994e7818c73eb3d2a220295
@@ -39,55 +39,57 @@ module SgtnClient
39
39
  get_translations(component, locale)
40
40
  end
41
41
 
42
- def translate(key, component, locale = nil, **kwargs)
42
+ def get_translation!(key, component, locale)
43
+ [get_bundle!(component, locale)[key], locale]
44
+ end
45
+
46
+ def translate(key, component, locale = nil, **kwargs, &block)
47
+ translate!(key, component, locale, **kwargs, &block)
48
+ rescue StandardError => e
49
+ SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] {#{key}, #{component}, #{locale}}. #{e}" }
50
+ key
51
+ end
52
+ alias t translate
53
+
54
+ # raise error when translation is not found
55
+ def translate!(key, component, locale = nil, **kwargs, &block)
43
56
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}" }
44
57
 
45
58
  begin
46
59
  best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
47
- messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
48
- result = messages&.fetch(key, nil)
60
+ result, actual_locale = get_translation!(key, component, best_match_locale)
49
61
  rescue StandardError => e
50
- SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] translation is missing. {#{key}, #{component}, #{locale}}. #{e}" }
51
- result = nil
62
+ raise e if block.nil?
52
63
  end
53
-
54
64
  if result.nil?
55
- return key unless block_given?
65
+ raise SingletonError, 'translation is missing.' if block.nil?
56
66
 
57
- result = yield
67
+ result = block.call
58
68
  return if result.nil?
59
69
  end
60
70
 
61
- if kwargs.empty?
62
- result
63
- else
64
- result.localize(actual_locale) % kwargs
65
- end
71
+ kwargs.empty? ? result : result.localize(actual_locale) % kwargs
66
72
  end
67
- alias t translate
73
+ alias t! translate!
68
74
 
69
75
  def get_translations(component, locale = nil)
76
+ get_translations!(component, locale)
77
+ rescue StandardError => e
78
+ SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] {#{component}, #{locale}}. #{e}"
79
+ nil
80
+ end
81
+
82
+ def get_translations!(component, locale = nil)
70
83
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}" }
71
84
 
72
85
  best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
73
- messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
86
+ messages = get_bundle!(component, best_match_locale)
74
87
 
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
88
+ { 'component' => component, 'locale' => best_match_locale, 'messages' => messages } if messages
79
89
  end
80
90
 
81
91
  private
82
92
 
83
- def get_bundle(component, locale)
84
- get_bundle!(component, locale)
85
- rescue StandardError => e
86
- SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] failed to get a bundle. component: #{component}, locale: #{locale}"
87
- SgtnClient.logger.error e
88
- nil
89
- end
90
-
91
93
  def get_bundle!(component, locale)
92
94
  SgtnClient.config.loader.get_bundle(component, locale)
93
95
  rescue StandardError
@@ -98,20 +100,9 @@ module SgtnClient
98
100
  SgtnClient.config.notify_observers(:available_locales, component)
99
101
  raise
100
102
  end
101
-
102
- def get_bundle_with_fallback(component, locale)
103
- messages = get_bundle(component, locale)
104
- return messages, locale if messages
105
-
106
- LocaleUtil.locale_fallbacks.each do |l|
107
- next if l == locale
108
-
109
- messages = get_bundle(component, l)
110
- return messages, l if messages
111
- end
112
- end
113
103
  end
114
104
 
115
105
  extend Implementation
106
+ extend Fallbacks
116
107
  end
117
108
  end
@@ -43,7 +43,7 @@ module SgtnClient
43
43
  end
44
44
 
45
45
  def self.age
46
- @age ||= SgtnClient.config.cache_expiry_period * 60
46
+ @age ||= (SgtnClient.config.cache_expiry_period || 24 * 60) * 60
47
47
  end
48
48
  end
49
49
 
@@ -0,0 +1,38 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
4
+ module SgtnClient
5
+ module Fallbacks # :nodoc:
6
+ def get_translation!(key, component, locale)
7
+ error = nil
8
+ localechain(locale) do |l|
9
+ return super(key, component, l)
10
+ rescue StandardError => e
11
+ error = e
12
+ end
13
+ raise error if error
14
+ end
15
+
16
+ def get_translations!(component, locale = nil)
17
+ error = nil
18
+ localechain(locale) do |l|
19
+ result = super(component, l)
20
+ return result if result
21
+ rescue StandardError => e
22
+ error = e
23
+ end
24
+ raise error if error
25
+ end
26
+
27
+ private
28
+
29
+ def localechain(locale)
30
+ yield locale
31
+ LocaleUtil.locale_fallbacks.each do |l|
32
+ next if l == locale
33
+
34
+ yield l
35
+ end
36
+ end
37
+ end
38
+ end
@@ -9,6 +9,7 @@ module SgtnClient # :nodoc:
9
9
  autoload :TranslationLoader, 'sgtn-client/loader'
10
10
  autoload :SingleOperation, 'sgtn-client/common/single_operation'
11
11
  autoload :Translation, 'sgtn-client/api/translation'
12
+ autoload :Fallbacks, 'sgtn-client/fallbacks'
12
13
  autoload :T, 'sgtn-client/api/t'
13
14
  autoload :Source, 'sgtn-client/api/source'
14
15
  autoload :Config, 'sgtn-client/core/config'
@@ -74,7 +74,7 @@ module SgtnClient
74
74
  end
75
75
 
76
76
  def self.locale_fallbacks
77
- @locale_fallbacks ||= [get_default_locale, get_source_locale, EN_LOCALE].uniq(&:to_s) - [nil, '']
77
+ @locale_fallbacks ||= ([get_default_locale, get_source_locale, EN_LOCALE].uniq(&:to_s) - [nil, '']).freeze
78
78
  end
79
79
 
80
80
  def self.lowercase_locales_map(component)
@@ -14,7 +14,7 @@ module Sgtn # :nodoc:
14
14
  end
15
15
 
16
16
  def_delegator SgtnClient::Config, :instance, :config
17
- delegate %i[translate t get_translations] => SgtnClient::Translation,
17
+ delegate %i[translate! t! translate t get_translations! get_translations] => SgtnClient::Translation,
18
18
  %i[locale locale=] => SgtnClient,
19
19
  %i[logger product_name version vip_server translation_bundle
20
20
  source_bundle cache_expiry_period log_file log_level].flat_map { |m|
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.7.6
4
+ version: 0.7.7.7
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-08-30 00:00:00.000000000 Z
11
+ date: 2022-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -144,6 +144,7 @@ files:
144
144
  - lib/sgtn-client/core/config.rb
145
145
  - lib/sgtn-client/core/exceptions.rb
146
146
  - lib/sgtn-client/exceptions.rb
147
+ - lib/sgtn-client/fallbacks.rb
147
148
  - lib/sgtn-client/formatters/plurals/plural_formatter.rb
148
149
  - lib/sgtn-client/i18n_backend.rb
149
150
  - lib/sgtn-client/loader.rb