i18n-active_record 1.3.0 → 1.4.1

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: a0bb9a3f4087d304d5ee5fdc38b8df8e31147a412c618fed9c82f1363db579be
4
- data.tar.gz: b77a32f97c5d1785084a5c8332ff7e8909ce81ec6d3724ec664650540c72451d
3
+ metadata.gz: 26219413730c53c1670e481938d6433aadcb1efec49430b5e91dd6cba431f360
4
+ data.tar.gz: ae147b2a40b8d31fa3c4d682168fb5a95d3dd97f31b9eca47910c9f07cc87a37
5
5
  SHA512:
6
- metadata.gz: 46eb68288d4b64db1760235e72fd73a3a03e7e06d19b9f699ed4b160a423dfde3333fe92f9556fd65ed2d3f1e5d8e1e14d5ffebcfa7d7251fd37632bc3b36f44
7
- data.tar.gz: 7f1ace5489592b78a2e0dab66564837025a21cb0a2b10dbaf3999a4ea128c0c983ba8f952b867236d71554226553b2c119ad32a232cf3a96c29f62200b29b9c9
6
+ metadata.gz: 796e5d5a521f0dea36d30aed6c2044e62438633dcf062c7f0f18ad52f546bf1b69e7350dc971cac714924c50d0d372d7b978c75b214a44bdbf1bddfdc06ea9ba
7
+ data.tar.gz: bb4e08743709d44b03c749c9dab82f2fb968df78c953183c4cda43f4d66e80815d20e627500f522c3390ce7954e6d0a9c4c94eded68911737e1dbc856c476c43
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # I18n::Backend::ActiveRecord [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop) [![Tests Status](https://github.com/svenfuchs/i18n-active_record/actions/workflows/test.yml/badge.svg)](https://github.com/svenfuchs/i18n-active_record/actions) [![Linter Status](https://github.com/svenfuchs/i18n-active_record/actions/workflows/linter.yml/badge.svg)](https://github.com/svenfuchs/i18n-active_record/actions)
2
2
 
3
3
  This repository contains the I18n ActiveRecord backend and support code that has been extracted from the `I18n` gem: http://github.com/svenfuchs/i18n.
4
- It is fully compatible with Rails 4, 5 and 6.
4
+ It is fully compatible with Rails 4 and higher.
5
5
 
6
6
  ## Installation
7
7
 
@@ -85,6 +85,16 @@ I18n::Backend::ActiveRecord.configure do |config|
85
85
  end
