Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def locale=(new_locale)

def locale
# Because I18n.locale defaults to :en, if we don't have :en in our available_locales, errors will happen
Thread.current[:faker_config_locale] || @default_locale || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
# I18n.locale_available? is used here because it relies on a memoized
# set of locales, unlike I18n.available_locales which is recomputed
# on every call.
Thread.current[:faker_config_locale] || @default_locale || (I18n.locale_available?(I18n.locale) ? I18n.locale : I18n.available_locales.first)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could this break if users have custom locales configured? I'm assuming no, but it's a good question to ask.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nope, I tested a quick little code snippet that adds a custom locale and it worked fine. The i18n gem changes the locale Set if you add anything to it, and so the memoized value invalidates and it re-grabs it.

end

def own_locale
Expand Down