Skip to content

Commit 6af7b96

Browse files
authored
Merge branch 'main' into register-compile-cache-hook-only-without-template-caching
2 parents 3d8e7b2 + 5713b34 commit 6af7b96

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

‎docs/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ nav_order: 6
1414

1515
*Mikael Henriksson*
1616

17+
* 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.
18+
19+
*Andrii Furmanets*
20+
1721
* Only recompile components when a template or a translation file changed, instead of before every request, when templates aren't cached. Pages render about twice as fast in development.
1822

1923
*Clément Joubert*

‎lib/view_component/translatable.rb‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +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-
__vc_i18n_backend.translate(locale, key, options)
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)
66+
end
6367
end
6468

6569
alias_method :t, :translate
@@ -102,17 +106,8 @@ def translate(key = nil, **options)
102106

103107
__vc_html_escape_translation_options!(options) if as_html
104108

105-
if key.start_with?(__vc_i18n_scope + ".")
106-
translated =
107-
catch(:exception) do
108-
__vc_i18n_backend.translate(locale, key, options)
109-
end
110-
111-
# Fallback to the global translations
112-
if translated.is_a? ::I18n::MissingTranslation
113-
return @view_context.translate(key, locale: locale, **options)
114-
end
115-
109+
if __vc_i18n_backend.exists?(locale, key)
110+
translated = __vc_i18n_backend.translate(locale, key, options)
116111
translated = __vc_html_safe_translation(translated) if as_html
117112
translated
118113
else

‎test/sandbox/test/translatable_test.rb‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,32 @@ def test_translating_from_the_class
165165
assert_equal({sidecar: "This is coming from the sidecar"}, TranslatableComponent.translate(".from"))
166166
end
167167

168+
def test_translating_from_the_class_falls_back_to_global_translations
169+
assert_equal "This is coming from Rails", TranslatableComponent.t("from.rails")
170+
assert_equal "Relative key from Rails", TranslatableComponent.t(".relative_rails_key")
171+
assert_equal "Translation missing: en.translatable_component.missing", TranslatableComponent.t(".missing")
172+
assert_equal "Default", TranslatableComponent.t(".missing", default: "Default")
173+
174+
assert_raises(I18n::MissingTranslationData) do
175+
TranslatableComponent.t(".missing", raise: true)
176+
end
177+
end
178+
179+
def test_translating_from_the_class_without_sidecar_translations
180+
assert_equal "OH NO! (you shouldn't see me)", MyComponent.t(".message")
181+
assert_equal "This is coming from Rails", MyComponent.translate("from.rails")
182+
end
183+
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+
168194
def test_inheriting_and_overriding_translations
169195
render_inline(TranslatableSubclassComponent.new)
170196

0 commit comments

Comments
 (0)