Skip to content

Commit e617046

Browse files
committed
Do not watch translations from gems when reloading is enabled
Co-authored-by: Gannon McGibbon <[email protected]> I18n is intialized with file watchers for all translation paths when reloading is enabled. This includes translations contained within gems; which the user will not be editing in development. This adds unnecessary performance overhead. This change ensures we're only watching the files we care about. ```ruby [ "/Users/schwad/.gem/ruby/3.3.3/gems/validate_url-1.0.15/lib/locale/ar.yml", # ... "/Users/schwad/path/to/my/app/config/locales/foo/en.yml" ... ] ``` [ "/Users/schwad/path/to/my/app/config/locales/foo/en.yml" ... ] ```
1 parent 4fa5681 commit e617046

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

activesupport/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Optimize load time for `Railtie#initialize_i18n`. Filter `I18n.load_path`s passed to the file watcher to only those
2+
under `Rails.root`. Previously the watcher would grab all available locales, including those in gems
3+
which do not require a watcher because they won't change.
4+
5+
*Nick Schwaderer*
6+
17
* Add a `filter` option to `in_order_of` to prioritize certain values in the sorting without filtering the results
28
by these values.
39

activesupport/lib/active_support/i18n_railtie.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ def self.initialize_i18n(app)
6262

6363
if app.config.reloading_enabled?
6464
directories = watched_dirs_with_extensions(reloadable_paths)
65-
reloader = app.config.file_watcher.new(I18n.load_path.dup, directories) do
66-
I18n.load_path.keep_if { |p| File.exist?(p) }
65+
root_load_paths = I18n.load_path.select { |path| path.start_with?(Rails.root.to_s) }
66+
reloader = app.config.file_watcher.new(root_load_paths, directories) do
67+
I18n.load_path.delete_if { |p| p.start_with?(Rails.root.to_s) && !File.exist?(p) }
6768
I18n.load_path |= reloadable_paths.flat_map(&:existent)
6869
end
6970

0 commit comments

Comments
 (0)