@webiny/i18n 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/I18n.d.ts +127 -0
- package/I18n.js +445 -0
- package/I18n.js.map +1 -0
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/extractor/extract.d.ts +2 -0
- package/extractor/extract.js +62 -0
- package/extractor/extract.js.map +1 -0
- package/extractor/index.d.ts +13 -0
- package/extractor/index.js +64 -0
- package/extractor/index.js.map +1 -0
- package/index.d.ts +6 -0
- package/index.js +34 -0
- package/index.js.map +1 -0
- package/modifiers/countModifier.d.ts +3 -0
- package/modifiers/countModifier.js +43 -0
- package/modifiers/countModifier.js.map +1 -0
- package/modifiers/dateModifier.d.ts +3 -0
- package/modifiers/dateModifier.js +19 -0
- package/modifiers/dateModifier.js.map +1 -0
- package/modifiers/dateTimeModifier.d.ts +3 -0
- package/modifiers/dateTimeModifier.js +19 -0
- package/modifiers/dateTimeModifier.js.map +1 -0
- package/modifiers/genderModifier.d.ts +3 -0
- package/modifiers/genderModifier.js +19 -0
- package/modifiers/genderModifier.js.map +1 -0
- package/modifiers/ifModifier.d.ts +3 -0
- package/modifiers/ifModifier.js +19 -0
- package/modifiers/ifModifier.js.map +1 -0
- package/modifiers/index.d.ts +3 -0
- package/modifiers/index.js +31 -0
- package/modifiers/index.js.map +1 -0
- package/modifiers/numberModifier.d.ts +3 -0
- package/modifiers/numberModifier.js +19 -0
- package/modifiers/numberModifier.js.map +1 -0
- package/modifiers/pluralModifier.d.ts +3 -0
- package/modifiers/pluralModifier.js +42 -0
- package/modifiers/pluralModifier.js.map +1 -0
- package/modifiers/priceModifier.d.ts +3 -0
- package/modifiers/priceModifier.js +19 -0
- package/modifiers/priceModifier.js.map +1 -0
- package/modifiers/timeModifier.d.ts +3 -0
- package/modifiers/timeModifier.js +19 -0
- package/modifiers/timeModifier.js.map +1 -0
- package/package.json +51 -0
- package/processors/default.d.ts +3 -0
- package/processors/default.js +69 -0
- package/processors/default.js.map +1 -0
- package/types.d.ts +56 -0
- package/types.js +5 -0
- package/types.js.map +1 -0
|
@@ -0,0 +1,69 @@
|
|
|
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 _trim = _interopRequireDefault(require("lodash/trim"));
|
|
11
|
+
|
|
12
|
+
const processTextPart = (part, values, modifiers) => {
|
|
13
|
+
if (part.startsWith("{") === false) {
|
|
14
|
+
return part;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const parts = (0, _trim.default)(part, "{}").split("|");
|
|
18
|
+
const [variable, modifier] = parts;
|
|
19
|
+
|
|
20
|
+
if (!values[variable]) {
|
|
21
|
+
return `{${variable}}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const output = {
|
|
25
|
+
value: values[variable]
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
if (modifier) {
|
|
29
|
+
const parameters = modifier.split(":");
|
|
30
|
+
const name = parameters.shift();
|
|
31
|
+
|
|
32
|
+
if (!name) {
|
|
33
|
+
return output.value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (modifiers[name]) {
|
|
37
|
+
const modifier = modifiers[name];
|
|
38
|
+
output.value = modifier.execute(output.value, parameters);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return output.value;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const processor = {
|
|
46
|
+
name: "default",
|
|
47
|
+
|
|
48
|
+
canExecute(data) {
|
|
49
|
+
for (const key in data.values) {
|
|
50
|
+
const value = data.values[key];
|
|
51
|
+
|
|
52
|
+
if (typeof value === "string" || typeof value === "number" || value === null || value instanceof Date) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return true;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
execute(data) {
|
|
63
|
+
const parts = data.translation.split(/({.*?})/);
|
|
64
|
+
return parts.reduce((carry, part) => carry + processTextPart(part, data.values, data.i18n.modifiers), "");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
};
|
|
68
|
+
var _default = processor;
|
|
69
|
+
exports.default = _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["processTextPart","part","values","modifiers","startsWith","parts","lodashTrim","split","variable","modifier","output","value","parameters","name","shift","execute","processor","canExecute","data","key","Date","translation","reduce","carry","i18n"],"sources":["default.ts"],"sourcesContent":["import lodashTrim from \"lodash/trim\";\nimport { Modifier, Processor } from \"~/types\";\n\nconst processTextPart = (\n part: string,\n values: Record<string, any>,\n modifiers: Record<string, Modifier>\n): string => {\n if (part.startsWith(\"{\") === false) {\n return part;\n }\n\n const parts = lodashTrim(part, \"{}\").split(\"|\");\n\n const [variable, modifier] = parts;\n\n if (!values[variable]) {\n return `{${variable}}`;\n }\n\n const output = {\n value: values[variable]\n };\n\n if (modifier) {\n const parameters: string[] = modifier.split(\":\");\n const name = parameters.shift();\n if (!name) {\n return output.value;\n }\n if (modifiers[name]) {\n const modifier = modifiers[name];\n output.value = modifier.execute(output.value, parameters);\n }\n }\n\n return output.value;\n};\n\nconst processor: Processor = {\n name: \"default\",\n canExecute(data) {\n for (const key in data.values) {\n const value = data.values[key];\n if (\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n value === null ||\n value instanceof Date\n ) {\n continue;\n }\n return false;\n }\n\n return true;\n },\n execute(data) {\n const parts = data.translation.split(/({.*?})/);\n return parts.reduce(\n (carry, part) => carry + processTextPart(part, data.values, data.i18n.modifiers),\n \"\"\n );\n }\n};\n\nexport default processor;\n"],"mappings":";;;;;;;;;AAA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AEAA,KAAK,KAAK,IAFV,IAGAA,KAAK,YAAYS,IAJrB,EAKE;QACE;MACH;;MACD,OAAO,KAAP;IACH;;IAED,OAAO,IAAP;EACH,CAjBwB;;EAkBzBL,OAAO,CAACG,IAAD,EAAO;IACV,MAAMb,KAAK,GAAGa,IAAI,CAACG,WAAL,CAAiBd,KAAjB,CAAuB,SAAvB,CAAd;IACA,OAAOF,KAAK,CAACiB,MAAN,CACH,CAACC,KAAD,EAAQtB,IAAR,KAAiBsB,KAAK,GAAGvB,eAAe,CAACC,IAAD,EAAOiB,IAAI,CAAChB,MAAZ,EAAoBgB,IAAI,CAACM,IAAL,CAAUrB,SAA9B,CADrC,EAEH,EAFG,CAAP;EAIH;;AAxBwB,CAA7B;eA2Bea,S"}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import I18N from "./I18n";
|
|
2
|
+
import { ReactElement } from "react";
|
|
3
|
+
export interface I18NDataValues {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}
|
|
6
|
+
export interface I18NData {
|
|
7
|
+
translation: string;
|
|
8
|
+
base: string;
|
|
9
|
+
namespace: string;
|
|
10
|
+
values: I18NDataValues;
|
|
11
|
+
i18n: I18N;
|
|
12
|
+
}
|
|
13
|
+
export interface ModifierOptions {
|
|
14
|
+
i18n: I18N;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @description I18N Modifier - used for modifying text dynamically.
|
|
18
|
+
*/
|
|
19
|
+
export interface Modifier {
|
|
20
|
+
name: string;
|
|
21
|
+
execute: (...args: any[]) => string;
|
|
22
|
+
}
|
|
23
|
+
export declare type ProcessorResult = string | ReactElement;
|
|
24
|
+
/**
|
|
25
|
+
* @description I18N Processor - used for outputting text.
|
|
26
|
+
*/
|
|
27
|
+
export interface Processor {
|
|
28
|
+
name: string;
|
|
29
|
+
canExecute: (data: I18NData) => boolean;
|
|
30
|
+
execute: (data: I18NData) => ProcessorResult;
|
|
31
|
+
}
|
|
32
|
+
export interface NumberFormat {
|
|
33
|
+
decimal: string;
|
|
34
|
+
thousand: string;
|
|
35
|
+
precision: number;
|
|
36
|
+
}
|
|
37
|
+
export interface PriceFormat {
|
|
38
|
+
symbol: string;
|
|
39
|
+
format: string;
|
|
40
|
+
decimal: string;
|
|
41
|
+
thousand: string;
|
|
42
|
+
precision: number;
|
|
43
|
+
}
|
|
44
|
+
export interface Formats {
|
|
45
|
+
date: string;
|
|
46
|
+
time: string;
|
|
47
|
+
datetime: string;
|
|
48
|
+
price: PriceFormat;
|
|
49
|
+
number: NumberFormat;
|
|
50
|
+
}
|
|
51
|
+
export interface Translator {
|
|
52
|
+
(base: any): any;
|
|
53
|
+
}
|
|
54
|
+
export interface Translations {
|
|
55
|
+
[key: string]: string;
|
|
56
|
+
}
|
package/types.js
ADDED
package/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import I18N from \"./I18n\";\nimport { ReactElement } from \"react\";\n\nexport interface I18NDataValues {\n [key: string]: any;\n}\nexport interface I18NData {\n translation: string;\n base: string;\n namespace: string;\n values: I18NDataValues;\n i18n: I18N;\n}\n\nexport interface ModifierOptions {\n i18n: I18N;\n}\n/**\n * @description I18N Modifier - used for modifying text dynamically.\n */\nexport interface Modifier {\n name: string;\n execute: (...args: any[]) => string;\n}\n\nexport type ProcessorResult = string | ReactElement;\n/**\n * @description I18N Processor - used for outputting text.\n */\nexport interface Processor {\n name: string;\n canExecute: (data: I18NData) => boolean;\n execute: (data: I18NData) => ProcessorResult;\n}\n\nexport interface NumberFormat {\n decimal: string;\n thousand: string;\n precision: number;\n}\n\nexport interface PriceFormat {\n symbol: string;\n format: string;\n decimal: string;\n thousand: string;\n precision: number;\n}\n\nexport interface Formats {\n date: string;\n time: string;\n datetime: string;\n price: PriceFormat;\n number: NumberFormat;\n}\n\nexport interface Translator {\n (base: any): any;\n}\n\nexport interface Translations {\n [key: string]: string;\n}\n"],"mappings":""}
|