(fix): Translation component issue returning undefined instead of defaultValue #3313
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.
When the translation component is passed a defaultValue that matches an i18n key with
an object value,
i18next.t
returnsundefined
instead of the default value.Example of the issue
example en.json file
In this case, a request for
i18next.t("components", "defaultValue")
will return undefinedbut
i18next.t("components.componentDef", "defaultValue")
will return correctlyOur Translation component was then returning an empty string in place of the default value
for these translations. This PR resolves this issue by checking to see if
i18next.t
returnsundefined
and returning the default value if so.To test:
You can see this by passing a default value that matches a "category" within an i18n definition file into a
Translation
component, before this PR, that would result in an empty string, now it should result in the default value getting returnedThe easiest way to test the premise of this PR is to drop a debugger on line 10 of the
translations.js
file that this PR modifies and then to test out several different keys and defaultValues from the console.