Repository navigation
Add a setting to load I18n translations when an API is compiled - #3019
ericproulx wants to merge 1 commit into
Conversation
1a79281 to
f851b9a
Compare
Danger ReportNo issues found. |
Danger ReportNo issues found. |
9240107 to
1e2f663
Compare
1e2f663 to
17ea848
Compare
|
I think we should we provide |
Rails is already doing calling it through i18n. This is only when it's not a Rails app. I like the |
17ea848 to
92e64b0
Compare
Rename? |
e3f5d35 to
291702f
Compare
|
Could we expose this through an explicit The first-error improvement makes sense, but An explicit Rails already eager-loads I18n when application eager loading is enabled. Automatically invoking Grape API eager loading from Rails would need explicit integration; naming the method — AI posting on behalf of @dblock |
I18n reads its locale files on the first lookup, so in an app that precompiles its API the first error a process answered still paid for the load: 3.65 ms against 0.87 ms once the translations were in memory. Grape.config.eager_load_i18n makes compiling eager-load the I18n backend, so an app calling compile! at boot pays it there. It is off by default. Rails, where Grape is most often mounted, already eager-loads I18n when config.eager_load is on, and keeps it lazy otherwise; forcing it there would make every I18n.reload! load at once in development, and could read the translations before the app's initializers have set its load path. The backend is asked directly, as one that does not inherit I18n::Backend::Base may have no eager_load!. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
291702f to
5ce1cc2
Compare
What
A new setting,
Grape.config.eager_load_i18n(defaultfalse). When it is on,Instance#initializeeager-loads the I18n backend (when the backend answerseager_load!), so compiling an API reads the locale files instead of leaving that to the first lookup.Why
I18n loads lazily, so the first error a process answers pays for the load. Measured in a fresh process after
API.compile!: first 400 3.65 ms lazy vs 0.87 ms eager. Steady state is unchanged (0.095 vs 0.084 ms).Why off by default
Grape is most often mounted in Rails, which already handles this:
config.eager_loadon (production by default): ActiveSupport's I18n railtie registersconfig.eager_load_namespaces << I18n, so Rails eager-loads the translations at boot.config.eager_loadoff (development, test): Rails keeps I18n lazy on purpose. Forcing it there would change two things.I18n.reload!, which Rails triggers when a locale file changes.compile!in an initializer would read the translations before Rails'after_initializehas set the app's load path.An app outside Rails that precompiles its API can turn it on to move the load from its first error to boot:
Notes
Instance#initializeruns once per API build, under the compile lock, on both an explicit and a lazy compile.eager_load!: the guard asks the backend, notI18n.I18n.eager_load!just callsbackend.eager_load!, and a backend that does not inheritI18n::Backend::Basemay not have it. That is spec'd: such a backend still answers errors.I18n.load_pathwith<<after compiling is read only afterI18n.reload!; assigning the path (I18n.load_path += [path]) reloads at once. The README's I18n section says so; thecompile!section and the Configuration list point to it.Tests
eager_load!still answers.🤖 Generated with Claude Code