Skip to content

Commit 541c00c

Browse files
author
Konstantinos Bairaktaris
committed
Support javascript expressions with strings in CLI
eg `<T username="Bill">hello {'{username}'}</T>`
1 parent b98443a commit 541c00c

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/cli/src/api/parsers/babel.js

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ function toStr(children, counter = 0) {
7575
// Child is not a React element, append as-is
7676
const chunk = child.value.trim();
7777
if (chunk) { result.push(chunk); }
78+
} else if (
79+
child.type === 'JSXExpressionContainer'
80+
&& child.expression.type === 'StringLiteral'
81+
) {
82+
const chunk = child.expression.value.trim();
83+
if (chunk) { result.push(chunk); }
7884
} else {
7985
return [[], 0];
8086
}

packages/react/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,10 @@ mix of text and React elements:
130130

131131
```javascript
132132
<T>
133-
A <button>button</button> and a <b>bold</b> walk into a bar
133+
A <button title="button!!!">button</button> and a <b>bold</b> walk into a bar
134134
</T>
135135
```
136136

137-
You must not inject any javascript code in the content of a T-component because:
138-
139-
1. It will be rendered differently every time and the SDK won't be able to
140-
predictably find a translation
141-
2. The CLI will not be able to extract a source string from it
142-
143137
If you do this, the string that will be sent to Transifex for translation will
144138
look like this:
145139

@@ -151,6 +145,12 @@ As long as the translation respects the numbered tags, the T-component will
151145
render the translation properly. Any props that the React elements have in the
152146
source version of the text will be applied to the translation as well.
153147

148+
You must not inject any javascript code in the content of a T-component because:
149+
150+
1. It will be rendered differently every time and the SDK won't be able to
151+
predictably find a translation
152+
2. The CLI will not be able to extract a source string from it
153+
154154
You can interpolate parameters as before, but you have to be careful with how
155155
you define them in the source body:
156156

0 commit comments

Comments
 (0)