i18n-active_record 1.0.1 → 1.1.0
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 +4 -4
- data/lib/i18n/active_record/version.rb +1 -1
- data/lib/i18n/backend/active_record.rb +80 -78
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 934e01acf036e3a1c8eb23f5f8f96c55e84d462a31a10183c12389e8466e233d
|
4
|
+
data.tar.gz: a0c8301e9e0c5eaac6badf2dfdeca19b581296eaa0e0a15464a13fbc6796087f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0702e26d0b3a600ff2d28db7473e9a34311f18a099a0009ec6bccec5382d5c4bcc9042b33653a214c377c332db0d902adb64103e05a98c1371d6d19ccb3c0297
|
7
|
+
data.tar.gz: f2dd449433f4b88763c5952831feff25956c727aa4ff086c2ae130709bfd968ef36f61bf7b2af9065681ac7132e7e7c3a05c13b268e5c1c3e282285cd1787c6b
|
@@ -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
|
28
|
-
super
|
29
|
-
|
24
|
+
def initialize
|
30
25
|
reload!
|
31
26
|
end
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
[]
|
37
|
-
end
|
28
|
+
module Implementation
|
29
|
+
include Base
|
30
|
+
include Flatten
|
38
31
|
|
39
|
-
|
40
|
-
|
32
|
+
def available_locales
|
33
|
+
Translation.available_locales
|
34
|
+
rescue ::ActiveRecord::StatementInvalid
|
35
|
+
[]
|
36
|
+
end
|
41
37
|
|
42
|
-
|
43
|
-
|
38
|
+
def store_translations(locale, data, options = {})
|
39
|
+
escape = options.fetch(:escape, true)
|
44
40
|
|
45
|
-
|
46
|
-
translation.
|
47
|
-
|
48
|
-
|
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
|
-
|
53
|
+
reload! if self.class.config.cache_translations
|
52
54
|
end
|
53
55
|
|
54
|
-
reload!
|
55
|
-
|
56
|
-
|
57
|
-
def reload!
|
58
|
-
@translations = nil
|
56
|
+
def reload!
|
57
|
+
@translations = nil
|
59
58
|
|
60
|
-
|
61
|
-
|
59
|
+
self
|
60
|
+
end
|
62
61
|
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
def initialized?
|
63
|
+
!@translations.nil?
|
64
|
+
end
|
66
65
|
|
67
|
-
|
68
|
-
|
69
|
-
|
66
|
+
def init_translations
|
67
|
+
@translations = Translation.to_hash
|
68
|
+
end
|
70
69
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
70
|
+
def translations(do_init: false)
|
71
|
+
init_translations if do_init || !initialized?
|
72
|
+
@translations ||= {}
|
73
|
+
end
|
75
74
|
|
76
|
-
|
75
|
+
protected
|
77
76
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
-
|
84
|
-
|
82
|
+
if self.class.config.cache_translations
|
83
|
+
keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)
|
85
84
|
|
86
|
-
|
87
|
-
|
85
|
+
return translations.dig(*keys)
|
86
|
+
end
|
88
87
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
88
|
+
result = if key == ''
|
89
|
+
Translation.locale(locale).all
|
90
|
+
else
|
91
|
+
Translation.locale(locale).lookup(key)
|
92
|
+
end
|
94
93
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
-
|
108
|
-
|
106
|
+
def build_translation_hash_by_key(lookup_key, translation)
|
107
|
+
hash = {}
|
109
108
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
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
|
-
|
122
|
-
|
120
|
+
hash
|
121
|
+
end
|
123
122
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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
|
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
|
+
date: 2021-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|