i18n-active_record 1.2.0 → 1.3.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 +18 -3
- data/lib/i18n/active_record/version.rb +1 -1
- data/lib/i18n/backend/active_record/configuration.rb +2 -1
- data/lib/i18n/backend/active_record/missing.rb +3 -2
- data/lib/i18n/backend/active_record/store_procs.rb +3 -1
- data/lib/i18n/backend/active_record.rb +15 -6
- data/test/active_record_test.rb +19 -0
- 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: a0bb9a3f4087d304d5ee5fdc38b8df8e31147a412c618fed9c82f1363db579be
|
4
|
+
data.tar.gz: b77a32f97c5d1785084a5c8332ff7e8909ce81ec6d3724ec664650540c72451d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46eb68288d4b64db1760235e72fd73a3a03e7e06d19b9f699ed4b160a423dfde3333fe92f9556fd65ed2d3f1e5d8e1e14d5ffebcfa7d7251fd37632bc3b36f44
|
7
|
+
data.tar.gz: 7f1ace5489592b78a2e0dab66564837025a21cb0a2b10dbaf3999a4ea128c0c983ba8f952b867236d71554226553b2c119ad32a232cf3a96c29f62200b29b9c9
|
data/README.md
CHANGED
@@ -89,11 +89,26 @@ end
|
|
89
89
|
|
90
90
|
You can now use `I18n.t('Your String')` to lookup translations in the database.
|
91
91
|
|
92
|
-
##
|
92
|
+
## Custom translation model
|
93
|
+
|
94
|
+
By default, the gem relies on [the built-in translation model](https://github.com/svenfuchs/i18n-active_record/blob/master/lib/i18n/backend/active_record/translation.rb).
|
95
|
+
However, to extend the default functionality, the translation model can be customized:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class MyTranslation < I18n::Backend::ActiveRecord::Translation
|
99
|
+
def value=(val)
|
100
|
+
super("custom #{val}")
|
101
|
+
end
|
102
|
+
end
|
93
103
|
|
94
|
-
|
104
|
+
I18n::Backend::ActiveRecord.configure do |config|
|
105
|
+
config.translation_model = MyTranslation
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
## Missing Translations
|
95
110
|
|
96
|
-
|
111
|
+
To make the `I18n::Backend::ActiveRecord::Missing` module working correctly pluralization rules should be configured properly.
|
97
112
|
The `i18n.plural.keys` translation key should be present in any of the backends.
|
98
113
|
See https://github.com/svenfuchs/i18n-active_record/blob/master/lib/i18n/backend/active_record/missing.rb for more information.
|
99
114
|
|
@@ -4,11 +4,12 @@ module I18n
|
|
4
4
|
module Backend
|
5
5
|
class ActiveRecord
|
6
6
|
class Configuration
|
7
|
-
attr_accessor :cleanup_with_destroy, :cache_translations
|
7
|
+
attr_accessor :cleanup_with_destroy, :cache_translations, :translation_model
|
8
8
|
|
9
9
|
def initialize
|
10
10
|
@cleanup_with_destroy = false
|
11
11
|
@cache_translations = false
|
12
|
+
@translation_model = I18n::Backend::ActiveRecord::Translation
|
12
13
|
end
|
13
14
|
end
|
14
15
|
end
|
@@ -36,13 +36,14 @@ module I18n
|
|
36
36
|
class ActiveRecord
|
37
37
|
module Missing
|
38
38
|
include Flatten
|
39
|
+
include TranslationModel
|
39
40
|
|
40
41
|
def store_default_translations(locale, key, options = {})
|
41
42
|
count, scope, _, separator = options.values_at(:count, :scope, :default, :separator)
|
42
43
|
separator ||= I18n.default_separator
|
43
44
|
key = normalize_flat_keys(locale, key, scope, separator)
|
44
45
|
|
45
|
-
return if
|
46
|
+
return if translation_model.locale(locale).lookup(key).exists?
|
46
47
|
|
47
48
|
interpolations = options.keys - I18n::RESERVED_KEYS
|
48
49
|
keys = count ? I18n.t('i18n.plural.keys', locale: locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
|
@@ -50,7 +51,7 @@ module I18n
|
|
50
51
|
end
|
51
52
|
|
52
53
|
def store_default_translation(locale, key, interpolations)
|
53
|
-
translation =
|
54
|
+
translation = translation_model.new locale: locale.to_s, key: key
|
54
55
|
translation.interpolations = interpolations
|
55
56
|
translation.save
|
56
57
|
end
|
@@ -23,6 +23,8 @@ module I18n
|
|
23
23
|
module Backend
|
24
24
|
class ActiveRecord
|
25
25
|
module StoreProcs
|
26
|
+
extend TranslationModel
|
27
|
+
|
26
28
|
def value=(val)
|
27
29
|
case val
|
28
30
|
when Proc
|
@@ -33,7 +35,7 @@ module I18n
|
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
|
38
|
+
translation_model.send(:include, self) if method(:to_s).respond_to?(:to_ruby)
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
@@ -25,12 +25,21 @@ module I18n
|
|
25
25
|
reload!
|
26
26
|
end
|
27
27
|
|
28
|
+
module TranslationModel
|
29
|
+
private
|
30
|
+
|
31
|
+
def translation_model
|
32
|
+
I18n::Backend::ActiveRecord.config.translation_model
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
28
36
|
module Implementation
|
29
37
|
include Base
|
30
38
|
include Flatten
|
39
|
+
include TranslationModel
|
31
40
|
|
32
41
|
def available_locales
|
33
|
-
|
42
|
+
translation_model.available_locales
|
34
43
|
rescue ::ActiveRecord::StatementInvalid
|
35
44
|
[]
|
36
45
|
end
|
@@ -39,7 +48,7 @@ module I18n
|
|
39
48
|
escape = options.fetch(:escape, true)
|
40
49
|
|
41
50
|
flatten_translations(locale, data, escape, false).each do |key, value|
|
42
|
-
translation =
|
51
|
+
translation = translation_model.locale(locale).lookup(expand_keys(key))
|
43
52
|
|
44
53
|
if self.class.config.cleanup_with_destroy
|
45
54
|
translation.destroy_all
|
@@ -47,7 +56,7 @@ module I18n
|
|
47
56
|
translation.delete_all
|
48
57
|
end
|
49
58
|
|
50
|
-
|
59
|
+
translation_model.create(locale: locale.to_s, key: key.to_s, value: value)
|
51
60
|
end
|
52
61
|
|
53
62
|
reload! if self.class.config.cache_translations
|
@@ -64,7 +73,7 @@ module I18n
|
|
64
73
|
end
|
65
74
|
|
66
75
|
def init_translations
|
67
|
-
@translations =
|
76
|
+
@translations = translation_model.to_h
|
68
77
|
end
|
69
78
|
|
70
79
|
def translations(do_init: false)
|
@@ -86,9 +95,9 @@ module I18n
|
|
86
95
|
end
|
87
96
|
|
88
97
|
result = if key == ''
|
89
|
-
|
98
|
+
translation_model.locale(locale).all
|
90
99
|
else
|
91
|
-
|
100
|
+
translation_model.locale(locale).lookup(key)
|
92
101
|
end
|
93
102
|
|
94
103
|
if result.empty?
|
data/test/active_record_test.rb
CHANGED
@@ -172,4 +172,23 @@ class I18nBackendActiveRecordTest < I18n::TestCase
|
|
172
172
|
I18n.t('foo')
|
173
173
|
end
|
174
174
|
end
|
175
|
+
|
176
|
+
class WithCustomTranslationModel < I18nBackendActiveRecordTest
|
177
|
+
class CustomTranslation < I18n::Backend::ActiveRecord::Translation
|
178
|
+
def value=(val)
|
179
|
+
super("custom #{val}")
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def setup
|
184
|
+
super
|
185
|
+
|
186
|
+
I18n::Backend::ActiveRecord.config.translation_model = CustomTranslation
|
187
|
+
end
|
188
|
+
|
189
|
+
test 'use a custom model' do
|
190
|
+
store_translations(:en, foo: 'foo')
|
191
|
+
assert_equal I18n.t(:foo), 'custom foo'
|
192
|
+
end
|
193
|
+
end
|
175
194
|
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.
|
4
|
+
version: 1.3.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: 2023-
|
11
|
+
date: 2023-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|