Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): Translation component issue returning undefined instead of defaultValue #3313

Merged

Conversation

spencern
Copy link
Contributor

@spencern spencern commented Nov 17, 2017

When the translation component is passed a defaultValue that matches an i18n key with
an object value, i18next.t returns undefined instead of the default value.

Example of the issue
example en.json file

...
"components": {
	"componentDef": "Translated Component Def"
}

In this case, a request for i18next.t("components", "defaultValue") will return undefined
but i18next.t("components.componentDef", "defaultValue") will return correctly

Our 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 returns undefined 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 returned

The 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.

… defaultValue

When the translation component is passed a defaultValue that matches an i18n key with
an object value, `i18next.t` returns `undefined` instead of the default value.
Our Translation component was then returning an empty string in place of the default value
for these translations. This commit fixes that and returns the defaultValue if `i18next.t` returns `undefined`  // i18next returns 'undefined' if the default value happens to be the key for a set of definitions

Example of the issue
example en.json file
```
...
"components": {
	"componentDef": "Translated Component Def"
}
```
In this case, a request for i18next.t("components", "defaultValue") will return undefined
but i18next.t("components.componentDef", "defaultValue") will return correctly
Copy link
Contributor

@jshimko jshimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. One small code style nitpick though...

Although it probably works fine, there probably shouldn't be a space after the spread operator.

// instead of this
<span {... rest}>{defaultValue}</span>

// use this
<span {...rest}>{defaultValue}</span>

Not sure what the general consensus is on that, but I've personally never seen someone use a space there. And all MDN examples don't have a space.

@spencern
Copy link
Contributor Author

@jshimko good catch. Fixed.

@spencern spencern changed the base branch from master to release-1.5.9 November 20, 2017 23:34
@spencern spencern merged commit 6c363e8 into release-1.5.9 Nov 20, 2017
@spencern spencern mentioned this pull request Nov 21, 2017
@spencern spencern deleted the fix-unreported-spencer-translation-component-undefined branch November 28, 2017 00:19
Akarshit pushed a commit that referenced this pull request Jan 7, 2018
…-translation-component-undefined

(fix): Translation component issue returning undefined instead of defaultValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants