@webiny/i18n-react 0.0.0-ee-vpcs.549378cf03
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 +21 -0
- package/README.md +19 -0
- package/index.d.ts +17 -0
- package/index.js +74 -0
- package/index.js.map +1 -0
- package/package.json +45 -0
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://www.npmjs.com/package/@webiny/i18n-react)
|
|
3
|
+
[](https://www.npmjs.com/package/@webiny/i18n-react)
|
|
4
|
+
[](https://github.com/prettier/prettier)
|
|
5
|
+
[](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?: string;
|
|
11
|
+
children?: React.ReactNode;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
declare const processor: Processor;
|
|
17
|
+
export default processor;
|
package/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.default = void 0;
|
|
9
|
+
|
|
10
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
11
|
+
|
|
12
|
+
var _react = _interopRequireDefault(require("react"));
|
|
13
|
+
|
|
14
|
+
var _trim = _interopRequireDefault(require("lodash/trim"));
|
|
15
|
+
|
|
16
|
+
var processTextPart = function processTextPart(part, values, modifiers) {
|
|
17
|
+
if (part.startsWith("{") === false) {
|
|
18
|
+
return part;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
part = (0, _trim.default)(part, "{}");
|
|
22
|
+
var parts = part.split("|");
|
|
23
|
+
|
|
24
|
+
var _parts = (0, _slicedToArray2.default)(parts, 2),
|
|
25
|
+
variable = _parts[0],
|
|
26
|
+
modifier = _parts[1];
|
|
27
|
+
|
|
28
|
+
if (!values[variable]) {
|
|
29
|
+
return variable;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
var value = values[variable];
|
|
33
|
+
|
|
34
|
+
if (modifier) {
|
|
35
|
+
var parameters = modifier.split(":");
|
|
36
|
+
var name = parameters.shift();
|
|
37
|
+
|
|
38
|
+
if (!name) {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (modifiers[name]) {
|
|
43
|
+
var _modifier = modifiers[name];
|
|
44
|
+
value = _modifier.execute(value, parameters);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
var processor = {
|
|
52
|
+
name: "react",
|
|
53
|
+
canExecute: function canExecute(data) {
|
|
54
|
+
for (var key in data.values) {
|
|
55
|
+
var value = data.values[key];
|
|
56
|
+
|
|
57
|
+
if ( /*#__PURE__*/_react.default.isValidElement(value)) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return false;
|
|
63
|
+
},
|
|
64
|
+
execute: function execute(data) {
|
|
65
|
+
var parts = data.translation.split(/({.*?})/);
|
|
66
|
+
return /*#__PURE__*/_react.default.createElement("i18n-text", null, parts.map(function (part, index) {
|
|
67
|
+
return /*#__PURE__*/_react.default.createElement("i18n-text-part", {
|
|
68
|
+
key: String(index)
|
|
69
|
+
}, processTextPart(part, data.values, data.i18n.modifiers));
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var _default = processor;
|
|
74
|
+
exports.default = _default;
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["processTextPart","part","values","modifiers","startsWith","lodashTrim","parts","split","variable","modifier","value","parameters","name","shift","execute","processor","canExecute","data","key","React","isValidElement","translation","map","index","String","i18n"],"sources":["index.tsx"],"sourcesContent":["import React from \"react\";\nimport lodashTrim from \"lodash/trim\";\nimport { I18NDataValues, Modifier, 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?: string;\n children?: React.ReactNode;\n };\n }\n }\n}\n\nconst processTextPart = (\n part: string,\n values: I18NDataValues,\n modifiers: Record<string, Modifier>\n): string => {\n if (part.startsWith(\"{\") === false) {\n return part;\n }\n\n part = lodashTrim(part, \"{}\");\n\n const parts: string[] = part.split(\"|\");\n\n const [variable, modifier] = parts;\n\n if (!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 (!name) {\n return value;\n }\n if (modifiers[name]) {\n const modifier = modifiers[name];\n value = modifier.execute(value, parameters);\n }\n }\n\n return value;\n};\n\nconst processor: Processor = {\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 return (\n <i18n-text>\n {parts.map((part, index) => (\n <i18n-text-part key={String(index)}>\n {processTextPart(part, data.values, data.i18n.modifiers)}\n </i18n-text-part>\n ))}\n </i18n-text>\n );\n }\n};\nexport default processor;\n"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAmBA,IAAMA,eAAe,GAAG,SAAlBA,eAAkB,CACpBC,IADoB,EAEpBC,MAFoB,EAGpBC,SAHoB,EAIX;EAC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I,WAAL,CAAiBd,KAAjB,CAAuB,SAAvB,CAAd;IACA,oBACI,gDACKD,KAAK,CAACgB,GAAN,CAAU,UAACrB,IAAD,EAAOsB,KAAP;MAAA,oBACP;QAAgB,GAAG,EAAEC,MAAM,CAACD,KAAD;MAA3B,GACKvB,eAAe,CAACC,IAAD,EAAOgB,IAAI,CAACf,MAAZ,EAAoBe,IAAI,CAACQ,IAAL,CAAUtB,SAA9B,CADpB,CADO;IAAA,CAAV,CADL,CADJ;EASH;AAvBwB,CAA7B;eAyBeY,S"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/i18n-react",
|
|
3
|
+
"version": "0.0.0-ee-vpcs.549378cf03",
|
|
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.19.0",
|
|
18
|
+
"@webiny/i18n": "0.0.0-ee-vpcs.549378cf03",
|
|
19
|
+
"lodash": "4.17.21"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@babel/cli": "^7.19.3",
|
|
23
|
+
"@babel/core": "^7.19.3",
|
|
24
|
+
"@babel/preset-env": "^7.19.4",
|
|
25
|
+
"@babel/preset-react": "^7.16.0",
|
|
26
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
27
|
+
"@webiny/cli": "^0.0.0-ee-vpcs.549378cf03",
|
|
28
|
+
"@webiny/project-utils": "^0.0.0-ee-vpcs.549378cf03",
|
|
29
|
+
"babel-plugin-lodash": "^3.3.4",
|
|
30
|
+
"rimraf": "^3.0.2",
|
|
31
|
+
"typescript": "4.7.4"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": "^17.0.2"
|
|
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": "549378cf03fcd27845fc3fa23d1dc6b32896f630"
|
|
45
|
+
}
|