From 5c66f487eb0b5d68b5be47c81874ae69130559db Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Mon, 8 Nov 2021 00:39:35 +0300 Subject: [PATCH] Support translating object properties --- README.md | 10 ++++++++++ __tests__/__snapshots__/index-test.js.snap | 10 +++++++++- __tests__/index-test.js | 5 +++++ index.js | 4 +++- package.json | 2 +- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ce71ff8..f9e5064 100644 --- a/README.md +++ b/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', {user.name})``` + +to + +`<>User {user.name} created new project` + # License and author Author: Vitaliy Filippov, 2021+ diff --git a/__tests__/__snapshots__/index-test.js.snap b/__tests__/__snapshots__/index-test.js.snap index 160efc7..c15ce11 100644 --- a/__tests__/__snapshots__/index-test.js.snap +++ b/__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())]: {}, + Родители +};" `; diff --git a/__tests__/index-test.js b/__tests__/index-test.js index 43da0eb..0e1c598 100644 --- a/__tests__/index-test.js +++ b/__tests__/index-test.js @@ -10,6 +10,7 @@ foo = "Меня зовут "+name+"! ";\ foo = ` Меня зовут ${name}!`;\ foo = Привет!;\ foo = L("У меня много {1}: ", L("братьев"));\ +var age = { "Братья": { "Иван": 32, "Михаил": 36 }, ["Сёстры"+getnum()]: {}, Родители };\ '; it('works', () => @@ -23,6 +24,10 @@ it('works', () => "Привет!", "У меня много {1}: ", "братьев", + "Братья", + "Иван", + "Михаил", + "Сёстры{1}" ] }); }); diff --git a/index.js b/index.js index 2141400..abae1e1 100644 --- a/index.js +++ b/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 { diff --git a/package.json b/package.json index 50218ac..36a99f2 100644 --- a/package.json +++ b/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": {