Skip to content

Commit

Permalink
Fix support for the default: option for a global translation (#1263)
Browse files Browse the repository at this point in the history
Previously the default value would have taken precedence over the
global translation fallback.
  • Loading branch information
elia authored Feb 10, 2022
1 parent 5ea40de commit 5796df9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ title: Changelog

## main

* Fix support for the `default:` option for a global translation.

*Elia Schito*

* Ensure i18n scope is a symbol to protect lookups.

*Simon Fish*
Expand Down
26 changes: 15 additions & 11 deletions lib/view_component/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,25 @@ def translate(key = nil, **options)
key = key&.to_s unless key.is_a?(String)
key = "#{i18n_scope}#{key}" if key.start_with?(".")

translated =
catch(:exception) do
i18n_backend.translate(locale, key, options)
if key.start_with?(i18n_scope + ".")
translated =
catch(:exception) do
i18n_backend.translate(locale, key, options)
end

# Fallback to the global translations
if translated.is_a? ::I18n::MissingTranslation
return super(key, locale: locale, **options)
end

# Fallback to the global translations
if translated.is_a? ::I18n::MissingTranslation
return super(key, locale: locale, **options)
end
if HTML_SAFE_TRANSLATION_KEY.match?(key)
translated = translated.html_safe # rubocop:disable Rails/OutputSafety
end

if HTML_SAFE_TRANSLATION_KEY.match?(key)
translated = translated.html_safe # rubocop:disable Rails/OutputSafety
translated
else
super(key, locale: locale, **options)
end

translated
end
alias :t :translate

Expand Down
13 changes: 11 additions & 2 deletions test/view_component/translatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def test_symbol_keys
end

def test_converts_key_to_string_as_necessary
key = Struct.new(:to_s).new(".hello")
klass = Struct.new(:to_s)
key = klass.new(".hello")
assert_equal "Hello from sidecar translations!", translate(key)
assert_equal key, translate(:"translations.missing", default: key)
end

def test_translate_marks_translations_named_html_as_safe_html
Expand Down Expand Up @@ -75,6 +75,15 @@ def test_translate_uses_the_helper_when_no_sidecar_file_is_provided
ViewComponent::CompileCache.cache.delete(TranslatableComponent)
end

def test_default
default_value = Object.new

assert_equal default_value, translate(".missing", default: default_value)
assert_equal default_value, translate("missing", default: default_value)
assert_equal "Hello from Rails translations!", translate("hello", default: default_value)
assert_equal "Hello from sidecar translations!", translate(".hello", default: default_value)
end

private

def translate(key, **options)
Expand Down

0 comments on commit 5796df9

Please sign in to comment.