i18n-active_record 1.0.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/i18n/active_record/version.rb +1 -1
- data/lib/i18n/backend/active_record/translation.rb +9 -3
- data/lib/i18n/backend/active_record.rb +80 -78
- data/test/active_record_test.rb +1 -1
- data/test/test_helper.rb +18 -12
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 696d09fb79116c47ec3924b359f31ba4c632d68a075d6da1af591dcb0412548f
|
4
|
+
data.tar.gz: eff1a17efd57a2f09546e80aa0d1269b07751f8615b65377c891d89186407d45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8dc72a6d40f4ce0da1d98f5ade7bc26f660766aa72814771f359b9c6c8e943a4c51acfce8f612794cd3ede72c3ad9808e9442524c951fded1c2ec2457be0201f
|
7
|
+
data.tar.gz: cc0fcb1c5121cefa325f52c4edbae8308a095d896cd6a8e3917d5c0144c1460f4f7795a4de6be9f25f399e678b4ce9fa2e95472e0cd651ee08aeead47e30ab03
|
data/README.md
CHANGED
@@ -115,7 +115,7 @@ The `interpolations` field is otherwise unused since the "value" in `Translation
|
|
115
115
|
|
116
116
|
## Examples
|
117
117
|
|
118
|
-
* http://collectiveidea.com/blog/archives/2016/05/31/beyond-yml-files-dynamic-translations/
|
118
|
+
* http://collectiveidea.com/blog/archives/2016/05/31/beyond-yml-files-dynamic-translations/ ([use `before_action`](https://github.com/svenfuchs/i18n-active_record/issues/138) for Rails 5.1+)
|
119
119
|
|
120
120
|
## Contributing
|
121
121
|
|
@@ -53,8 +53,14 @@ module I18n
|
|
53
53
|
|
54
54
|
self.table_name = 'translations'
|
55
55
|
|
56
|
-
|
57
|
-
|
56
|
+
if ::ActiveRecord.version >= Gem::Version.new('7.1.0.alpha')
|
57
|
+
serialize :value, coder: YAML
|
58
|
+
serialize :interpolations, coder: YAML, type: Array
|
59
|
+
else
|
60
|
+
serialize :value
|
61
|
+
serialize :interpolations, Array
|
62
|
+
end
|
63
|
+
|
58
64
|
after_commit :invalidate_translations_cache
|
59
65
|
|
60
66
|
class << self
|
@@ -79,7 +85,7 @@ module I18n
|
|
79
85
|
Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
|
80
86
|
end
|
81
87
|
|
82
|
-
def
|
88
|
+
def to_h
|
83
89
|
Translation.all.each.with_object({}) do |t, memo|
|
84
90
|
locale_hash = (memo[t.locale.to_sym] ||= {})
|
85
91
|
keys = t.key.split('.')
|
@@ -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_h
|
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
|
data/test/active_record_test.rb
CHANGED
@@ -123,7 +123,7 @@ class I18nBackendActiveRecordTest < I18n::TestCase
|
|
123
123
|
I18n.t('.') # Fixes test flakiness by loading available locales
|
124
124
|
I18n::Backend::ActiveRecord::Translation.destroy_all
|
125
125
|
|
126
|
-
|
126
|
+
assert_match(/[Tt]ranslation missing: en\.no key/, I18n.t('.'))
|
127
127
|
end
|
128
128
|
|
129
129
|
test 'intially unitinitialized' do
|
data/test/test_helper.rb
CHANGED
@@ -10,35 +10,35 @@ require 'i18n/tests'
|
|
10
10
|
|
11
11
|
begin
|
12
12
|
require 'active_record'
|
13
|
-
|
13
|
+
ActiveRecord::Base.connection
|
14
14
|
rescue LoadError => e
|
15
15
|
puts "can't use ActiveRecord backend because: #{e.message}"
|
16
|
-
rescue
|
16
|
+
rescue ActiveRecord::ConnectionNotEstablished
|
17
17
|
require 'i18n/backend/active_record'
|
18
18
|
|
19
|
-
case ENV
|
19
|
+
case ENV.fetch('DB', nil)
|
20
20
|
when 'postgres'
|
21
|
-
|
21
|
+
ActiveRecord::Base.establish_connection(
|
22
22
|
adapter: 'postgresql',
|
23
23
|
database: 'i18n_unittest',
|
24
|
-
username: ENV
|
25
|
-
password: ENV
|
24
|
+
username: ENV.fetch('PG_USER', 'postgres'),
|
25
|
+
password: ENV.fetch('PG_PASSWORD', 'postgres'),
|
26
26
|
host: 'localhost'
|
27
27
|
)
|
28
28
|
when 'mysql'
|
29
|
-
|
29
|
+
ActiveRecord::Base.establish_connection(
|
30
30
|
adapter: 'mysql2',
|
31
31
|
database: 'i18n_unittest',
|
32
|
-
username: ENV
|
33
|
-
password: ENV
|
32
|
+
username: ENV.fetch('MYSQL_USER', 'root'),
|
33
|
+
password: ENV.fetch('MYSQL_PASSWORD', ''),
|
34
34
|
host: '127.0.0.1'
|
35
35
|
)
|
36
36
|
else
|
37
|
-
|
37
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
38
38
|
end
|
39
39
|
|
40
|
-
|
41
|
-
|
40
|
+
ActiveRecord::Migration.verbose = false
|
41
|
+
ActiveRecord::Schema.define(version: 1) do
|
42
42
|
create_table :translations, force: true do |t|
|
43
43
|
t.string :locale
|
44
44
|
t.string :key
|
@@ -48,6 +48,12 @@ rescue ::ActiveRecord::ConnectionNotEstablished
|
|
48
48
|
end
|
49
49
|
add_index :translations, %i[locale key], unique: true
|
50
50
|
end
|
51
|
+
|
52
|
+
if ActiveRecord::Base.respond_to?(:yaml_column_permitted_classes=)
|
53
|
+
ActiveRecord::Base.yaml_column_permitted_classes = [Symbol]
|
54
|
+
elsif ActiveRecord.respond_to?(:yaml_column_permitted_classes=)
|
55
|
+
ActiveRecord.yaml_column_permitted_classes = [Symbol]
|
56
|
+
end
|
51
57
|
end
|
52
58
|
|
53
59
|
TEST_CASE = defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase
|
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.2.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:
|
11
|
+
date: 2023-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -150,7 +150,8 @@ files:
|
|
150
150
|
homepage: http://github.com/svenfuchs/i18n-active_record
|
151
151
|
licenses:
|
152
152
|
- MIT
|
153
|
-
metadata:
|
153
|
+
metadata:
|
154
|
+
rubygems_mfa_required: 'true'
|
154
155
|
post_install_message:
|
155
156
|
rdoc_options: []
|
156
157
|
require_paths:
|
@@ -166,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
167
|
- !ruby/object:Gem::Version
|
167
168
|
version: '0'
|
168
169
|
requirements: []
|
169
|
-
rubygems_version: 3.
|
170
|
+
rubygems_version: 3.4.10
|
170
171
|
signing_key:
|
171
172
|
specification_version: 4
|
172
173
|
summary: I18n ActiveRecord backend
|