-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Reload I18n.load_path in development #21124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -175,6 +175,12 @@ def to_ary | |||
@paths | |||
end | |||
|
|||
def expanded_paths |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels weird to have both an expanded_paths
and an expanded
method - why can't we use expanded
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it is a red flag that we have to add this method. We can handle new files inside app without having to change Paths
, so I believe we can use the same machinery to load new locales files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Demo::Application.config.paths["config/locales"].expanded
=> ["/app/config/locales/fr.yml", "/app/config/locales/devise.en.yml", "/app/config/locales/devise_invitable.en.yml", "/app/config/locales/en.yml", "/app/config/locales/dk.yml", "/app/config/locales/simple_form.en.yml"]
expanded
returns all available files in config/locales
, and for reloading part I just need the absolute path, which would be /app/config/locales
in this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your explanations, that clears things up a lot 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any ideas how to deal with it without adding new methods to the class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see it though, we'll probably need new methods in the class. Probably like this:
def extensions
$1.split(',') if @glob =~ /\{([\S]+)\}/
end
def expand_paths_without_uniq # :nodoc:
raise "You need to set a path root" unless @root.path
map { |path| File.expand_path(path, @root.path) }
end
# Expands all paths against the root and return all unique values.
def expanded
result = []
expand_paths_without_uniq.each do |p|
if @glob && File.directory?(path)
Dir.chdir(path) do
result.concat(Dir.glob(@glob).map { |file| File.join path, file }.sort)
end
else
result << path
end
end
result.uniq!
result
end
This would definitely be cool to have. Though I wonder about the precedent we're setting. As far back as I can remember any change in |
Speaking of While not in scope for this PR, perhaps it's worthwhile to explore loading locales from different path and eventually making the default be This could be a road to making locales a first-class asset, e.g.
etc. |
I could see moving locale files to a different directory would make this reloading change easier to understand and remove doubt what's loaded in config. We'd need more feedback to decide on a change like that though. |
I'd be OK with app/locales if it wasn't a default directory. That is, if you needed it, that's where you'd set it up. It's already a very small minority of apps that need localization, so don't want to trouble app/ with another default directory that only sees rare use. |
@kaspth actually, we already reload |
Funny, I was working on a feature today that needed localization and just realized this too 😅 Lets keep the locale files where they've always been then 👍 |
@@ -4,6 +4,22 @@ | |||
|
|||
module I18n | |||
class Railtie < Rails::Railtie | |||
module Helpers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think this should be moved to the path object. Can we find a new method that's stepping stone between the simpleness of expanded_paths
and all the extra work expanded
does. To me it seems there's code there to be shared - we just need to bring out the similarities more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure we can, I just thought that you wanted to avoid changing Paths
class
@kaspth actually, I found different way of dealing with |
@kirs nice 😁 Perhaps we could |
@@ -4,6 +4,15 @@ | |||
|
|||
module I18n | |||
class Railtie < Rails::Railtie | |||
module Helpers | |||
def self.watched_dirs_with_extensions(paths) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Helpers
isn't pulling it's weight anymore. Lets just add this as a private class method in the Railtie. We control the methods there, so there's no chance of name collisions if that's what you're worried about 😄
Aside from the final comments, we can hoist ⚓ and 🚢. Well done, @kirs 👍. |
@kaspth all fixed 🚢 thanks for review! |
|
Reload I18n.load_path in development
After Rails update [1], `railties_load_path` supports `Rails::Paths::Path` objects only. Using `load_path` is the common approach, which is also used by other gems [2] [1] rails/rails#21124 [2] https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L71
After Rails update [1], railties_load_path stores Rails::Paths::Path instances only. Modifying I18n.load_path is the common approach, which is also used by other gems [2]. [1] rails/rails#21124 [2] https://github.com/rails/rails/blob/master/activemodel/lib/active_model. rb#L71 More info: rails/rails#21228 (comment) Thanks @kirs for fixing this with mislav#450 Since PRs are not currently being responded to on will_paginate, I'm porting the patch over to this fork.
After Rails update [1], `railties_load_path` supports `Rails::Paths::Path` objects only. Using `load_path` is the common approach, which is also used by other gems [2] [1] rails/rails#21124 [2] https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L71 (cherry picked from commit 6bfa748)
After Rails update [1], `railties_load_path` supports `Rails::Paths::Path` objects only. Using `load_path` is the common approach, which is also used by other gems [2] [1] rails/rails#21124 [2] https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L71
Right now, when you add new locale file in development,
rails server
should be restarted to catch new YAML files.Of course we add new locales not so often, but in huge Rails app single locale is separated by many files, like:
billing.en.yml
,admin.en.yml
,dashboard.en.yml
. This patch could be quite useful in this kind of situations.This patch also solved the case when locale file was deleted, because right now I18n would raise an error.
Generally, if we use reloading for all kinds of resources in development, why don't we use it for locales?
/cc @rafaelfranca @sirupsen