-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from curvetips/trans-component
Support react-i18n <Trans> components
- Loading branch information
Showing
7 changed files
with
246 additions
and
32 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
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,77 @@ | ||
import parse5 from 'parse5' | ||
|
||
const jsExpr = /(.*?)({+[^]+?}+)(.*)/ | ||
|
||
function transform(node) { | ||
let output = [] | ||
delete node.parentNode | ||
if (node.nodeName === '#text') { | ||
let text = node.value | ||
let m = jsExpr.exec(node.value) | ||
if (!m) { | ||
return node | ||
} | ||
while ((m = jsExpr.exec(text))) { | ||
if (m[1]) { | ||
output.push({ | ||
nodeName: '#text', | ||
value: m[1], | ||
parentNode: node.parentNode | ||
}) | ||
} | ||
output.push({ | ||
nodeName: '#expression', | ||
value: m[2], | ||
parentNode: node.parentNode | ||
}) | ||
text = m[3] | ||
} | ||
if (text) { | ||
output.push({ | ||
nodeName: '#text', | ||
value: text, | ||
parentNode: node.parentNode | ||
}) | ||
} | ||
} else { | ||
node.childNodes = Array.prototype.concat.apply( | ||
[], | ||
node.childNodes.map(transform) | ||
) | ||
output.push(node) | ||
} | ||
return output | ||
} | ||
|
||
|
||
export function parseJSX(fragment) { | ||
const ast = parse5.parseFragment(fragment) | ||
return transform(ast)[0].childNodes | ||
} | ||
|
||
|
||
function astToText(ast) { | ||
let output = '' | ||
|
||
function walk(nodes) { | ||
nodes.forEach((node, ix) => { | ||
if (node.nodeName === '#text') { | ||
output += node.value; | ||
} else if (node.nodeName === '#expression') { | ||
output += `<${ix}>${node.value}</${ix}>` | ||
} else { | ||
output += `<${ix}>` | ||
walk(node.childNodes) | ||
output += `</${ix}>` | ||
} | ||
}) | ||
} | ||
|
||
walk(ast) | ||
return output | ||
} | ||
|
||
export default function jsxToText(fragment) { | ||
const ast = parseJSX(fragment) | ||
return astToText(ast) | ||
} |
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,12 @@ | ||
const mycomp = () => ( | ||
<Trans i18nKey="key1">Key 1 default</Trans> | ||
<Trans i18nKey="key2"> | ||
Key 2 | ||
default value | ||
</Trans> | ||
|
||
<Trans i18nKey="key3">This is a <strong>test</strong></Trans> | ||
<Trans i18nKey="key4" count={count}>You have {{count}} apples</Trans> | ||
<Trans i18nKey="key5">You have <a>one <i>very</i> bad</a> apple</Trans> | ||
<Trans i18nKey="key6">This is a <strong>{{test}}</strong></Trans> | ||
) |
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,73 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { test } from 'tap'; | ||
import jsxToText, { parseJSX } from '../src/jsx-parser'; | ||
|
||
const defaults = {}; | ||
|
||
test('JSX Parse plain text', (t) => { | ||
const ast = parseJSX('This is plain text') | ||
t.same(ast.length, 1); | ||
t.same(ast[0].nodeName, '#text') | ||
t.same(ast[0].value, 'This is plain text') | ||
t.end(); | ||
}); | ||
|
||
test('JSX Transform bare javascript expression', (t) => { | ||
const ast = parseJSX('{{name}}') | ||
t.same(ast.length, 1); | ||
t.same(ast[0].nodeName, '#expression') | ||
t.same(ast[0].value, '{{name}}') | ||
t.end(); | ||
}); | ||
|
||
test('JSX Transform leading javascript expression', (t) => { | ||
const ast = parseJSX('{{name}}, you are so fine') | ||
t.same(ast.length, 2); | ||
t.same(ast[0].nodeName, '#expression') | ||
t.same(ast[0].value, '{{name}}') | ||
t.same(ast[1].nodeName, '#text') | ||
t.same(ast[1].value, ', you are so fine') | ||
t.end(); | ||
}); | ||
|
||
test('JSX Transform trailing javascript expression', (t) => { | ||
const ast = parseJSX('My name is {{name}}') | ||
t.same(ast.length, 2); | ||
t.same(ast[0].nodeName, '#text') | ||
t.same(ast[0].value, 'My name is ') | ||
t.same(ast[1].nodeName, '#expression') | ||
t.same(ast[1].value, '{{name}}') | ||
t.end(); | ||
}); | ||
|
||
test('JSX Transform javascript expression', (t) => { | ||
const ast = parseJSX('My name is {{name}}. And you?') | ||
t.same(ast.length, 3); | ||
t.same(ast[0].nodeName, '#text') | ||
t.same(ast[0].value, 'My name is ') | ||
t.same(ast[1].nodeName, '#expression') | ||
t.same(ast[1].value, '{{name}}') | ||
t.same(ast[2].nodeName, '#text') | ||
t.same(ast[2].value, '. And you?') | ||
t.end(); | ||
}); | ||
|
||
test('JSX Nested expression', (t) => { | ||
const ast = parseJSX('Hello, <strong>{{name}}</strong>, how are you?') | ||
t.same(ast.length, 3); | ||
t.same(ast[0].nodeName, '#text') | ||
t.same(ast[1].nodeName, 'strong') | ||
t.same(ast[1].childNodes.length, 1) | ||
t.same(ast[1].childNodes[0].nodeName, '#expression') | ||
t.same(ast[1].childNodes[0].value, '{{name}}') | ||
t.same(ast[2].nodeName, '#text') | ||
t.end(); | ||
}); | ||
|
||
|
||
test('JSX to i18next', (t) => { | ||
t.same(jsxToText('Basic text'), 'Basic text') | ||
t.same(jsxToText('Hello, {{name}}'), 'Hello, <1>{{name}}</1>') | ||
t.end() | ||
}) |
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