i18n-active_record 1.2.0 → 1.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 696d09fb79116c47ec3924b359f31ba4c632d68a075d6da1af591dcb0412548f
4
- data.tar.gz: eff1a17efd57a2f09546e80aa0d1269b07751f8615b65377c891d89186407d45
3
+ metadata.gz: a0bb9a3f4087d304d5ee5fdc38b8df8e31147a412c618fed9c82f1363db579be
4
+ data.tar.gz: b77a32f97c5d1785084a5c8332ff7e8909ce81ec6d3724ec664650540c72451d
5
5
  SHA512:
6
- metadata.gz: 8dc72a6d40f4ce0da1d98f5ade7bc26f660766aa72814771f359b9c6c8e943a4c51acfce8f612794cd3ede72c3ad9808e9442524c951fded1c2ec2457be0201f
7
- data.tar.gz: cc0fcb1c5121cefa325f52c4edbae8308a095d896cd6a8e3917d5c0144c1460f4f7795a4de6be9f25f399e678b4ce9fa2e95472e0cd651ee08aeead47e30ab03
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
- ## Missing Translations
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
- ### Usage
104
+ I18n::Backend::ActiveRecord.configure do |config|
105
+ config.translation_model = MyTranslation
106
+ end
107
+ ```
108
+
109
+ ## Missing Translations
95
110
 
96
- In order to make the `I18n::Backend::ActiveRecord::Missing` module working correctly pluralization rules should be configured properly.
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module ActiveRecord
5
- VERSION = '1.2.0'
5
+ VERSION = '1.3.0'
6
6
  end
7
7
  end
@@ -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 ActiveRecord::Translation.locale(locale).lookup(key).exists?
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 = ActiveRecord::Translation.new locale: locale.to_s, key: key
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
- Translation.send(:include, self) if method(:to_s).respond_to?(:to_ruby)
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
- Translation.available_locales
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 = Translation.locale(locale).lookup(expand_keys(key))
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
- Translation.create(locale: locale.to_s, key: key.to_s, value: value)
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 = Translation.to_h
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
- Translation.locale(locale).all
98
+ translation_model.locale(locale).all
90
99
  else
91
- Translation.locale(locale).lookup(key)
100
+ translation_model.locale(locale).lookup(key)
92
101
  end
93
102
 
94
103
  if result.empty?
@@ -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.2.0
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-08-17 00:00:00.000000000 Z
11
+ date: 2023-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n