i18n-active_record 1.3.0 → 1.4.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 +11 -1
- data/lib/generators/i18n/active_record/templates/advanced_initializer.rb.erb +1 -0
- data/lib/generators/i18n/active_record/templates/migration.rb.erb +1 -0
- data/lib/generators/i18n/active_record/templates/simple_initializer.rb.erb +1 -0
- 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/translation.rb +19 -2
- data/test/active_record_test.rb +31 -0
- data/test/test_helper.rb +2 -1
- 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: a6895e3f4b2c615221ac6a44bba495f6a6557e76fe5815fe604005e3e9d455d0
|
4
|
+
data.tar.gz: febd2054212ebeec7624de340aebc0c9f86ae4ec509f55751f97459adef82bf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fbd83d43d6158a23eb0573a598c87c23986912403496761d60b9e72950d06ff0897e6e911bb2f3d1073ab0066e17d7aa02c0f288e6b2f43bacad3f306f31625
|
7
|
+
data.tar.gz: b51858da9da92d578fe190a185e45df9779e34dfb07c5b8daf097059edf18394c83fbbb1f2add6ac72b59e8ff19342f1ba3190bb72bee322a4430e25decce5ae
|
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
|
4
|
+
It is fully compatible with Rails 4+.
|
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.
|
@@ -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
|
@@ -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
|
-
|
96
|
+
select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
|
86
97
|
end
|
87
98
|
|
88
99
|
def to_h
|
89
|
-
|
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
|
data/test/active_record_test.rb
CHANGED
@@ -191,4 +191,35 @@ class I18nBackendActiveRecordTest < I18n::TestCase
|
|
191
191
|
assert_equal I18n.t(:foo), 'custom foo'
|
192
192
|
end
|
193
193
|
end
|
194
|
+
|
195
|
+
class ScopeTest < I18nBackendActiveRecordTest
|
196
|
+
def setup
|
197
|
+
super
|
198
|
+
|
199
|
+
I18n::Backend::ActiveRecord.config.scope = 'scope1'
|
200
|
+
end
|
201
|
+
|
202
|
+
test 'scope config option divides translations into isolated sets' do
|
203
|
+
store_translations(:en, foo: 'foo1')
|
204
|
+
assert_equal('foo1', I18n.t(:foo))
|
205
|
+
|
206
|
+
I18n::Backend::ActiveRecord.config.scope = 'scope2'
|
207
|
+
store_translations(:en, foo: 'foo2')
|
208
|
+
assert_equal('foo2', I18n.t(:foo))
|
209
|
+
|
210
|
+
I18n::Backend::ActiveRecord.config.scope = 'scope1'
|
211
|
+
assert_equal('foo1', I18n.t(:foo))
|
212
|
+
end
|
213
|
+
|
214
|
+
test 'scope config of nil disables scope' do
|
215
|
+
store_translations(:en, bar1: 'bar1')
|
216
|
+
|
217
|
+
I18n::Backend::ActiveRecord.config.scope = 'scope2'
|
218
|
+
store_translations(:en, bar2: 'bar2')
|
219
|
+
|
220
|
+
I18n::Backend::ActiveRecord.config.scope = nil
|
221
|
+
assert_equal('bar1', I18n.t(:bar1))
|
222
|
+
assert_equal('bar2', I18n.t(:bar2))
|
223
|
+
end
|
224
|
+
end
|
194
225
|
end
|
data/test/test_helper.rb
CHANGED
@@ -40,13 +40,14 @@ rescue ActiveRecord::ConnectionNotEstablished
|
|
40
40
|
ActiveRecord::Migration.verbose = false
|
41
41
|
ActiveRecord::Schema.define(version: 1) do
|
42
42
|
create_table :translations, force: true do |t|
|
43
|
+
t.string :scope
|
43
44
|
t.string :locale
|
44
45
|
t.string :key
|
45
46
|
t.text :value
|
46
47
|
t.text :interpolations
|
47
48
|
t.boolean :is_proc, default: false
|
48
49
|
end
|
49
|
-
add_index :translations, %i[locale key], unique: true
|
50
|
+
add_index :translations, %i[scope locale key], unique: true
|
50
51
|
end
|
51
52
|
|
52
53
|
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.
|
4
|
+
version: 1.4.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: 2024-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|