Skip to content

Commit 2297b26

Browse files
joelhawksleyCopilot
andcommitted
Check sidecar key existence instead of catching MissingTranslation
Use I18nBackend#exists? to decide between sidecar and global translations in both the class and instance translate methods, rather than catching I18n's internal :exception throw. This also fixes `default:` shadowing existing global translations, since the sidecar backend applied the default before the fallback could run. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: ba89fbcb-d0e9-4f12-8673-169fbe391979
1 parent b4c7916 commit 2297b26

3 files changed

Lines changed: 17 additions & 21 deletions

File tree

‎docs/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ nav_order: 6
1010

1111
## main
1212

13-
* Fix `Component.translate` raising `UncaughtThrowError` for keys missing from the sidecar translations, and `NoMethodError` for components without sidecar translations. It now falls back to the global translations, like the instance method does.
13+
* Fix `Component.translate` raising `UncaughtThrowError` for keys missing from the sidecar translations, and `NoMethodError` for components without sidecar translations. It now falls back to the global translations, like the instance method does. Fix `translate` returning the `default:` value instead of an existing global translation.
1414

1515
*Andrii Furmanets*
1616

‎lib/view_component/translatable.rb‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,11 @@ def translate(key = nil, **options)
5959
locale = options.delete(:locale) || ::I18n.locale
6060
key = __vc_i18n_key(key, options.delete(:scope))
6161

62-
if __vc_i18n_backend
63-
translated = catch(:exception) do
64-
__vc_i18n_backend.translate(locale, key, options)
65-
end
66-
67-
return translated unless translated.is_a?(::I18n::MissingTranslation)
62+
if __vc_i18n_backend&.exists?(locale, key)
63+
__vc_i18n_backend.translate(locale, key, options)
64+
else
65+
::I18n.translate(key, locale: locale, **options)
6866
end
69-
70-
# Fallback to the global translations
71-
::I18n.translate(key, locale: locale, **options)
7267
end
7368

7469
alias_method :t, :translate
@@ -111,17 +106,8 @@ def translate(key = nil, **options)
111106

112107
__vc_html_escape_translation_options!(options) if as_html
113108

114-
if key.start_with?(__vc_i18n_scope + ".")
115-
translated =
116-
catch(:exception) do
117-
__vc_i18n_backend.translate(locale, key, options)
118-
end
119-
120-
# Fallback to the global translations
121-
if translated.is_a? ::I18n::MissingTranslation
122-
return @view_context.translate(key, locale: locale, **options)
123-
end
124-
109+
if __vc_i18n_backend.exists?(locale, key)
110+
translated = __vc_i18n_backend.translate(locale, key, options)
125111
translated = __vc_html_safe_translation(translated) if as_html
126112
translated
127113
else

‎test/sandbox/test/translatable_test.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ def test_translating_from_the_class_without_sidecar_translations
181181
assert_equal "This is coming from Rails", MyComponent.translate("from.rails")
182182
end
183183

184+
def test_default_does_not_shadow_global_translations
185+
assert_equal "This is coming from Rails", TranslatableComponent.t("from.rails", default: "Default")
186+
assert_equal "Relative key from Rails", TranslatableComponent.t(".relative_rails_key", default: "Default")
187+
assert_equal "Default", TranslatableComponent.t(".missing", default: "Default")
188+
189+
assert_equal "This is coming from Rails", translate("from.rails", default: "Default")
190+
assert_equal "Relative key from Rails", translate(".relative_rails_key", default: "Default")
191+
assert_equal "Default", translate(".missing", default: "Default")
192+
end
193+
184194
def test_inheriting_and_overriding_translations
185195
render_inline(TranslatableSubclassComponent.new)
186196

0 commit comments

Comments
 (0)