lit 1.1.5 → 1.1.6

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: 7dcf44da1e10a331cafdd9352461c1d9b985c6ea3d97e61744a1390e036be71a
4
- data.tar.gz: 3c03d7e70b3c172092d807539543f0389e4e04c7f0420c66ffb5f96f742a4fa6
3
+ metadata.gz: 1bda244665d30fcad8f9ad16a593e47ffd6cf54d8d7c9a6a777d5038bad75f1b
4
+ data.tar.gz: 7217df2945e7a5b1813d2a4892eebf46fcf8a97cdd8e9d8648c7a23a2962715f
5
5
  SHA512:
6
- metadata.gz: ecf7688c8aa6f32c6c01fab3b14024720f75155b66ed1be9494c40f0ed4755855e2b73b9d1d528a7a215513316a27ceb3427a84e72e0420f611a4c7e877b6ce0
7
- data.tar.gz: 5f4fe480d203c6ade865f44385b4dfb951aa4a32c33f8e224b9371e3f36f5a16acd444ef73136bc28f7278d6518b7681d29540dca11833e6f7ecfc3b776b1386
6
+ metadata.gz: 5b6f9f9d65dc9463316c0abd3a56ad2ddcff9a56b6a8a4ed86b4fc6a1b526ac90b46298adf5109d40d58eccd2f5b5164f5e5f4ef424787b156c7248d6f94c2a1
7
+ data.tar.gz: 3f7888ee62c7c037b32d83ca184cc06715e90366a789f7c39f51f4205d1f0134aa8460755228f169e66d3b1724e23bf87f2b88589f34f09d7f68a02453158743
@@ -23,6 +23,18 @@ module Lit::FrontendHelper
23
23
  def t(key, options = {})
24
24
  translate(key, options)
25
25
  end
26
+
27
+ def missing_translation(key, options)
28
+ # We try to humanize the key. Rails will do
29
+ # it anyway in below call to super, but then it will wrap it also in
30
+ # translation_missing span.
31
+ # Humanizing key should be last resort
32
+ if Lit::Services::HumanizeService.should_humanize?(key)
33
+ return Lit::Services::HumanizeService.humanize_and_cache(key, options)
34
+ end
35
+
36
+ super(key, options)
37
+ end
26
38
  end
27
39
  prepend Lit::FrontendHelper::TranslationKeyWrapper
28
40
 
@@ -1,4 +1,5 @@
1
1
  require 'i18n'
2
+ require 'lit/services/humanize_service'
2
3
 
3
4
  module Lit
4
5
  class I18nBackend
@@ -28,7 +29,7 @@ module Lit
28
29
  @untranslated_key = key if key.present? && options[:default].instance_of?(Object)
29
30
 
30
31
  if key.nil? && options[:lit_default_copy].present?
31
- update_default_localization(locale, content, options)
32
+ update_default_localization(locale, options)
32
33
  end
33
34
  end
34
35
 
@@ -72,10 +73,12 @@ module Lit
72
73
  ::Rails::VERSION::MAJOR >= 7
73
74
  end
74
75
 
75
- def update_default_localization(locale, content, options)
76
+ def update_default_localization(locale, options)
76
77
  parts = I18n.normalize_keys(locale, @untranslated_key, options[:scope], options[:separator])
77
78
  key_with_locale = parts.join('.')
78
- @cache.update_locale(key_with_locale, content, content.is_a?(Array))
79
+ content = options[:lit_default_copy]
80
+ # we do not force array on singular strings packed into Array
81
+ @cache.update_locale(key_with_locale, content, content.is_a?(Array) && content.length > 1)
79
82
  end
80
83
 
81
84
  def can_dup_default(options = {})
@@ -118,12 +121,18 @@ module Lit
118
121
  if options[:lit_default_copy].is_a?(Array)
119
122
  default = options[:lit_default_copy].map do |key_or_value|
120
123
  if key_or_value.is_a?(Symbol)
121
- I18n.normalize_keys(nil, key_or_value.to_s, options[:scope], options[:separator]).join('.').to_sym
124
+ normalized = I18n.normalize_keys(
125
+ nil, key_or_value.to_s, options[:scope], options[:separator]
126
+ ).join('.')
127
+ if on_rails_6_1_or_higher? && Lit::Services::HumanizeService.should_humanize?(key)
128
+ Lit::Services::HumanizeService.humanize(normalized)
129
+ else
130
+ normalized.to_sym
131
+ end
122
132
  else
