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

Fix unescape logic, expression props, and ICU format for Trans component #892

Merged
merged 4 commits into from
Aug 23, 2023
Merged
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
31 changes: 23 additions & 8 deletions src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,15 @@ export default class JsxLexer extends JavascriptLexer {
) {
entry[property.name.text] = false
} else {
entry[
property.name.text
] = `{${property.initializer.expression.text}}`
entry[property.name.text] = `{${
property.initializer.expression.text ||
this.cleanMultiLineCode(
sourceText.slice(
property.initializer.expression.pos,
property.initializer.expression.end
)
)
}}`
}
} else {
entry[property.name.text] = property.initializer.text
Expand All @@ -138,15 +144,20 @@ export default class JsxLexer extends JavascriptLexer {
const nodeAsString = this.nodeToString.call(this, node, sourceText)
const defaultsProp = getPropValue(tagNode, 'defaults')
let defaultValue = defaultsProp || nodeAsString
if (entry.shouldUnescape === true) {

// If `shouldUnescape` is not true, it means the value cannot contain HTML entities,
// so we need to unescape these entities now so that they can be properly rendered later
if (entry.shouldUnescape !== true) {
defaultValue = unescape(defaultValue)
}

if (defaultValue !== '') {
entry.defaultValue = defaultValue

if (!entry.key) {
entry.key = nodeAsString
// If there's no key, default to the stringified unescaped node, then to the default value:
// https://github.com/i18next/react-i18next/blob/95f9c6a7b602a7b1fd33c1ded6dcfc23a52b853b/src/TransWithoutContext.js#L337
entry.key = unescape(nodeAsString) || entry.defaultValue
}
}

Expand Down Expand Up @@ -192,15 +203,19 @@ export default class JsxLexer extends JavascriptLexer {
return elemsToString(children)
}

cleanMultiLineCode(text) {
return text
.replace(/(^(\n|\r)\s*)|((\n|\r)\s*$)/g, '')
.replace(/(\n|\r)\s*/g, ' ')
}

parseChildren(children = [], sourceText) {
return children
.map((child) => {
if (child.kind === ts.SyntaxKind.JsxText) {
return {
type: 'text',
content: child.text
.replace(/(^(\n|\r)\s*)|((\n|\r)\s*$)/g, '')
.replace(/(\n|\r)\s*/g, ' '),
content: this.cleanMultiLineCode(child.text),
}
} else if (
child.kind === ts.SyntaxKind.JsxElement ||
Expand Down
43 changes: 41 additions & 2 deletions test/lexers/jsx-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ describe('JsxLexer', () => {
done()
})

it('extracts keys from Trans elements without an i18nKey, with defaults, and without children', (done) => {
const Lexer = new JsxLexer()
// Based on https://react.i18next.com/latest/trans-component#alternative-usage-components-array
const content = `
<Trans
defaults="hello <0>{{what}}</0>"
values={{
what: "world"
}}
components={[<strong />]}
/>
`.trim()
assert.deepEqual(Lexer.extract(content), [
{
key: 'hello <0>{{what}}</0>',
defaultValue: 'hello <0>{{what}}</0>',
components: '{[<strong />]}',
values: '{{ what: "world" }}',
},
])
done()
})

it('extracts keys from Trans elements and ignores values of expressions and spaces', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans count={count}>{{ key: property }}</Trans>'
Expand Down Expand Up @@ -452,12 +475,28 @@ describe('JsxLexer', () => {
done()
})

it('does not unescape i18nKey', (done) => {
const Lexer = new JsxLexer()
const content =
'<Trans i18nKey="I&apos;m testing">I&apos;m Cielquan</Trans>'
assert.equal(Lexer.extract(content)[0].key, 'I&apos;m testing')
done()
})

it('unescapes key when i18nKey is not provided', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans>I&apos;m Cielquan</Trans>'
assert.equal(Lexer.extract(content)[0].key, "I'm Cielquan")
done()
})

it('supports the shouldUnescape options', (done) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this a little counter intuitive and confusing. I would expect that shouldUnescape would unescape the defaultValue. If I understand correctly, that's not the case in react-18next, is that right?

Also, I'm wondering if we should add a test with a i18nKey property with escaped value. wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little confusing, but I'll try to explain as I understand it.

The shouldUnescape option means that after the translation is loaded/displayed, it will be unescaped. That's why when we see the shouldUnescape option, we don't unescape when extracting, so that it can be unescaped by react-i18next when it is loaded/displayed.

Also, I think it makes sense to add a test for a i18nKey with an escaped value, nice suggestion.

const Lexer = new JsxLexer()
const content = '<Trans shouldUnescape>I&apros;m Cielquan</Trans>'
const content = '<Trans shouldUnescape>I&apos;m Cielquan</Trans>'
assert.equal(Lexer.extract(content)[0].key, "I'm Cielquan")
assert.equal(
Lexer.extract(content)[0].defaultValue,
'I&apros;m Cielquan'
'I&apos;m Cielquan'
)
done()
})
Expand Down