-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e4ef25
commit 6fb4692
Showing
24 changed files
with
230 additions
and
58 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const React = require('react'); | ||
const reactI18next = require('react-i18next'); | ||
|
||
const hasChildren = node => node && (node.children || (node.props && node.props.children)); | ||
|
||
const getChildren = node => | ||
node && node.children ? node.children : node.props && node.props.children; | ||
|
||
const renderNodes = reactNodes => { | ||
if (typeof reactNodes === 'string') { | ||
return reactNodes; | ||
} | ||
|
||
return Object.keys(reactNodes).map((key, i) => { | ||
const child = reactNodes[key]; | ||
const isElement = React.isValidElement(child); | ||
|
||
if (typeof child === 'string') { | ||
return child; | ||
} | ||
if (hasChildren(child)) { | ||
const inner = renderNodes(getChildren(child)); | ||
return React.cloneElement(child, { ...child.props, key: i }, inner); | ||
} | ||
if (typeof child === 'object' && !isElement) { | ||
return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, ''); | ||
} | ||
|
||
return child; | ||
}); | ||
}; | ||
|
||
const useMock = [k => k, {}]; | ||
useMock.t = k => k; | ||
useMock.i18n = {}; | ||
|
||
module.exports = { | ||
// this mock makes sure any components using the translate HoC receive the t function as a prop | ||
// eslint-disable-next-line react/display-name | ||
withTranslation: () => Component => props => <Component t={k => k} {...props} />, | ||
Trans: ({ children }) => | ||
Array.isArray(children) ? renderNodes(children) : renderNodes([children]), | ||
Translation: ({ children }) => children(k => k, { i18n: {} }), | ||
useTranslation: () => useMock, | ||
|
||
// mock if needed | ||
I18nextProvider: reactI18next.I18nextProvider, | ||
initReactI18next: reactI18next.initReactI18next, | ||
setDefaults: reactI18next.setDefaults, | ||
getDefaults: reactI18next.getDefaults, | ||
setI18n: reactI18next.setI18n, | ||
getI18n: reactI18next.getI18n, | ||
}; |
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
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
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
Oops, something went wrong.