Browse Source

Support translating object properties

master
Vitaliy Filippov 9 months ago
parent
commit
5c66f487eb
  1. 10
      README.md
  2. 10
      __tests__/__snapshots__/index-test.js.snap
  3. 5
      __tests__/index-test.js
  4. 4
      index.js
  5. 2
      package.json

10
README.md

@ -76,6 +76,16 @@ import { plural } from 'babel-plugin-react-translate/runtime';
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
Author: Vitaliy Filippov, 2021+

10
__tests__/__snapshots__/index-test.js.snap

@ -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 = /*#__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())]: {},
Родители
};"
`;

5
__tests__/index-test.js

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

4
index.js

@ -165,7 +165,9 @@ module.exports = function(babel)
addImport(path);
let repl = t.callExpression(t.identifier('L'), [ t.stringLiteral(text) ]);
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));
else
{

2
package.json

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

Loading…
Cancel
Save