86
86
  ```
87
87
 
88
+ The ActiveRecord backend can be configured to use a `scope` to isolate sets of translations. That way, two applications
89
+ using the backend with the same database table can use translation data independently of one another.
90
+ If configured with a scope, all data used will be limited to records with that particular scope identifier:
91
+
92
+ ```ruby
93
+ I18n::Backend::ActiveRecord.configure do |config|
94
+ config.scope = 'app1' # defaults to nil, disabling scope
95
+ end
96
+ ```
97
+
88
98
  ## Usage
89
99
 
90
100
  You can now use `I18n.t('Your String')` to lookup translations in the database.
@@ -15,4 +15,5 @@ end
15
15
  I18n::Backend::ActiveRecord.configure do |config|
16
16
  # config.cache_translations = true # defaults to false
17
17
  # config.cleanup_with_destroy = true # defaults to false
18
+ # config.scope = 'app_scope' # defaults to nil, won't be used
18
19
  end
@@ -1,6 +1,7 @@
1
1
  class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
2
  def change
3
3
  create_table :<%= table_name %> do |t|
4
+ t.string :scope
4
5
  t.string :locale
5
6
  t.string :key
6
7
  t.text :value
@@ -4,4 +4,5 @@ I18n.backend = I18n::Backend::ActiveRecord.new
4
4
  I18n::Backend::ActiveRecord.configure do |config|
5
5
  # config.cache_translations = true # defaults to false
6
6
  # config.cleanup_with_destroy = true # defaults to false
7
+ # config.scope = 'app_scope' # defaults to nil, won't be used
7
8
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module ActiveRecord
5
- VERSION = '1.3.0'
5
+ VERSION = '1.4.1'
6
6
  end
7
7
  end
@@ -4,12 +4,13 @@ module I18n
4
4
  module Backend
5
5
  class ActiveRecord
6
6
  class Configuration
7
- attr_accessor :cleanup_with_destroy, :cache_translations, :translation_model
7
+ attr_accessor :cleanup_with_destroy, :cache_translations, :translation_model, :scope
8
8
 
9
9
  def initialize
10
10
  @cleanup_with_destroy = false
11
11
  @cache_translations = false
12
12
  @translation_model = I18n::Backend::ActiveRecord::Translation
13
+ @scope = nil
13
14
  end
14
15
  end
15
16
  end
@@ -61,13 +61,24 @@ module I18n
61
61
  serialize :interpolations, Array
62
62
  end
63
63
 
64
+ before_validation :set_scope
64
65
  after_commit :invalidate_translations_cache
65
66
 
67
+ default_scope { scoped }
68
+
66
69
  class << self
67
70
  def locale(locale)
68
71
  where(locale: locale.to_s)
69
72
  end
70
73
 
74
+ def scoped
75
+ if (record_scope = ActiveRecord.config.scope)
76
+ where(scope: record_scope)
77
+ else
78
+ all
79
+ end
80
+ end
81
+
71
82
  def lookup(keys, *separator)
72
83
  column_name = connection.quote_column_name('key')
73
84
  keys = Array(keys).map!(&:to_s)
@@ -82,11 +93,11 @@ module I18n
82
93
  end
83
94
 
84
95
  def available_locales
85
- Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
96
+ select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
86
97
  end
87
98
 
88
99
  def to_h
89
- Translation.all.each.with_object({}) do |t, memo|
100
+ all.each.with_object({}) do |t, memo|
90
101
  locale_hash = (memo[t.locale.to_sym] ||= {})
91
102
  keys = t.key.split('.')
92
103
  keys.each.with_index.inject(locale_hash) do |iterator, (key_part, index)|
@@ -129,6 +140,12 @@ module I18n
129
140
  def invalidate_translations_cache
130
141
  I18n.backend.reload! if I18n::Backend::ActiveRecord.config.cache_translations
131
142
  end
143
+
144
+ private
145
+
146
+ def set_scope
147
+ self.scope ||= ActiveRecord.config.scope if ActiveRecord.config.scope
148
+ end
132
149
  end
133
150
  end
134
151
  end
@@ -91,7 +91,7 @@ module I18n
91
91
  if self.class.config.cache_translations
92
92
  keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)
93
93
 
94
- return translations.dig(*keys)
94
+ return lookup_in_cache(keys)
95
95
  end
96
96
 
97
97
  result = if key == ''
@@ -112,6 +112,10 @@ module I18n
112
112
  end
113
113
  end
114
114
 
115
+ def lookup_in_cache(keys)
116
+ keys.inject(translations) { |node, part| node[part] if node.is_a?(Hash) }
117
+ end
118
+
115
119
  def build_translation_hash_by_key(lookup_key, translation)
116
120
  hash = {}
117
121
 
@@ -96,6 +96,10 @@ class I18nBackendActiveRecordTest < I18n::TestCase
96
96
  assert_equal I18n.t(:foo), { bar: { fizz: 'buzz', spuz: 'zazz' }, baz: { fizz: 'buzz' } }
97
97
  end
98
98
 
99
+ test 'lookup of a key nested under a translation returns nothing' do
100
+ assert_equal 'default', I18n.t('foo.bar.baz', default: 'default')
101
+ end
102
+
99
103
  test 'build_translation_hash_by_key' do
100
104
  translation = I18n::Backend::ActiveRecord::Translation.new(value: 'translation', key: 'foo.bar.fizz.buzz')
101
105
  expected_hash = { 'bar' => { 'fizz' => { 'buzz' => 'translation' } } }
@@ -191,4 +195,35 @@ class I18nBackendActiveRecordTest < I18n::TestCase
191
195
  assert_equal I18n.t(:foo), 'custom foo'
192
196
  end
193
197
  end
198
+
199
+ class ScopeTest < I18nBackendActiveRecordTest
200
+ def setup
201
+ super
202
+
203
+ I18n::Backend::ActiveRecord.config.scope = 'scope1'
204
+ end
205
+
206
+ test 'scope config option divides translations into isolated sets' do
207
+ store_translations(:en, foo: 'foo1')
208
+ assert_equal('foo1', I18n.t(:foo))
209
+
210
+ I18n::Backend::ActiveRecord.config.scope = 'scope2'
211
+ store_translations(:en, foo: 'foo2')
212
+ assert_equal('foo2', I18n.t(:foo))
213
+
214
+ I18n::Backend::ActiveRecord.config.scope = 'scope1'
215
+ assert_equal('foo1', I18n.t(:foo))
216
+ end
217
+
218
+ test 'scope config of nil disables scope' do
219
+ store_translations(:en, bar1: 'bar1')
220
+
221
+ I18n::Backend::ActiveRecord.config.scope = 'scope2'
222
+ store_translations(:en, bar2: 'bar2')
223
+
224
+ I18n::Backend::ActiveRecord.config.scope = nil
225
+ assert_equal('bar1', I18n.t(:bar1))
226
+ assert_equal('bar2', I18n.t(:bar2))
227
+ end
228
+ end
194
229
  end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/setup'
4
+ require 'logger' # Rails 6.1 expects it to be loaded, recent concurrent-ruby no longer does that
4
5
  require 'minitest/autorun'
5
6
  require 'mocha/minitest'
6
7
  require 'test_declarative'
@@ -29,6 +30,7 @@ rescue ActiveRecord::ConnectionNotEstablished
29
30
  ActiveRecord::Base.establish_connection(
30
31
  adapter: 'mysql2',
31
32
  database: 'i18n_unittest',
33
+ encoding: 'utf8mb4',
32
34
  username: ENV.fetch('MYSQL_USER', 'root'),
33
35
  password: ENV.fetch('MYSQL_PASSWORD', ''),
34
36
  host: '127.0.0.1'
@@ -40,13 +42,14 @@ rescue ActiveRecord::ConnectionNotEstablished
40
42
  ActiveRecord::Migration.verbose = false
41
43
  ActiveRecord::Schema.define(version: 1) do
42
44
  create_table :translations, force: true do |t|
45
+ t.string :scope
43
46
  t.string :locale
44
47
  t.string :key
45
48
  t.text :value
46
49
  t.text :interpolations
47
50
  t.boolean :is_proc, default: false
48
51
  end
49
- add_index :translations, %i[locale key], unique: true
52
+ add_index :translations, %i[scope locale key], unique: true
50
53
  end
51
54
 
52
55
  if ActiveRecord::Base.respond_to?(:yaml_column_permitted_classes=)
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.3.0
4
+ version: 1.4.1
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-09-18 00:00:00.000000000 Z
11
+ date: 2026-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n