Skip to content

Commit

Permalink
(Finally) fix the issue with how JSX handles newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
kbairak committed Jul 26, 2024
1 parent 0dbcce5 commit 16e99af
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
18 changes: 15 additions & 3 deletions packages/cli/src/api/parsers/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,25 @@ function toStr(children, counter = 0) {
}
} else if (child.type === 'JSXText') {
// Child is not a React element, append as-is
const chunk = child.value.trim().replace(/\s+/g, ' ');
let chunk = child.value;

// Try to mimic how JSX is parsed in runtime React
const [startMatch] = /^[\s\n]*/.exec(child.value);
if (startMatch.includes('\n')) {
chunk = chunk.substring(startMatch.length);
}

const [endMatch] = /[\s\n]*$/.exec(child.value);
if (endMatch.includes('\n')) {
chunk = chunk.substring(0, chunk.length - endMatch.length);
}

if (chunk) { result.push(chunk); }
} else if (
child.type === 'JSXExpressionContainer'
&& child.expression.type === 'StringLiteral'
) {
const chunk = child.expression.value.trim();
const chunk = child.expression.value;
if (chunk) { result.push(chunk); }
} else {
return [[], 0];
Expand Down Expand Up @@ -195,7 +207,7 @@ function babelExtractPhrases(HASHES, source, relativeFile, options) {

if (!string && elem.name.name === 'T' && node.children && node.children.length) {
const [result] = toStr(node.children);
string = result.join(' ').trim();
string = result.join('');
}

if (!string) return;
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/api/extract.hashedkeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ describe('extractPhrases with hashed keys', () => {
string: 'Text 5',
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
},
'404d0c0fef510bc89da7bc58ef160ccc': {
string: 'Text <1> 6 </1>',
'121b687b8625b4e58ba7f36dca77ad7f': {
string: 'Text <1>6</1>',
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
},
'4f5fe2d7356c474bd2f4c03176c6bc45': {
string: 'Text <1> <2> 7 </2> </1>',
'3ed8a3c47f6a32ece9c9ae0c2a060d45': {
string: 'Text <1><2>7</2></1>',
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
},
'1ecaf4c087b894bf86987fc2972ddba7': {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/api/extract.sourcekeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ describe('extractPhrases with source keys', () => {
string: 'Text 5',
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
},
'Text <1> 6 </1>': {
string: 'Text <1> 6 </1>',
'Text <1>6</1>': {
string: 'Text <1>6</1>',
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
},
'Text <1> <2> 7 </2> </1>': {
string: 'Text <1> <2> 7 </2> </1>',
'Text <1><2>7</2></1>': {
string: 'Text <1><2>7</2></1>',
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
},
'Text 8::foo': {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ If you do this, the string that will be sent to Transifex for translation will
look like this:

```
A <1> button </1> and a <2> bold </2> walk into a bar
A <1>button</1> and a <2>bold</2> walk into a bar
```

As long as the translation respects the numbered tags, the T-component will
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/T.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function T({ _str, children, ...props }) {
if (!children) { return t(_str, props); }

const [templateArray, propsContainer] = toStr(children);
const templateString = templateArray.join(' ').trim();
const templateString = templateArray.join('');
const translation = t(templateString, props);

const result = toElement(translation, propsContainer);
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/utils/toStr.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export function toStr(children, counter = 0) {
// Child is not a React element, append as-is
/* eslint-disable no-lonely-if */
if (typeof child === 'string' || child instanceof String) {
const chunk = child.trim().replace(/\s+/g, ' ');
if (chunk) { result.push(chunk); }
if (child) { result.push(child); }
} else {
result.push(child);
}
Expand Down

0 comments on commit 16e99af

Please sign in to comment.