i18n-active_record 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 402118f9fb5b5b88dec6745249ded8e131b509c3bb4b8b35e5857182c85e6ca8
4
- data.tar.gz: f120411a8ffc00c065ef50eda91d32515e23f51d6e4125184513c5a34496edb5
3
+ metadata.gz: 934e01acf036e3a1c8eb23f5f8f96c55e84d462a31a10183c12389e8466e233d
4
+ data.tar.gz: a0c8301e9e0c5eaac6badf2dfdeca19b581296eaa0e0a15464a13fbc6796087f
5
5
  SHA512:
6
- metadata.gz: f36d522e0792d4c5460fe9d7b3af69d2fa27817f2f2ecd58eb18ecdcea13012e542bfe4022d2e27c18f454107e63c1406450c3d709830caeb1c33f1a0b2997b3
7
- data.tar.gz: 7ee1bec18a25efbad1fbec55e89b061b050cfdf2fc51c2453f6dee559ad71bf60ab8c5d8397900c4d87be82dff59f830e494c427c42214d60469c047d06c08fc
6
+ metadata.gz: 0702e26d0b3a600ff2d28db7473e9a34311f18a099a0009ec6bccec5382d5c4bcc9042b33653a214c377c332db0d902adb64103e05a98c1371d6d19ccb3c0297
7
+ data.tar.gz: f2dd449433f4b88763c5952831feff25956c727aa4ff086c2ae130709bfd968ef36f61bf7b2af9065681ac7132e7e7c3a05c13b268e5c1c3e282285cd1787c6b
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module ActiveRecord
5
- VERSION = '1.0.1'
5
+ VERSION = '1.1.0'
6
6
  end
7
7
  end
@@ -11,9 +11,6 @@ module I18n
11
11
  autoload :Translation, 'i18n/backend/active_record/translation'
12
12
  autoload :Configuration, 'i18n/backend/active_record/configuration'
13
13
 
14
- include Base
15
- include Flatten
16
-
17
14
  class << self
18
15
  def configure
19
16
  yield(config) if block_given?
@@ -24,109 +21,114 @@ module I18n
24
21
  end
25
22
  end
26
23
 
27
- def initialize(*args)
28
- super
29
-
24
+ def initialize
30
25
  reload!
31
26
  end
32
27
 
33
- def available_locales
34
- Translation.available_locales
35
- rescue ::ActiveRecord::StatementInvalid
36
- []
37
- end
28
+ module Implementation
29
+ include Base
30
+ include Flatten
38
31
 
39
- def store_translations(locale, data, options = {})
40
- escape = options.fetch(:escape, true)
32
+ def available_locales
33
+ Translation.available_locales
34
+ rescue ::ActiveRecord::StatementInvalid
35
+ []
36
+ end
41
37
 
42
- flatten_translations(locale, data, escape, false).each do |key, value|
43
- translation = Translation.locale(locale).lookup(expand_keys(key))
38
+ def store_translations(locale, data, options = {})
39
+ escape = options.fetch(:escape, true)
44
40
 
45
- if self.class.config.cleanup_with_destroy
46
- translation.destroy_all
47
- else
48
- translation.delete_all
41
+ flatten_translations(locale, data, escape, false).each do |key, value|
42
+ translation = Translation.locale(locale).lookup(expand_keys(key))
43
+
44
+ if self.class.config.cleanup_with_destroy
45
+ translation.destroy_all
46
+ else
47
+ translation.delete_all
48
+ end
49
+
50
+ Translation.create(locale: locale.to_s, key: key.to_s, value: value)
49
51
  end
50
52
 
51
- Translation.create(locale: locale.to_s, key: key.to_s, value: value)
53
+ reload! if self.class.config.cache_translations
52
54
  end
53
55
 
54
- reload! if self.class.config.cache_translations
55
- end
56
-
57
- def reload!
58
- @translations = nil
56
+ def reload!
57
+ @translations = nil
59
58
 
