Skip to content

Commit 122a3ac

Browse files
authored
Merge pull request #212 from 6temes/feature/auto-reload-translations-in-development
Add automatic reloading of .po/.mo files in development
2 parents 992fefa + b1c5991 commit 122a3ac

File tree

6 files changed

+83
-14
lines changed

6 files changed

+83
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
## Unreleased
44

5+
- Add automatic reloading of .po and .mo files in development mode
6+
57
## 2.0.0
8+
69
- change how model attributes are looked up (class first, then sti root)
710
- drop support for old rubies
811

912
## v1.13.0
13+
1014
- Use subclasses instead of direct_descendants on rails 7 and above
1115

1216
## v1.12.0

Gemfile.lock

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ GEM
109109
marcel (1.0.2)
110110
method_source (1.0.0)
111111
mini_mime (1.1.2)
112+
mini_portile2 (2.8.9)
112113
minitest (5.18.0)
113114
net-imap (0.3.4)
114115
date
@@ -120,11 +121,8 @@ GEM
120121
net-smtp (0.3.3)
121122
net-protocol
122123
nio4r (2.5.9)
123-
nokogiri (1.15.2-arm64-darwin)
124-
racc (~> 1.4)
125-
nokogiri (1.15.2-x86_64-darwin)
126-
racc (~> 1.4)
127-
nokogiri (1.15.2-x86_64-linux)
124+
nokogiri (1.15.2)
125+
mini_portile2 (~> 2.8.2)
128126
racc (~> 1.4)
129127
prime (0.1.2)
130128
forwardable
@@ -178,9 +176,8 @@ GEM
178176
slim (4.1.0)
179177
temple (>= 0.7.6, < 0.9)
180178
tilt (>= 2.0.6, < 2.1)
181-
sqlite3 (1.7.3-arm64-darwin)
182-
sqlite3 (1.7.3-x86_64-darwin)
183-
sqlite3 (1.7.3-x86_64-linux)
179+
sqlite3 (1.7.3)
180+
mini_portile2 (~> 2.8.0)
184181
temple (0.8.2)
185182
text (1.3.1)
186183
thor (1.2.2)
@@ -197,6 +194,7 @@ PLATFORMS
197194
arm64-darwin-21
198195
arm64-darwin-23
199196
arm64-darwin-24
197+
arm64-darwin-25
200198
x86_64-darwin-22
201199
x86_64-linux
202200

Readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,23 @@ Rails.application.config.gettext_i18n_rails.use_for_active_record_attributes = f
296296

297297
And now you can use your I18n yaml files instead.
298298

299+
Auto-reload translations in development
300+
========================================
301+
302+
By default, .po and .mo files are automatically reloaded in development mode when they change, so you don't need to restart the Rails server after editing translations.
303+
304+
This feature is enabled by default in development. You can configure it in any environment file:
305+
306+
```Ruby
307+
# To disable in development
308+
config.gettext_i18n_rails.auto_reload = false
309+
310+
# To enable in production (not recommended)
311+
config.gettext_i18n_rails.auto_reload = true
312+
```
313+
314+
The auto-reload feature uses `ActiveSupport::FileUpdateChecker` to monitor changes to translation files in your `locale/` directory and reloads them only when they've been modified, ensuring minimal performance impact.
315+
299316
Using your translations from javascript
300317
=======================================
301318

lib/gettext_i18n_rails/railtie.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Railtie < ::Rails::Railtie
55
config.gettext_i18n_rails.msgcat = nil
66
config.gettext_i18n_rails.xgettext = nil
77
config.gettext_i18n_rails.use_for_active_record_attributes = true
8+
config.gettext_i18n_rails.auto_reload = Rails.env.development?
89

910
rake_tasks do
1011
if Gem::Specification.find_all_by_name("gettext", ">= 3.0.2").any?
@@ -18,6 +19,20 @@ class Railtie < ::Rails::Railtie
1819
require 'gettext_i18n_rails/active_model'
1920
end
2021
end
22+
23+
# Auto-reload .po and .mo files when they change
24+
if app.config.gettext_i18n_rails.auto_reload
25+
po_files = Dir[Rails.root.join("locale/**/*.{po,mo}")]
26+
27+
reloader = ActiveSupport::FileUpdateChecker.new(po_files) do
28+
FastGettext.translation_repositories.each_value(&:reload)
29+
Rails.logger.info "Reloaded gettext translations"
30+
end
31+
32+
app.executor.to_run do
33+
reloader.execute_if_updated
34+
end
35+
end
2136
end
2237
end
2338
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
describe GettextI18nRails::Railtie do
4+
describe 'auto-reload configuration' do
5+
it 'can be set to true or false' do
6+
config = GettextI18nRails::Railtie.config.gettext_i18n_rails
7+
config.auto_reload = true
8+
config.auto_reload.should == true
9+
end
10+
11+
it 'can be disabled' do
12+
config = GettextI18nRails::Railtie.config.gettext_i18n_rails
13+
config.auto_reload = false
14+
config.auto_reload.should == false
15+
end
16+
end
17+
end

spec/spec_helper.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,35 @@
77
require 'tempfile'
88
require 'active_support'
99
require 'active_support/core_ext/string/output_safety'
10+
require 'rails/railtie'
1011
require 'active_record'
1112
require 'action_controller'
1213
require 'action_mailer'
1314
require 'fast_gettext'
15+
16+
# Define minimal Rails stub for library compatibility
17+
# Rails::VERSION::MAJOR is checked by action_controller.rb at load time
18+
module Rails
19+
module VERSION
20+
MAJOR = 7
21+
end
22+
23+
def self.root
24+
File.dirname(__FILE__)
25+
end
26+
27+
def self.env
28+
ActiveSupport::StringInquirer.new('test')
29+
end
30+
end
31+
1432
require 'gettext_i18n_rails'
33+
34+
# Manually load ActiveRecord/ActiveModel extensions since we're not running full Rails initialization
35+
# In a real Rails app, these would be loaded via the Railtie's after_initialize hook
36+
require 'gettext_i18n_rails/active_model'
37+
require 'gettext_i18n_rails/active_record'
38+
1539
require 'temple'
1640

1741
if ActiveSupport::VERSION::MAJOR >= 3
@@ -38,12 +62,6 @@ def method_missing(name, engine, options = {})
3862
end
3963
end
4064

41-
module Rails
42-
def self.root
43-
File.dirname(__FILE__)
44-
end
45-
end
46-
4765
def with_file(content, extension = '')
4866
Tempfile.open(['gettext_i18n_rails_specs', extension]) do |f|
4967
f.write(content)

0 commit comments

Comments
 (0)