123
133
  key_or_value
124
134
  end
125
135
  end
126
- default = default.first if default.is_a?(Array)
127
136
  else
128
137
  default = options[:lit_default_copy]
129
138
  end
@@ -131,22 +140,16 @@ module Lit
131
140
  end
132
141
  # if we have content now, let's store it in cache
133
142
  if content.present?
134
- @cache[key_with_locale] = content
135
- content = @cache[key_with_locale]
136
- end
137
- # content might be nil - default value passed to cache was in fact
138
- # useless.
139
- # if content is still nil, we may try to humanize it. Rails will do
140
- # it anyway if we return nil, but then it will wrap it also in
141
- # translation_missing span.
142
- # Humanizing key should be last resort
143
- if content.nil? && Lit.humanize_key && Lit.humanize_key_ignored.match(key).nil?
144
- content = key.to_s.split('.').last.humanize
145
- if content.present?
146
- @cache[key_with_locale] = content
147
- content = @cache[key_with_locale]
143
+ content = Array.wrap(content).compact.reject(&:empty?).reverse.find do |default_cand|
144
+ @cache[key_with_locale] = default_cand
145
+ @cache[key_with_locale]
148
146
  end
149
147
  end
148
+
149
+ if content.nil? && !on_rails_6_1_or_higher? && Lit::Services::HumanizeService.should_humanize?(key)
150
+ @cache[key_with_locale] = Lit::Services::HumanizeService.humanize(key)
151
+ content = @cache[key_with_locale]
152
+ end
150
153
  end
151
154
  end
152
155
  # return translated content
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lit
4
+ module Services
5
+ # Checks if should humanize based on config and blacklist.
6
+ # Performs humanize if required
7
+ # Caches the value of humanization
8
+ class HumanizeService
9
+ def self.should_humanize?(key)
10
+ Lit.humanize_key && Lit.humanize_key_ignored.match(key).nil?
11
+ end
12
+
13
+ def self.humanize(key)
14
+ key.to_s.split('.').last.humanize
15
+ end
16
+
17
+ def self.humanize_and_cache(key, options)
18
+ content = humanize(key)
19
+ parts = I18n.normalize_keys(
20
+ options[:locale] || I18n.locale, key, options[:scope], options[:separator]
21
+ )
22
+ key_with_locale = parts.join('.')
23
+ I18n.cache_store[key_with_locale] = content
24
+ I18n.cache_store[key_with_locale]
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/lit/version.rb CHANGED
@@ -10,7 +10,7 @@ module Lit
10
10
  module Version
11
11
  MAJOR = 1
12
12
  MINOR = 1
13
- TINY = 5
13
+ TINY = 6
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
16
16
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Litwiniuk
8
8
  - Piotr Boniecki
9
9
  - Michał Buszkiewicz
10
10
  - Szymon Soppa
11
- autorequire:
11
+ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2021-12-23 00:00:00.000000000 Z
14
+ date: 2022-03-28 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -312,6 +312,7 @@ files:
312
312
  - lib/lit/middleware.rb
313
313
  - lib/lit/rails.rb
314
314
  - lib/lit/railtie.rb
315
+ - lib/lit/services/humanize_service.rb
315
316
  - lib/lit/services/localization_keys_to_hash_service.rb
316
317
  - lib/lit/version.rb
317
318
  - lib/tasks/lit_tasks.rake
@@ -319,7 +320,7 @@ homepage: https://github.com/prograils/lit
319
320
  licenses:
320
321
  - MIT
321
322
  metadata: {}
322
- post_install_message:
323
+ post_install_message:
323
324
  rdoc_options: []
324
325
  require_paths:
325
326
  - lib
@@ -335,7 +336,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
335
336
  version: '0'
336
337
  requirements: []
337
338
  rubygems_version: 3.1.6
338
- signing_key:
339
+ signing_key:
339
340
  specification_version: 4
340
341
  summary: Database powered i18n backend with web gui
341
342
  test_files: []