Do not watch translations from gems when reloading is enabled

Co-authored-by: Gannon McGibbon <gannon@hey.com>

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"
  ...
]
```
This commit is contained in:
Nick Schwaderer 2024-07-04 14:09:25 +01:00
parent 4fa56814f1
commit e617046124
2 changed files with 9 additions and 2 deletions

@ -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.

@ -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