@webiny/i18n-react 5.15.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @webiny/i18n-react
2
+ [![](https://img.shields.io/npm/dw/@webiny/i18n-react.svg)](https://www.npmjs.com/package/@webiny/i18n-react)
3
+ [![](https://img.shields.io/npm/v/@webiny/i18n-react.svg)](https://www.npmjs.com/package/@webiny/i18n-react)
4
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
+
7
+ A simple [webiny-i18n](./../webiny-i18n) processor, recommended for
8
+ React apps.
9
+
10
+ ## Install
11
+ ```
12
+ npm install --save @webiny/i18n-react
13
+ ```
14
+
15
+ Or if you prefer yarn:
16
+ ```
17
+ yarn add @webiny/i18n-react
18
+ ```
19
+
package/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import React from "react";
2
+ import { Processor } from "@webiny/i18n/types";
3
+ declare global {
4
+ namespace JSX {
5
+ interface IntrinsicElements {
6
+ "i18n-text": {
7
+ children?: React.ReactNode;
8
+ };
9
+ "i18n-text-part": {
10
+ key?: any;
11
+ children?: React.ReactNode;
12
+ };
13
+ }
14
+ }
15
+ }
16
+ declare const _default: Processor;
17
+ export default _default;
package/index.js ADDED
@@ -0,0 +1,61 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import _has from "lodash/has";
3
+ import _trim from "lodash/trim";
4
+ import _startsWith from "lodash/startsWith";
5
+ import React from "react";
6
+
7
+ var processTextPart = function processTextPart(part, values, modifiers) {
8
+ if (!_startsWith(part, "{")) {
9
+ return part;
10
+ }
11
+
12
+ part = _trim(part, "{}");
13
+ var parts = part.split("|");
14
+
15
+ var _parts = _slicedToArray(parts, 2),
16
+ variable = _parts[0],
17
+ modifier = _parts[1];
18
+
19
+ if (!_has(values, variable)) {
20
+ return variable;
21
+ }
22
+
23
+ var value = values[variable];
24
+
25
+ if (modifier) {
26
+ var parameters = modifier.split(":");
27
+ var name = parameters.shift();
28
+
29
+ if (modifiers[name]) {
30
+ var _modifier = modifiers[name];
31
+ value = _modifier.execute(value, parameters);
32
+ }
33
+ }
34
+
35
+ return value;
36
+ };
37
+
38
+ export default {
39
+ name: "react",
40
+ canExecute: function canExecute(data) {
41
+ for (var key in data.values) {
42
+ var value = data.values[key];
43
+
44
+ if ( /*#__PURE__*/React.isValidElement(value)) {
45
+ return true;
46
+ }
47
+ }
48
+
49
+ return false;
50
+ },
51
+ execute: function execute(data) {
52
+ var parts = data.translation.split(/({.*?})/); // @ts-ignore
53
+
54
+ return /*#__PURE__*/React.createElement("i18n-text", null, parts.map(function (part, index) {
55
+ return /*#__PURE__*/React.createElement("i18n-text-part", {
56
+ key: index
57
+ }, processTextPart(part, data.values, data.i18n.modifiers));
58
+ }));
59
+ }
60
+ };
61
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.tsx"],"names":["React","processTextPart","part","values","modifiers","parts","split","variable","modifier","value","parameters","name","shift","execute","canExecute","data","key","isValidElement","translation","map","index","i18n"],"mappings":";;;;AACA,OAAOA,KAAP,MAAkB,OAAlB;;AAmBA,IAAMC,eAAe,GAAG,SAAlBA,eAAkB,CAACC,IAAD,EAAeC,MAAf,EAA4BC,SAA5B,EAA+C;AACnE,MAAI,CAAC,YAAaF,IAAb,EAAmB,GAAnB,CAAL,EAA8B;AAC1B,WAAOA,IAAP;AACH;;AAEDA,EAAAA,IAAI,GAAG,MAAOA,IAAP,EAAa,IAAb,CAAP;AAEA,MAAMG,KAAK,GAAGH,IAAI,CAACI,KAAL,CAAW,GAAX,CAAd;;AAEA,8BAA6BD,KAA7B;AAAA,MAAOE,QAAP;AAAA,MAAiBC,QAAjB;;AAEA,MAAI,CAAC,KAAML,MAAN,EAAcI,QAAd,CAAL,EAA8B;AAC1B,WAAOA,QAAP;AACH;;AAED,MAAIE,KAAK,GAAGN,MAAM,CAACI,QAAD,CAAlB;;AACA,MAAIC,QAAJ,EAAc;AACV,QAAME,UAAU,GAAGF,QAAQ,CAACF,KAAT,CAAe,GAAf,CAAnB;AACA,QAAMK,IAAI,GAAGD,UAAU,CAACE,KAAX,EAAb;;AACA,QAAIR,SAAS,CAACO,IAAD,CAAb,EAAqB;AACjB,UAAMH,SAAQ,GAAGJ,SAAS,CAACO,IAAD,CAA1B;AACAF,MAAAA,KAAK,GAAGD,SAAQ,CAACK,OAAT,CAAiBJ,KAAjB,EAAwBC,UAAxB,CAAR;AACH;AACJ;;AAED,SAAOD,KAAP;AACH,CA1BD;;AA4BA,eAAe;AACXE,EAAAA,IAAI,EAAE,OADK;AAEXG,EAAAA,UAFW,sBAEAC,IAFA,EAEM;AACb,SAAK,IAAMC,GAAX,IAAkBD,IAAI,CAACZ,MAAvB,EAA+B;AAC3B,UAAMM,KAAK,GAAGM,IAAI,CAACZ,MAAL,CAAYa,GAAZ,CAAd;;AACA,wBAAIhB,KAAK,CAACiB,cAAN,CAAqBR,KAArB,CAAJ,EAAiC;AAC7B,eAAO,IAAP;AACH;AACJ;;AAED,WAAO,KAAP;AACH,GAXU;AAYXI,EAAAA,OAZW,mBAYHE,IAZG,EAYG;AACV,QAAMV,KAAK,GAAGU,IAAI,CAACG,WAAL,CAAiBZ,KAAjB,CAAuB,SAAvB,CAAd,CADU,CAEV;;AACA,wBACI,uCACKD,KAAK,CAACc,GAAN,CAAU,UAACjB,IAAD,EAAOkB,KAAP;AAAA,0BACP;AAAgB,QAAA,GAAG,EAAEA;AAArB,SACKnB,eAAe,CAACC,IAAD,EAAOa,IAAI,CAACZ,MAAZ,EAAoBY,IAAI,CAACM,IAAL,CAAUjB,SAA9B,CADpB,CADO;AAAA,KAAV,CADL,CADJ;AASH;AAxBU,CAAf","sourcesContent":["import _ from \"lodash\";\nimport React from \"react\";\nimport { Processor } from \"@webiny/i18n/types\";\n\ndeclare global {\n // eslint-disable-next-line\n namespace JSX {\n interface IntrinsicElements {\n \"i18n-text\": {\n children?: React.ReactNode;\n };\n\n \"i18n-text-part\": {\n key?: any;\n children?: React.ReactNode;\n };\n }\n }\n}\n\nconst processTextPart = (part: string, values: any, modifiers): any => {\n if (!_.startsWith(part, \"{\")) {\n return part;\n }\n\n part = _.trim(part, \"{}\");\n\n const parts = part.split(\"|\");\n\n const [variable, modifier] = parts;\n\n if (!_.has(values, variable)) {\n return variable;\n }\n\n let value = values[variable];\n if (modifier) {\n const parameters = modifier.split(\":\");\n const name = parameters.shift();\n if (modifiers[name]) {\n const modifier = modifiers[name];\n value = modifier.execute(value, parameters);\n }\n }\n\n return value;\n};\n\nexport default {\n name: \"react\",\n canExecute(data) {\n for (const key in data.values) {\n const value = data.values[key];\n if (React.isValidElement(value)) {\n return true;\n }\n }\n\n return false;\n },\n execute(data) {\n const parts = data.translation.split(/({.*?})/);\n // @ts-ignore\n return (\n <i18n-text>\n {parts.map((part, index) => (\n <i18n-text-part key={index}>\n {processTextPart(part, data.values, data.i18n.modifiers)}\n </i18n-text-part>\n ))}\n </i18n-text>\n );\n }\n} as Processor;\n"],"file":"index.js"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@webiny/i18n-react",
3
+ "version": "5.15.0-beta.0",
4
+ "main": "index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/webiny/webiny-js.git"
8
+ },
9
+ "description": "A simple webiny-i18n processor, recommended for React apps.",
10
+ "contributors": [
11
+ "Pavel Denisjuk <pavel@webiny.com>",
12
+ "Sven Al Hamad <sven@webiny.com>",
13
+ "Adrian Smijulj <adrian@webiny.com>"
14
+ ],
15
+ "license": "MIT",
16
+ "dependencies": {
17
+ "@babel/runtime": "7.15.3",
18
+ "@webiny/i18n": "5.15.0-beta.0",
19
+ "lodash": "4.17.21"
20
+ },
21
+ "devDependencies": {
22
+ "@babel/cli": "^7.5.5",
23
+ "@babel/core": "^7.5.5",
24
+ "@babel/preset-env": "^7.5.5",
25
+ "@babel/preset-react": "^7.0.0",
26
+ "@babel/preset-typescript": "^7.8.3",
27
+ "@webiny/cli": "^5.15.0-beta.0",
28
+ "@webiny/project-utils": "^5.15.0-beta.0",
29
+ "babel-plugin-lodash": "^3.3.4",
30
+ "rimraf": "^3.0.2",
31
+ "typescript": "^4.1.3"
32
+ },
33
+ "peerDependencies": {
34
+ "react": "16.14.0"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public",
38
+ "directory": "dist"
39
+ },
40
+ "scripts": {
41
+ "build": "yarn webiny run build",
42
+ "watch": "yarn webiny run watch"
43
+ },
44
+ "gitHead": "baf2dbb8cae1b623b59d20fedc0cd0e11a310ca1"
45
+ }