-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for Custom DefaultNS typing (#1328)
* Allow for Custom DefaultNS typing * Add small usage example * Fix warns when running jest * Use new CustomTypeOptions type * Update typescript and add tests for custom types * Add deprecation warning
- Loading branch information
Showing
10 changed files
with
241 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as React from 'react'; | ||
import { Trans } from 'react-i18next'; | ||
|
||
function defaultNamespaceUsage() { | ||
return <Trans i18nKey="foo">foo</Trans>; | ||
} | ||
|
||
function namedDefaultNamespaceUsage() { | ||
return ( | ||
<Trans ns="custom" i18nKey="foo"> | ||
foo | ||
</Trans> | ||
); | ||
} | ||
|
||
function alternateNamespaceUsage() { | ||
return ( | ||
<Trans ns="alternate" i18nKey="baz"> | ||
foo | ||
</Trans> | ||
); | ||
} | ||
|
||
function arrayNamespace() { | ||
return ( | ||
<Trans ns={['alternate', 'custom']} i18nKey={['alternate:baz', 'custom:bar']}> | ||
foo | ||
</Trans> | ||
); | ||
} | ||
|
||
function expectErrorWhenNamespaceDoesNotExist() { | ||
return ( | ||
// @ts-expect-error | ||
<Trans ns="fake" i18nKey="foo"> | ||
foo | ||
</Trans> | ||
); | ||
} | ||
|
||
function expectErrorWhenKeyNotInNamespace() { | ||
return ( | ||
// @ts-expect-error | ||
<Trans ns="custom" i18nKey="fake"> | ||
foo | ||
</Trans> | ||
); | ||
} | ||
|
||
function expectErrorWhenUsingArrayNamespaceAndUnscopedKey() { | ||
return ( | ||
// @ts-expect-error | ||
<Trans ns={['custom']} i18nKey={['foo']}> | ||
foo | ||
</Trans> | ||
); | ||
} | ||
|
||
function expectErrorWhenUsingArrayNamespaceAndWrongKey() { | ||
return ( | ||
// @ts-expect-error | ||
<Trans ns={['custom']} i18nKey={['custom:fake']}> | ||
foo | ||
</Trans> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react'; | ||
import { Translation } from 'react-i18next'; | ||
|
||
function defaultNamespaceUsage() { | ||
return <Translation>{(t) => <>{t('foo')}</>}</Translation>; | ||
} | ||
|
||
function namedDefaultNamespaceUsage() { | ||
return <Translation ns="custom">{(t) => <>{t('foo')}</>}</Translation>; | ||
} | ||
|
||
function alternateNamespaceUsage() { | ||
return <Translation ns="alternate">{(t) => <>{t('baz')}</>}</Translation>; | ||
} | ||
|
||
function arrayNamespace() { | ||
return ( | ||
<Translation ns={['alternate', 'custom']}> | ||
{(t) => ( | ||
<> | ||
{t('alternate:baz')} | ||
{t('custom:foo')} | ||
</> | ||
)} | ||
</Translation> | ||
); | ||
} | ||
|
||
function expectErrorWhenNamespaceDoesNotExist() { | ||
// @ts-expect-error | ||
return <Translation ns="fake">{(t) => <>{t('foo')}</>}</Translation>; | ||
} | ||
|
||
function expectErrorWhenKeyNotInNamespace() { | ||
// @ts-expect-error | ||
return <Translation ns="custom">{(t) => <>{t('fake')}</>}</Translation>; | ||
} | ||
|
||
function expectErrorWhenUsingArrayNamespaceAndUnscopedKey() { | ||
// @ts-expect-error | ||
return <Translation ns={['custom']}>{(t) => <>{t('foo')}</>}</Translation>; | ||
} | ||
|
||
function expectErrorWhenUsingArrayNamespaceAndWrongKey() { | ||
// @ts-expect-error | ||
return <Translation ns={['custom']}>{(t) => <>{t('custom:fake')}</>}</Translation>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'react-i18next'; | ||
|
||
declare module 'react-i18next' { | ||
interface CustomTypeOptions { | ||
defaultNS: 'custom'; | ||
resources: { | ||
custom: { | ||
foo: 'foo'; | ||
bar: 'bar'; | ||
}; | ||
alternate: { | ||
baz: 'baz'; | ||
}; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"include": ["./**/*"], | ||
"exclude": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
function defaultNamespaceUsage() { | ||
const { t } = useTranslation(); | ||
|
||
return <>{t('foo')}</>; | ||
} | ||
|
||
function namedDefaultNamespaceUsage() { | ||
const [t] = useTranslation('custom'); | ||
return <>{t('bar')}</>; | ||
} | ||
|
||
function alternateNamespaceUsage() { | ||
const [t] = useTranslation('alternate'); | ||
return <>{t('baz')}</>; | ||
} | ||
|
||
function arrayNamespace() { | ||
const [t] = useTranslation(['alternate', 'custom']); | ||
return ( | ||
<> | ||
{t('alternate:baz')} | ||
{t('custom:foo')} | ||
</> | ||
); | ||
} | ||
|
||
function expectErrorWhenNamespaceDoesNotExist() { | ||
// @ts-expect-error | ||
const [t] = useTranslation('fake'); | ||
return <>{t('foo')}</>; | ||
} | ||
|
||
function expectErrorWhenKeyNotInNamespace() { | ||
const [t] = useTranslation('custom'); | ||
// @ts-expect-error | ||
return <>{t('fake')}</>; | ||
} | ||
|
||
function expectErrorWhenUsingArrayNamespaceAndUnscopedKey() { | ||
const [t] = useTranslation(['custom']); | ||
// @ts-expect-error | ||
return <>{t('foo')}</>; | ||
} | ||
|
||
function expectErrorWhenUsingArrayNamespaceAndWrongKey() { | ||
const [t] = useTranslation(['custom']); | ||
// @ts-expect-error | ||
return <>{t('custom:fake')}</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters