Support translating object properties

master
Vitaliy Filippov 2021-11-08 00:39:35 +03:00
parent 4d0fe4874e
commit 5c66f487eb
5 changed files with 28 additions and 3 deletions

View File

@ -76,6 +76,16 @@ import { plural } from 'babel-plugin-react-translate/runtime';
console.log(plural(155, 'брат', 'брата', 'братьев')); console.log(plural(155, 'брат', 'брата', 'братьев'));
``` ```
# TODO
It would be interesting to support React interpolation, like:
```L('User $1 created new project', <a href="/user/1/">{user.name}</a>)```
to
`<>User <a href="/user/1/">{user.name}</a> created new project</>`
# License and author # License and author
Author: Vitaliy Filippov, 2021+ Author: Vitaliy Filippov, 2021+

View File

@ -7,5 +7,13 @@ var foo = L(\\"\\\\u0401-Mobile\\");
foo = L(\\"\\\\u041C\\\\u0435\\\\u043D\\\\u044F \\\\u0437\\\\u043E\\\\u0432\\\\u0443\\\\u0442 {1}!\\", name) + \\" \\"; foo = L(\\"\\\\u041C\\\\u0435\\\\u043D\\\\u044F \\\\u0437\\\\u043E\\\\u0432\\\\u0443\\\\u0442 {1}!\\", name) + \\" \\";
foo = \\" \\" + L(\\"\\\\u041C\\\\u0435\\\\u043D\\\\u044F \\\\u0437\\\\u043E\\\\u0432\\\\u0443\\\\u0442 {1}!\\", name); foo = \\" \\" + L(\\"\\\\u041C\\\\u0435\\\\u043D\\\\u044F \\\\u0437\\\\u043E\\\\u0432\\\\u0443\\\\u0442 {1}!\\", name);
foo = /*#__PURE__*/React.createElement(\\"span\\", null, L(\\"\\\\u041F\\\\u0440\\\\u0438\\\\u0432\\\\u0435\\\\u0442!\\")); foo = /*#__PURE__*/React.createElement(\\"span\\", null, L(\\"\\\\u041F\\\\u0440\\\\u0438\\\\u0432\\\\u0435\\\\u0442!\\"));
foo = L(\\"У меня много {1}: \\", L(\\"братьев\\"));" foo = L(\\"У меня много {1}: \\", L(\\"братьев\\"));
var age = {
[L(\\"\\\\u0411\\\\u0440\\\\u0430\\\\u0442\\\\u044C\\\\u044F\\")]: {
[L(\\"\\\\u0418\\\\u0432\\\\u0430\\\\u043D\\")]: 32,
[L(\\"\\\\u041C\\\\u0438\\\\u0445\\\\u0430\\\\u0438\\\\u043B\\")]: 36
},
[L(\\"\\\\u0421\\\\u0451\\\\u0441\\\\u0442\\\\u0440\\\\u044B{1}\\", getnum())]: {},
Родители
};"
`; `;

View File

@ -10,6 +10,7 @@ foo = "Меня зовут "+name+"! ";\
foo = ` Меня зовут ${name}!`;\ foo = ` Меня зовут ${name}!`;\
foo = <span>Привет!</span>;\ foo = <span>Привет!</span>;\
foo = L("У меня много {1}: ", L("братьев"));\ foo = L("У меня много {1}: ", L("братьев"));\
var age = { "Братья": { "Иван": 32, "Михаил": 36 }, ["Сёстры"+getnum()]: {}, Родители };\
'; ';
it('works', () => it('works', () =>
@ -23,6 +24,10 @@ it('works', () =>
"Привет!", "Привет!",
"У меня много {1}: ", "У меня много {1}: ",
"братьев", "братьев",
"Братья",
"Иван",
"Михаил",
"Сёстры{1}"
] ]
}); });
}); });

View File

@ -165,7 +165,9 @@ module.exports = function(babel)
addImport(path); addImport(path);
let repl = t.callExpression(t.identifier('L'), [ t.stringLiteral(text) ]); let repl = t.callExpression(t.identifier('L'), [ t.stringLiteral(text) ]);
repl = withWhite(lwhite, repl, rwhite); repl = withWhite(lwhite, repl, rwhite);
if (isJSX) if (parent.isObjectProperty() && !parent.node.computed)
parent.replaceWith(t.objectProperty(repl, parent.node.value, true, parent.node.shorthard, parent.node.decorators));
else if (isJSX)
path.replaceWith(t.jsxExpressionContainer(repl)); path.replaceWith(t.jsxExpressionContainer(repl));
else else
{ {

View File

@ -1,6 +1,6 @@
{ {
"name": "babel-plugin-react-translate", "name": "babel-plugin-react-translate",
"version": "1.0.4", "version": "1.0.5",
"description": "Babel-based JS/JSX auto-translator for Russian language", "description": "Babel-based JS/JSX auto-translator for Russian language",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {