Configure Rails application i18n configuration in railtie #314
+78
−21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What are you trying to accomplish?
In the context of a Rails application, call
Worldwide::Config.configure_i18n
with the application's i18n config instance instead of the statically-availableI18n.config
object. Avoid configuring the same i18n config instance more than once.What approach did you choose and why?
Configure Rails application's existing i18n state
Invoke
Worldwide::Config.configure_i18n
in the gem railtie's initializer block, passing in the application'sconfig.i18n
instance.Only configure an i18n config instance once
Maintain a class instance variable in
Worldwide::Config
that remembers each i18n config object that was successfully configured by theconfigure_i18n
method. Subsequent calls toconfigure_i18n
that operate on the same i18n instance will return early.Calling
Worldwide::Config.configure_i18n
with the same i18n config instance is not technically idempotent, as the method also accepts an optionaladditional_components
parameter. This example shows the limitations of my implementation:What should reviewers focus on?
I chose to use
a setan array that remembers previously-seen i18n configs rather than try to manage their state in the context of repeated invocations toconfigure_i18n
. I did not come up with a cheap and clean way to maintain a sane set of updates to i18n configurations, so I went with this simple approach.If someone wants to run
configure_i18n
on an i18n config instance after the first invocation, they can manipulate the cache withWorldwide::Config.previously_configured_i18ns.clear
and take on the responsibility of editing the i18n config instance accordingly.Testing
Railtie testing
In a Rails app, I introduced a railtie that configured the shared i18n configs
available_locales
, ensuring that this railtie's initializer ran before Worldwide's own railie:The railties initialized in the desired order.
Avoid configuring i18n config more than once
In a large app that has both direct and transitive dependencies on Worldwide, with several configuration classes that wind up calling
Worldwide::Config.configure_i18n
on a shared i18n config instance, only the first call toconfigure_i18n
actually updated the i18n config instance.Checklist