Skip to content
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

Implement I18n.eager_load! #469

Merged
merged 1 commit into from
Feb 24, 2019
Merged

Implement I18n.eager_load! #469

merged 1 commit into from
Feb 24, 2019

Conversation

casperisfine
Copy link

Depending on the amount of translation keys and locales, loading the translation data can be quite slow (~2s in our case).

Because of this, if you are in a production web app context, you do want to trigger the loading during boot time so that the first user to hit that code path won't be slowed down.

Rails has a convention for this, if your namespace respond to eager_load! you can simply add it to Rails.application.config.eager_load_namespaces like so Rails.application.config.eager_load_namespaces << I18n.

@@ -95,10 +95,19 @@ def available_locales
end

def reload!
eager_load! if eager_loaded?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not call eager_load! here cause the same problem that you were trying to avoid with our patch? Every time a new I18n path is added we would eager load the files.

I guess this will be fine because you would only call eager_load after the load_path are all set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a trade off. Either reload! becomes eager once eager_load! was called once, and you risk wasting compute in reloading the same data multiple times if it's called afterwards.

Or either you don't and then you risk "disabling" eager loading if it's called later.

I think I prefer the former.

@casperisfine
Copy link
Author

@radar 👋 any thoughts on this PR?

@radar
Copy link
Collaborator

radar commented Feb 24, 2019

Seems like a sensible fix. Thanks for submitting it!

@radar radar merged commit 8cfff80 into ruby-i18n:master Feb 24, 2019
@JuanitoFatas JuanitoFatas deleted the eagerloading branch February 26, 2019 15:34
@JuanitoFatas JuanitoFatas restored the eagerloading branch February 26, 2019 15:34
@goldenson goldenson deleted the eagerloading branch March 5, 2019 15:26
@goldenson goldenson restored the eagerloading branch March 5, 2019 15:26
@franzliedke
Copy link

franzliedke commented May 20, 2019

@casperisfine Interesting change, thank you!

So, in a Rails context, this would be used by adding config.eager_load_namespaces << I18n to the production environment config?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants