Skip to content

Commit b22bd9f

Browse files
authored
Scope config option to isolate translations (#140)
* Improve CI - Add Rails 8 to test matrix - Add excludes for incompatable Ruby/Rails combinations - Fix sqlite3 dependencies for older Rails versions * Add scope config option Allow the entire backend to use translations only from a subset of the translation records.
1 parent fce95f2 commit b22bd9f

File tree

14 files changed

+120
-34
lines changed

14 files changed

+120
-34
lines changed

.github/workflows/test.yml

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,41 @@ jobs:
3636
strategy:
3737
fail-fast: true
3838
matrix:
39-
ruby: [2.5, 2.6, 2.7, '3.0', 3.1, 3.2, 'head']
40-
rails: [4, 5, 6, 7, 'head']
39+
ruby: [2.5, 2.6, 2.7, '3.0', 3.1, 3.2, 3.3, 'head']
40+
rails: [4, 5, 6, 7, 8, 'head']
4141
exclude:
42-
- ruby: 2.5
43-
rails: 7
44-
- ruby: 2.5
45-
rails: head
46-
- ruby: 2.6
47-
rails: 7
48-
- ruby: 2.6
49-
rails: head
50-
- ruby: 2.7
51-
rails: 4
52-
- ruby: '3.0'
53-
rails: 4
54-
- ruby: '3.0'
55-
rails: 5
56-
- ruby: 3.1
57-
rails: 4
58-
- ruby: 3.1
59-
rails: 5
60-
- ruby: 3.2
61-
rails: 4
62-
- ruby: 3.2
63-
rails: 5
64-
- ruby: head
65-
rails: 4
66-
- ruby: head
67-
rails: 5
42+
- { 'ruby': '2.5', 'rails': '7' }
43+
- { 'ruby': '2.5', 'rails': '8' }
44+
- { 'ruby': '2.5', 'rails': 'head' }
45+
46+
- { 'ruby': '2.6', 'rails': '7' }
47+
- { 'ruby': '2.6', 'rails': '8' }
48+
- { 'ruby': '2.6', 'rails': 'head' }
49+
50+
- { 'ruby': '2.7', 'rails': '4' }
51+
- { 'ruby': '2.7', 'rails': '8' }
52+
- { 'ruby': '2.7', 'rails': 'head' }
53+
54+
- { 'ruby': '3.0', 'rails': '4' }
55+
- { 'ruby': '3.0', 'rails': '5' }
56+
- { 'ruby': '3.0', 'rails': '8' }
57+
- { 'ruby': '3.0', 'rails': 'head' }
58+
59+
- { 'ruby': '3.1', 'rails': '4' }
60+
- { 'ruby': '3.1', 'rails': '5' }
61+
- { 'ruby': '3.1', 'rails': '8' }
62+
- { 'ruby': '3.1', 'rails': 'head' }
63+
64+
- { 'ruby': '3.2', 'rails': '4' }
65+
- { 'ruby': '3.2', 'rails': '5' }
66+
67+
- { 'ruby': '3.3', 'rails': '4' }
68+
- { 'ruby': '3.3', 'rails': '5' }
69+
70+
- { 'ruby': 'head', 'rails': '4' }
71+
- { 'ruby': 'head', 'rails': '5' }
72+
- { 'ruby': 'head', 'rails': '6' }
73+
- { 'ruby': 'head', 'rails': '7' }
6874
name: 'Ruby: ${{ matrix.ruby }}, Rails: ${{ matrix.rails }}'
6975
runs-on: ubuntu-latest
7076
env:

Appraisals

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
appraise 'rails-4' do
44
gem 'activerecord', '~> 4.2.0'
55
gem 'mysql2', '~> 0.4.10'
6-
gem 'sqlite3', '~> 1.3.13'
76
gem 'pg', '~> 0.18.0'
7+
gem 'sqlite3', '~> 1.3.13'
88
end
99

1010
appraise 'rails-5' do
1111
gem 'activerecord', '~> 5.2.0'
12+
gem 'sqlite3', '~> 1.3.13'
1213
gem 'psych', '~> 3.1'
1314
end
1415

1516
appraise 'rails-6' do
1617
gem 'activerecord', '~> 6.1.0'
18+
gem 'sqlite3', '~> 1.4.4'
1719
end
1820

1921
appraise 'rails-7' do
2022
gem 'activerecord', '~> 7.0.0'
23+
gem 'sqlite3', '~> 1.4.4'
24+
end
25+
26+
appraise 'rails-8' do
27+
gem 'activerecord', '~> 8.0.0'
2128
end
2229

2330
appraise 'rails-head' do

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ I18n::Backend::ActiveRecord.configure do |config|
8585
end
8686
```
8787

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+
8898
## Usage
8999

90100
You can now use `I18n.t('Your String')` to lookup translations in the database.

gemfiles/rails_5.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source "https://rubygems.org"
55
gem "activerecord", "~> 5.2.0"
66
gem "mysql2"
77
gem "pg"
8-
gem "sqlite3"
8+
gem "sqlite3", "~> 1.3.13"
99
gem "psych", "~> 3.1"
1010

1111
gemspec path: "../"

gemfiles/rails_6.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ source "https://rubygems.org"
55
gem "activerecord", "~> 6.1.0"
66
gem "mysql2"
77
gem "pg"
8-
gem "sqlite3"
8+
gem "sqlite3", "~> 1.4.4"
99

1010
gemspec path: "../"

gemfiles/rails_7.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ source "https://rubygems.org"
55
gem "activerecord", "~> 7.0.0"
66
gem "mysql2"
77
gem "pg"
8-
gem "sqlite3"
8+
gem "sqlite3", "~> 1.4.4"
99

1010
gemspec path: "../"

gemfiles/rails_8.gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "activerecord", "~> 8.0.0"
6+
gem "mysql2"
7+
gem "pg"
8+
gem "sqlite3"
9+
10+
gemspec path: "../"

lib/generators/i18n/active_record/templates/advanced_initializer.rb.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ end
1515
I18n::Backend::ActiveRecord.configure do |config|
1616
# config.cache_translations = true # defaults to false
1717
# config.cleanup_with_destroy = true # defaults to false
18+
# config.scope = 'app_scope' # defaults to nil, won't be used
1819
end

lib/generators/i18n/active_record/templates/migration.rb.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
22
def change
33
create_table :<%= table_name %> do |t|
4+
t.string :scope
45
t.string :locale
56
t.string :key
67
t.text :value

lib/generators/i18n/active_record/templates/simple_initializer.rb.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ I18n.backend = I18n::Backend::ActiveRecord.new
44
I18n::Backend::ActiveRecord.configure do |config|
55
# config.cache_translations = true # defaults to false
66
# config.cleanup_with_destroy = true # defaults to false
7+
# config.scope = 'app_scope' # defaults to nil, won't be used
78
end

0 commit comments

Comments
 (0)