Skip to content

Commit

Permalink
WIP of initializing fallbacks on start
Browse files Browse the repository at this point in the history
As pointed out in shioyama#481, I18n fallbacks [are lazy-initialized by
default](https://github.com/ruby-i18n/i18n/blob/4709391dceab9096d5988576f93935843023a6ef/lib/i18n/locale/fallbacks.rb#L71).
This can be problematic in production environments, particularly with
forking appservers, since:
1. The first request pays the initialization cost
2. Memory usage is duplicated across appserver processes rather than
   taking advantage of copy-on-write

This is a draft of a way to mitigate these problems. Readability isn't
great since the `i18n` library doesn't seem to have an API to explicitly
initialize fallbacks. Instead we're just looping through each locale and
referencing it so that `i18n` will initialize it.

Considerations:
1. We probably don't want this behavior in development, how should we
   account for that?
2. Ideally this behavior would be configurable, what is the right place
   for that configuration to live? This could potentially address (1).
3. Would this be considered a breaking change to have turned on by
   default?
  • Loading branch information
f1sherman committed Dec 23, 2020
1 parent ff0e52d commit b811e7f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/mobility/plugins/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ module Fallbacks

def generate_fallbacks(fallbacks)
fallbacks_class = I18n.respond_to?(:fallbacks) ? I18nFallbacks : I18n::Locale::Fallbacks
fallbacks_class.new(fallbacks)
fallbacks_class.new(fallbacks).tap do |fallbacks_instance|
fallbacks.each_key do |locale|
# Initialize fallbacks
fallbacks_instance[locale]
end
end
end

class I18nFallbacks < ::I18n::Locale::Fallbacks
Expand Down

0 comments on commit b811e7f

Please sign in to comment.