-
Notifications
You must be signed in to change notification settings - Fork 282
Cache key_value? lookups with pre-built flat Set per locale #709
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 all commits
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,7 +19,14 @@ def data | |||||||
| adapter_class = adapter_class.to_s | ||||||||
| adapter_class = "I18n::Tasks::Data::FileSystem" if adapter_class == "file_system" | ||||||||
| data_config.except!(:adapter, :class) | ||||||||
| ActiveSupport::Inflector.constantize(adapter_class).new data_config | ||||||||
| adapter = ActiveSupport::Inflector.constantize(adapter_class).new data_config | ||||||||
| # Wrap reload to invalidate key_value? cache | ||||||||
| task = self | ||||||||
| adapter.define_singleton_method(:reload) do | ||||||||
| task.invalidate_key_value_cache! | ||||||||
| super() | ||||||||
| end | ||||||||
| adapter | ||||||||
| end | ||||||||
| end | ||||||||
|
|
||||||||
|
|
@@ -56,13 +63,19 @@ def t_proc(locale = base_locale) | |||||||
|
|
||||||||
| # whether the value for key exists in locale (defaults: base_locale) | ||||||||
| def key_value?(key, locale = base_locale) | ||||||||
| !t(key, locale).nil? | ||||||||
| @key_value_cache ||= {} | ||||||||
|
||||||||
| @key_value_cache ||= {} | |
| @key_value_cache ||= {} | |
| locale = locale.to_s |
Copilot
AI
Feb 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key_value? previously accepted Symbol keys (they were coerced via Siblings#get(full_key.to_s)), but locale_cache.include?(key) will not match because the Set is populated with Strings. To preserve existing behavior, coerce key to a String before lookup (and ideally do the same normalization when building the set).
Copilot
AI
Feb 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key_value? now checks membership in a Set of node keys, which changes semantics for leaf nodes with value == nil. Previously key_value? returned false in that case because t(key, locale) returns nil when the stored translation value is nil (see Node#value_or_children_hash). With the current implementation, those keys will be treated as present, which can hide missing translations. Consider only adding keys for non-leaf nodes, or for leaf nodes where node.value is not nil (i.e., mimic t(...).nil? behavior).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapping
adapter.reloadviadefine_singleton_methodand callingsuper()assumes every configured data adapter implementsreloadwith a compatible signature. Sincedata.adapteris configurable, this can raiseNoMethodError(noreload) or break if an adapter definesreload(*args)later. Consider guarding withrespond_to?(:reload)and forwarding args/kwargs/block (super(*args, **kwargs, &block)), or wrapping viaModule#prependto avoid signature mismatches.