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

feat: Parse formatters for interpolated values in Trans #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/nodes-to-string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ensureArray, ensureBoolean, ensureString } from 'ensure-type';
import _get from 'lodash/get';
import { get as _get, find } from 'lodash';

const isJSXText = (node) => {
if (!node) {
Expand Down Expand Up @@ -66,7 +66,10 @@ const nodesToString = (nodes, options) => {
} if (isStringLiteral(expression)) {
memo += expression.value;
} else if (isObjectExpression(expression) && (_get(expression, 'properties[0].type') === 'Property')) {
memo += `{{${expression.properties[0].key.name}}}`;
const baseProperty = expression.properties[0].key.name;
const formatProperty = find(expression.properties, ['key.name', 'format']);
const formatter = formatProperty ? `, ${formatProperty.value.value}` : '';
memo += `{{${baseProperty + formatter}}}`;
} else {
console.error(`Unsupported JSX expression. Only static values or {{interpolation}} blocks are supported. Got ${expression.type}:`);
console.error(ensureString(options?.code).slice(node.start, node.end));
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/trans.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const Component = () => (
</Trans>
<Trans i18nKey="string-literal">This is a <strong>test</strong></Trans>
<Trans i18nKey="object-expression">This is a <strong>{{test}}</strong></Trans>
<Trans i18nKey="object-expression-with-format">Interpolation + custom formatter Trans <strong>{{test, format: 'currency(.00/w)'}}</strong></Trans>
<Trans i18nKey="arithmetic-expression">
2 + 2 = {{ result: 2 + 2 }}
</Trans>
Expand Down
2 changes: 2 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ test('Parse Trans components', () => {
'multiline-text-string': 'multiline text string',
'string-literal': 'This is a <strong>test</strong>',
'object-expression': 'This is a <1>{{test}}</1>',
'object-expression-with-format': 'Interpolation + custom formatter Trans <1>{{test, currency(.00/w)}}</1>',
'arithmetic-expression': '2 + 2 = {{result}}',
'components': 'Go to <1>Administration > Tools</1> to download administrative tools.',

Expand Down Expand Up @@ -260,6 +261,7 @@ test('Parse Trans components with fallback key', () => {
'multiline-text-string': 'multiline text string',
"string-literal": "This is a <strong>test</strong>",
"object-expression": "This is a <1>{{test}}</1>",
"object-expression-with-format": "Interpolation + custom formatter Trans <1>{{test, currency(.00/w)}}</1>",
'arithmetic-expression': '2 + 2 = {{result}}',
'components': 'Go to <1>Administration > Tools</1> to download administrative tools.',
"lorem-ipsum": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>",
Expand Down