-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Performance: Cache translations to reduce file read and parse operations #2939
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
756767a
379b0b3
7233dfa
dac7cff
aa82207
559859a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,11 +39,13 @@ def random | |
| end | ||
| end | ||
|
|
||
| # rubocop:disable Style/ClassVars | ||
| class Base | ||
| Numbers = Array(0..9) | ||
| ULetters = Array('A'..'Z') | ||
| LLetters = Array('a'..'z') | ||
| Letters = ULetters + LLetters | ||
| @@translate_cache = {} | ||
|
|
||
| class << self | ||
| attr_reader :flexible_key | ||
|
|
@@ -161,8 +163,9 @@ def parse(key) | |
| # locale is specified | ||
| def translate(*args, **opts) | ||
| opts[:locale] ||= Faker::Config.locale | ||
| translate_key = args.to_s + opts.sort.join | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does sound reasonable to hash the Another interesting fact here is that |
||
| opts[:raise] = true | ||
| I18n.translate(*args, **opts) | ||
| @@translate_cache[translate_key] ||= I18n.translate(*args, **opts) | ||
| rescue I18n::MissingTranslationData | ||
| opts[:locale] = :en | ||
|
|
||
|
|
@@ -268,6 +271,7 @@ def disable_enforce_available_locales | |
| end | ||
| end | ||
| end | ||
| # rubocop:enable Style/ClassVars | ||
| end | ||
|
|
||
| # require faker objects | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.