60
- self
61
- end
59
+ self
60
+ end
62
61
 
63
- def initialized?
64
- !@translations.nil?
65
- end
62
+ def initialized?
63
+ !@translations.nil?
64
+ end
66
65
 
67
- def init_translations
68
- @translations = Translation.to_hash
69
- end
66
+ def init_translations
67
+ @translations = Translation.to_hash
68
+ end
70
69
 
71
- def translations(do_init: false)
72
- init_translations if do_init || !initialized?
73
- @translations ||= {}
74
- end
70
+ def translations(do_init: false)
71
+ init_translations if do_init || !initialized?
72
+ @translations ||= {}
73
+ end
75
74
 
76
- protected
75
+ protected
77
76
 
78
- def lookup(locale, key, scope = [], options = {})
79
- key = normalize_flat_keys(locale, key, scope, options[:separator])
80
- key = key[1..-1] if key.first == '.'
81
- key = key[0..-2] if key.last == '.'
77
+ def lookup(locale, key, scope = [], options = {})
78
+ key = normalize_flat_keys(locale, key, scope, options[:separator])
79
+ key = key[1..-1] if key.first == '.'
80
+ key = key[0..-2] if key.last == '.'
82
81
 
83
- if self.class.config.cache_translations
84
- keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)
82
+ if self.class.config.cache_translations
83
+ keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)
85
84
 
86
- return translations.dig(*keys)
87
- end
85
+ return translations.dig(*keys)
86
+ end
88
87
 
89
- result = if key == ''
90
- Translation.locale(locale).all
91
- else
92
- Translation.locale(locale).lookup(key)
93
- end
88
+ result = if key == ''
89
+ Translation.locale(locale).all
90
+ else
91
+ Translation.locale(locale).lookup(key)
92
+ end
94
93
 
95
- if result.empty?
96
- nil
97
- elsif result.first.key == key
98
- result.first.value
99
- else
100
- result = result.inject({}) do |hash, translation|
101
- hash.deep_merge build_translation_hash_by_key(key, translation)
94
+ if result.empty?
95
+ nil
96
+ elsif result.first.key == key
97
+ result.first.value
98
+ else
99
+ result = result.inject({}) do |hash, translation|
100
+ hash.deep_merge build_translation_hash_by_key(key, translation)
101
+ end
102
+ result.deep_symbolize_keys
102
103
  end
103
- result.deep_symbolize_keys
104
104
  end
105
- end
106
105
 
107
- def build_translation_hash_by_key(lookup_key, translation)
108
- hash = {}
106
+ def build_translation_hash_by_key(lookup_key, translation)
107
+ hash = {}
109
108
 
110
- chop_range = if lookup_key == ''
111
- 0..-1
112
- else
113
- (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
114
- end
115
- translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
116
- translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
117
- iterator[key] = translation_nested_keys[index + 1] ? {} : translation.value
118
- iterator[key]
119
- end
109
+ chop_range = if lookup_key == ''
110
+ 0..-1
111
+ else
112
+ (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
113
+ end
114
+ translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
115
+ translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
116
+ iterator[key] = translation_nested_keys[index + 1] ? {} : translation.value
117
+ iterator[key]
118
+ end
120
119
 
121
- hash
122
- end
120
+ hash
121
+ end
123
122
 
124
- # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
125
- def expand_keys(key)
126
- key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, k|
127
- keys << [keys.last, k].compact.join(FLATTEN_SEPARATOR)
123
+ # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
124
+ def expand_keys(key)
125
+ key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, k|
126
+ keys << [keys.last, k].compact.join(FLATTEN_SEPARATOR)
127
+ end
128
128
  end
129
129
  end
130
+
131
+ include Implementation
130
132
  end
131
133
  end
132
134
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-15 00:00:00.000000000 Z
11
+ date: 2021-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n