-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
t()
with components passed as interpolation variables (#7)
* Add failing test of t() with component as interpolation variables * Merge branch 'main' into component-as-interpolation * Add and use function that supports nested interpolated * Safe operator * Switch to switch statement. Update comments * Fix a bug and add tests * add changeset * Remove JSX * Modify rollup config * Update method description * Update .gitignore * Remove dynamic react import * Update comments and rename var * Update src/utils.js Co-authored-by: Trish Rempel <[email protected]> --------- Co-authored-by: Robert Perry <[email protected]> Co-authored-by: Robert Perry <[email protected]>
- Loading branch information
1 parent
26d90d9
commit 3d725cb
Showing
8 changed files
with
198 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/i18next-shopify': minor | ||
--- | ||
|
||
Support t() with components passed as interpolation variables |
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 |
---|---|---|
|
@@ -15,3 +15,5 @@ node_modules | |
npm-debug.log | ||
npm-debug.log.* | ||
yarn-error.log | ||
i18nextShopify.min.js | ||
i18nextShopify.js |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Utils replaceValue replaces a string with React elements 1`] = ` | ||
[ | ||
"Hello, ", | ||
<span> | ||
John | ||
</span>, | ||
"!", | ||
] | ||
`; | ||
|
||
exports[`Utils replaceValue replaces a string within nested React elements 1`] = ` | ||
<React.Fragment> | ||
Hello there | ||
<span> | ||
John | ||
</span> | ||
</React.Fragment> | ||
`; |
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,51 @@ | ||
import React from 'react'; | ||
|
||
import {replaceValue} from '../src/utils'; | ||
|
||
describe('Utils', () => { | ||
describe('replaceValue', () => { | ||
it('replaces a string with a single level of interpolation', () => { | ||
expect(replaceValue('Hello, {{name}}!', '{{name}}', 'John')).toBe( | ||
'Hello, John!', | ||
); | ||
}); | ||
|
||
it('replaces a string within an array with a single level of interpolation', () => { | ||
expect( | ||
replaceValue(['Blah! ', 'Hello, {{name}}!'], '{{name}}', 'John'), | ||
).toStrictEqual(['Blah! ', 'Hello, John!']); | ||
}); | ||
|
||
it('replaces a string with React elements', () => { | ||
const span = React.createElement('span', {}, 'John'); | ||
|
||
expect( | ||
replaceValue('Hello, {{name}}!', '{{name}}', span), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('returns the original string when there is no match with given React elements', () => { | ||
const span = React.createElement('span', {}, 'John'); | ||
|
||
expect(replaceValue('Hello, {{name}}!', '{{no_match}}', span)).toBe( | ||
'Hello, {{name}}!', | ||
); | ||
}); | ||
|
||
it('replaces a string within nested React elements', () => { | ||
const span = React.createElement('span', {key: '1'}, '{{name}}'); | ||
const fragment = React.createElement(React.Fragment, {}, [ | ||
'Hello there ', | ||
span, | ||
]); | ||
|
||
expect(replaceValue(fragment, '{{name}}', 'John')).toMatchSnapshot(); | ||
}); | ||
|
||
it('replaces a string with a regular expression', () => { | ||
expect(replaceValue('Hello, {{name}}!', /{{name}}/, 'John')).toBe( | ||
'Hello, John!', | ||
); | ||
}); | ||
}); | ||
}); |