From e61704612471ae01076ff454c57164127b088d83 Mon Sep 17 00:00:00 2001 From: Nick Schwaderer Date: Thu, 4 Jul 2024 14:09:25 +0100 Subject: [PATCH] Do not watch translations from gems when reloading is enabled Co-authored-by: Gannon McGibbon 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" ... ] ``` --- activesupport/CHANGELOG.md | 6 ++++++ activesupport/lib/active_support/i18n_railtie.rb | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 89c5e1fe2e..ae3a189f4a 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,9 @@ +* Optimize load time for `Railtie#initialize_i18n`. Filter `I18n.load_path`s passed to the file watcher to only those + under `Rails.root`. Previously the watcher would grab all available locales, including those in gems + which do not require a watcher because they won't change. + + *Nick Schwaderer* + * Add a `filter` option to `in_order_of` to prioritize certain values in the sorting without filtering the results by these values. diff --git a/activesupport/lib/active_support/i18n_railtie.rb b/activesupport/lib/active_support/i18n_railtie.rb index 2b4075a39e..6d4eb11c4e 100644 --- a/activesupport/lib/active_support/i18n_railtie.rb +++ b/activesupport/lib/active_support/i18n_railtie.rb @@ -62,8 +62,9 @@ def self.initialize_i18n(app) if app.config.reloading_enabled? directories = watched_dirs_with_extensions(reloadable_paths) - reloader = app.config.file_watcher.new(I18n.load_path.dup, directories) do - I18n.load_path.keep_if { |p| File.exist?(p) } + root_load_paths = I18n.load_path.select { |path| path.start_with?(Rails.root.to_s) } + reloader = app.config.file_watcher.new(root_load_paths, directories) do + I18n.load_path.delete_if { |p| p.start_with?(Rails.root.to_s) && !File.exist?(p) } I18n.load_path |= reloadable_paths.flat_map(&:existent) end