[guides] Update missing translation example #38624
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The Rails Guides contain an example of how to raise for a missing
translation when
I18n.t
is called from anywhere, includingnon-ActionView contexts. The example builds a custom exception handler
that catches and raises any instance of
I18n::MissingTranslationData
and passes on any other exception to be handled by
I18n
as normallyhappens.
Unfortunately, while
I18n::MissingTranslationData
is what eventuallyshould be raised for a missing translation, the initial exception the
handler sees is actually
I18n::MissingTranslation
, andMissingTranslationData
is the product of calling#to_exception
onthat exception object. As a result, the example in the guides ends up
not raising for missing translations, silently resulting in the return
of the missing translation key. This issue appears to have first been captured in this issue: #13429.
This change updates the guides to with a working example, catching
I18n::MissingTranslation
which results in a raise ofI18n::MissingTranslationData
with the missing key.Close #13429