@itwin/presentation-common 4.4.0-dev.26 → 4.4.0-dev.28

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.
@@ -4,8 +4,10 @@
4
4
  import { Content } from "./content/Content";
5
5
  import { Descriptor } from "./content/Descriptor";
6
6
  import { Item } from "./content/Item";
7
+ import { DisplayValueGroup } from "./content/Value";
7
8
  import { ElementProperties } from "./ElementProperties";
8
9
  import { Node } from "./hierarchy/Node";
10
+ import { NodePathElement } from "./hierarchy/NodePathElement";
9
11
  import { LabelDefinition } from "./LabelDefinition";
10
12
  /** @internal */
11
13
  export interface LocalizationHelperProps {
@@ -17,18 +19,19 @@ export declare class LocalizationHelper {
17
19
  constructor(props: LocalizationHelperProps);
18
20
  getLocalizedString(text: string): string;
19
21
  getLocalizedNodes(nodes: Node[]): Node[];
22
+ getLocalizedNodePathElement(npe: NodePathElement): NodePathElement;
23
+ getLocalizedDisplayValueGroup(group: DisplayValueGroup): DisplayValueGroup;
20
24
  getLocalizedLabelDefinition(labelDefinition: LabelDefinition): LabelDefinition;
21
25
  getLocalizedLabelDefinitions(labelDefinitions: LabelDefinition[]): LabelDefinition[];
22
26
  getLocalizedContentDescriptor(descriptor: Descriptor): Descriptor;
23
27
  getLocalizedContentItems(items: Item[]): Item[];
24
28
  getLocalizedContent(content: Content): Content;
25
29
  getLocalizedElementProperties(elem: ElementProperties): ElementProperties;
26
- private translateContentItem;
27
- private translateContentItemDisplayValue;
28
- private translateContentItemValue;
29
- private translateContentDescriptorField;
30
- private translateContentDescriptorCategory;
31
- private translateNode;
32
- private translateLabelDefinition;
30
+ private getLocalizedContentItem;
31
+ private getLocalizedRawValue;
32
+ private getLocalizedContentField;
33
+ private getLocalizedCategoryDescription;
34
+ private getLocalizedNode;
35
+ private getLocalizedDisplayValue;
33
36
  }
34
37
  //# sourceMappingURL=LocalizationHelper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"LocalizationHelper.d.ts","sourceRoot":"","sources":["../../../src/presentation-common/LocalizationHelper.ts"],"names":[],"mappings":"AAIA;;GAEG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAuB,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAIzE,gBAAgB;AAChB,MAAM,WAAW,uBAAuB;IACtC,kBAAkB,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CAC7C;AAED,gBAAgB;AAChB,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,mBAAmB,CAA0B;gBAEzC,KAAK,EAAE,uBAAuB;IAInC,kBAAkB,CAAC,IAAI,EAAE,MAAM;IAI/B,iBAAiB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE;IAMxC,2BAA2B,CAAC,eAAe,EAAE,eAAe,GAAG,eAAe;IAK9E,4BAA4B,CAAC,gBAAgB,EAAE,eAAe,EAAE;IAKhE,6BAA6B,CAAC,UAAU,EAAE,UAAU;IAMpD,wBAAwB,CAAC,KAAK,EAAE,IAAI,EAAE;IAKtC,mBAAmB,CAAC,OAAO,EAAE,OAAO;IAMpC,6BAA6B,CAAC,IAAI,EAAE,iBAAiB;IAK5D,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,gCAAgC;IAQxC,OAAO,CAAC,yBAAyB;IAsBjC,OAAO,CAAC,+BAA+B;IAIvC,OAAO,CAAC,kCAAkC;IAK1C,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,wBAAwB;CAYjC"}
1
+ {"version":3,"file":"LocalizationHelper.d.ts","sourceRoot":"","sources":["../../../src/presentation-common/LocalizationH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AAoB;IAqB5B,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,+BAA+B;IAMvC,OAAO,CAAC,gBAAgB;IAQxB,OAAO,CAAC,wBAAwB;CAYjC"}
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
  /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
6
  /** @packageDocumentation
7
7
  * @module Core
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.LocalizationHelper = void 0;
11
+ const Content_1 = require("./content/Content");
12
+ const Descriptor_1 = require("./content/Descriptor");
11
13
  const Value_1 = require("./content/Value");
12
14
  const LabelDefinition_1 = require("./LabelDefinition");
13
15
  const KEY_PATTERN = /@[\w\d\-_]+:[\w\d\-\._]+?@/g;
@@ -20,102 +22,117 @@ class LocalizationHelper {
20
22
  return text.replace(KEY_PATTERN, (key) => this._getLocalizedString(key.replace(/^@|@$/g, "")));
21
23
  }
22
24
  getLocalizedNodes(nodes) {
23
- for (const node of nodes)
24
- this.translateNode(node);
25
- return nodes;
25
+ return nodes.map((n) => this.getLocalizedNode(n));
26
+ }
27
+ getLocalizedNodePathElement(npe) {
28
+ return {
29
+ ...npe,
30
+ node: this.getLocalizedNode(npe.node),
31
+ children: npe.children.map((c) => this.getLocalizedNodePathElement(c)),
32
+ };
33
+ }
34
+ getLocalizedDisplayValueGroup(group) {
35
+ return {
36
+ ...group,
37
+ displayValue: this.getLocalizedDisplayValue(group.displayValue),
38
+ };
26
39
  }
27
40
  getLocalizedLabelDefinition(labelDefinition) {
28
- this.translateLabelDefinition(labelDefinition);
41
+ const getLocalizedComposite = (compositeValue) => ({
42
+ ...compositeValue,
43
+ values: compositeValue.values.map((value) => this.getLocalizedLabelDefinition(value)),
44
+ });
45
+ if (labelDefinition.typeName === LabelDefinition_1.LabelDefinition.COMPOSITE_DEFINITION_TYPENAME) {
46
+ return {
47
+ ...labelDefinition,
48
+ rawValue: getLocalizedComposite(labelDefinition.rawValue),
49
+ };
50
+ }
51
+ if (labelDefinition.typeName === "string") {
52
+ return {
53
+ ...labelDefinition,
54
+ rawValue: this.getLocalizedString(labelDefinition.rawValue),
55
+ displayValue: this.getLocalizedString(labelDefinition.displayValue),
56
+ };
57
+ }
29
58
  return labelDefinition;
30
59
  }
31
60
  getLocalizedLabelDefinitions(labelDefinitions) {
32
- labelDefinitions.forEach((labelDefinition) => this.translateLabelDefinition(labelDefinition));
33
- return labelDefinitions;
61
+ return labelDefinitions.map((labelDefinition) => this.getLocalizedLabelDefinition(labelDefinition));
34
62
  }
35
63
  getLocalizedContentDescriptor(descriptor) {
36
- descriptor.fields.forEach((field) => this.translateContentDescriptorField(field));
37
- descriptor.categories.forEach((category) => this.translateContentDescriptorCategory(category));
38
- return descriptor;
64
+ const clone = new Descriptor_1.Descriptor(descriptor);
65
+ clone.fields.forEach((field) => this.getLocalizedContentField(field));
66
+ clone.categories.forEach((category) => this.getLocalizedCategoryDescription(category));
67
+ return clone;
39
68
  }
40
69
  getLocalizedContentItems(items) {
41
- items.forEach((item) => this.translateContentItem(item));
42
- return items;
70
+ return items.map((item) => this.getLocalizedContentItem(item));
43
71
  }
44
72
  getLocalizedContent(content) {
45
- this.getLocalizedContentDescriptor(content.descriptor);
46
- this.getLocalizedContentItems(content.contentSet);
47
- return content;
73
+ return new Content_1.Content(this.getLocalizedContentDescriptor(content.descriptor), this.getLocalizedContentItems(content.contentSet));
48
74
  }
49
75
  getLocalizedElementProperties(elem) {
50
- elem.label = this.getLocalizedString(elem.label);
51
- return elem;
52
- }
53
- translateContentItem(item) {
54
- for (const key in item.displayValues) {
55
- // istanbul ignore else
56
- if (key)
57
- item.displayValues[key] = this.translateContentItemDisplayValue(item.displayValues[key]);
58
- }
59
- for (const key in item.values) {
60
- // istanbul ignore else
61
- if (key)
62
- item.values[key] = this.translateContentItemValue(item.values[key]);
63
- }
64
- this.translateLabelDefinition(item.label);
76
+ return {
77
+ ...elem,
78
+ label: this.getLocalizedString(elem.label),
79
+ };
65
80
  }
66
- translateContentItemDisplayValue(value) {
67
- // istanbul ignore else
68
- if (typeof value === "string") {
69
- value = this.getLocalizedString(value);
70
- }
71
- return value;
81
+ // warning: this function mutates the item
82
+ getLocalizedContentItem(item) {
83
+ item.label = this.getLocalizedLabelDefinition(item.label);
84
+ item.values = Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {});
85
+ item.displayValues = Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});
86
+ return item;
72
87
  }
73
- translateContentItemValue(value) {
88
+ getLocalizedRawValue(value) {
74
89
  if (typeof value === "string") {
75
- value = this.getLocalizedString(value);
90
+ return this.getLocalizedString(value);
76
91
  }
77
- else if (Value_1.Value.isNavigationValue(value)) {
78
- this.translateLabelDefinition(value.label);
92
+ if (Value_1.Value.isNavigationValue(value)) {
93
+ return {
94
+ ...value,
95
+ label: this.getLocalizedLabelDefinition(value.label),
96
+ };
79
97
  }
80
- else if (Value_1.Value.isNestedContent(value)) {
81
- for (const nestedValue of value) {
82
- for (const key in nestedValue.values) {
83
- // istanbul ignore else
84
- if (key)
85
- nestedValue.values[key] = this.translateContentItemValue(nestedValue.values[key]);
86
- }
87
- for (const key in nestedValue.displayValues) {
88
- // istanbul ignore else
89
- if (key)
90
- nestedValue.displayValues[key] = this.translateContentItemDisplayValue(nestedValue.displayValues[key]);
91
- }
92
- }
98
+ if (Value_1.Value.isNestedContent(value)) {
99
+ return value.map((item) => ({
100
+ ...item,
101
+ values: Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {}),
102
+ displayValues: Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {}),
103
+ }));
93
104
  }
94
105
  return value;
95
106
  }
96
- translateContentDescriptorField(field) {
107
+ // warning: this function mutates the field
108
+ getLocalizedContentField(field) {
97
109
  field.label = this.getLocalizedString(field.label);
110
+ return field;
98
111
  }
99
- translateContentDescriptorCategory(category) {
112
+ // warning: this function mutates the category
113
+ getLocalizedCategoryDescription(category) {
100
114
  category.label = this.getLocalizedString(category.label);
101
115
  category.description = this.getLocalizedString(category.description);
116
+ return category;
102
117
  }
103
- translateNode(node) {
104
- this.translateLabelDefinition(node.label);
105
- // istanbul ignore else
106
- if (node.description)
107
- node.description = this.getLocalizedString(node.description);
108
- }
109
- translateLabelDefinition(labelDefinition) {
110
- const translateComposite = (compositeValue) => {
111
- compositeValue.values.map((value) => this.translateLabelDefinition(value));
118
+ getLocalizedNode(node) {
119
+ return {
120
+ ...node,
121
+ label: this.getLocalizedLabelDefinition(node.label),
122
+ ...(node.description ? { description: this.getLocalizedString(node.description) } : undefined),
112
123
  };
113
- if (labelDefinition.typeName === LabelDefinition_1.LabelDefinition.COMPOSITE_DEFINITION_TYPENAME)
114
- translateComposite(labelDefinition.rawValue);
115
- else if (labelDefinition.typeName === "string") {
116
- labelDefinition.rawValue = this.getLocalizedString(labelDefinition.rawValue);
117
- labelDefinition.displayValue = this.getLocalizedString(labelDefinition.displayValue);
124
+ }
125
+ getLocalizedDisplayValue(value) {
126
+ if (typeof value === "undefined") {
127
+ return undefined;
128
+ }
129
+ if (typeof value === "string") {
130
+ return this.getLocalizedString(value);
131
+ }
132
+ if (Array.isArray(value)) {
133
+ return value.map((v) => this.getLocalizedDisplayValue(v));
118
134
  }
135
+ return Object.entries(value).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});
119
136
  }
120
137
  }
121
138
  exports.LocalizationHelper = LocalizationHelper;
@@ -1 +1 @@
1
- {"version":3,"file":"LocalizationHelper.js","sour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AAC,IAAU;QAC9B,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC1C,uBAAuB;QACvB,IAAI,IAAI,CAAC,WAAW;YAClB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACjE,CAAC;IAEO,wBAAwB,CAAC,eAAgC;QAC/D,MAAM,kBAAkB,GAAG,CAAC,cAAmC,EAAE,EAAE;YACjE,cAAc,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,wBAAwB,CAAC,KAAK,CAAC,CAAC,CAAC;QAC7E,CAAC,CAAC;QAEF,IAAI,eAAe,CAAC,QAAQ,KAAK,iCAAe,CAAC,6BAA6B;YAC5E,kBAAkB,CAAC,eAAe,CAAC,QAA+B,CAAC,CAAC;aACjE,IAAI,eAAe,CAAC,QAAQ,KAAK,QAAQ,EAAE;YAC9C,eAAe,CAAC,QAAQ,GAAG,IAAI,CAAC,kBAAkB,CAAC,eAAe,CAAC,QAAkB,CAAC,CAAC;YACvF,eAAe,CAAC,YAAY,GAAG,IAAI,CAAC,kBAAkB,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;SACtF;IACH,CAAC;CACF;AAzHD,gDAyHC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Core\r\n */\r\n\r\nimport { CategoryDescription } from \"./content/Category\";\r\nimport { Content } from \"./content/Content\";\r\nimport { Descriptor } from \"./content/Descriptor\";\r\nimport { Field } from \"./content/Fields\";\r\nimport { Item } from \"./content/Item\";\r\nimport { DisplayValue, Value } from \"./content/Value\";\r\nimport { ElementProperties } from \"./ElementProperties\";\r\nimport { Node } from \"./hierarchy/Node\";\r\nimport { LabelCompositeValue, LabelDefinition } from \"./LabelDefinition\";\r\n\r\nconst KEY_PATTERN = /@[\\w\\d\\-_]+:[\\w\\d\\-\\._]+?@/g;\r\n\r\n/** @internal */\r\nexport interface LocalizationHelperProps {\r\n getLocalizedString: (key: string) => string;\r\n}\r\n\r\n/** @internal */\r\nexport class LocalizationHelper {\r\n private _getLocalizedString: (key: string) => string;\r\n\r\n constructor(props: LocalizationHelperProps) {\r\n this._getLocalizedString = props.getLocalizedString;\r\n }\r\n\r\n public getLocalizedString(text: string) {\r\n return text.replace(KEY_PATTERN, (key) => this._getLocalizedString(key.replace(/^@|@$/g, \"\")));\r\n }\r\n\r\n public getLocalizedNodes(nodes: Node[]): Node[] {\r\n for (const node of nodes)\r\n this.translateNode(node);\r\n return nodes;\r\n }\r\n\r\n public getLocalizedLabelDefinition(labelDefinition: LabelDefinition): LabelDefinition {\r\n this.translateLabelDefinition(labelDefinition);\r\n return labelDefinition;\r\n }\r\n\r\n public getLocalizedLabelDefinitions(labelDefinitions: LabelDefinition[]) {\r\n labelDefinitions.forEach((labelDefinition) => this.translateLabelDefinition(labelDefinition));\r\n return labelDefinitions;\r\n }\r\n\r\n public getLocalizedContentDescriptor(descriptor: Descriptor) {\r\n descriptor.fields.forEach((field) => this.translateContentDescriptorField(field));\r\n descriptor.categories.forEach((category) => this.translateContentDescriptorCategory(category));\r\n return descriptor;\r\n }\r\n\r\n public getLocalizedContentItems(items: Item[]) {\r\n items.forEach((item) => this.translateContentItem(item));\r\n return items;\r\n }\r\n\r\n public getLocalizedContent(content: Content) {\r\n this.getLocalizedContentDescriptor(content.descriptor);\r\n this.getLocalizedContentItems(content.contentSet);\r\n return content;\r\n }\r\n\r\n public getLocalizedElementProperties(elem: ElementProperties) {\r\n elem.label = this.getLocalizedString(elem.label);\r\n return elem;\r\n }\r\n\r\n private translateContentItem(item: Item) {\r\n for (const key in item.displayValues) {\r\n // istanbul ignore else\r\n if (key)\r\n item.displayValues[key] = this.translateContentItemDisplayValue(item.displayValues[key]);\r\n }\r\n for (const key in item.values) {\r\n // istanbul ignore else\r\n if (key)\r\n item.values[key] = this.translateContentItemValue(item.values[key]);\r\n }\r\n this.translateLabelDefinition(item.label);\r\n }\r\n\r\n private translateContentItemDisplayValue(value: DisplayValue): DisplayValue {\r\n // istanbul ignore else\r\n if (typeof value === \"string\") {\r\n value = this.getLocalizedString(value);\r\n }\r\n return value;\r\n }\r\n\r\n private translateContentItemValue(value: Value): Value {\r\n if (typeof value === \"string\") {\r\n value = this.getLocalizedString(value);\r\n } else if (Value.isNavigationValue(value)) {\r\n this.translateLabelDefinition(value.label);\r\n } else if (Value.isNestedContent(value)) {\r\n for (const nestedValue of value) {\r\n for (const key in nestedValue.values) {\r\n // istanbul ignore else\r\n if (key)\r\n nestedValue.values[key] = this.translateContentItemValue(nestedValue.values[key]);\r\n }\r\n for (const key in nestedValue.displayValues) {\r\n // istanbul ignore else\r\n if (key)\r\n nestedValue.displayValues[key] = this.translateContentItemDisplayValue(nestedValue.displayValues[key]);\r\n }\r\n }\r\n }\r\n return value;\r\n }\r\n\r\n private translateContentDescriptorField(field: Field) {\r\n field.label = this.getLocalizedString(field.label);\r\n }\r\n\r\n private translateContentDescriptorCategory(category: CategoryDescription) {\r\n category.label = this.getLocalizedString(category.label);\r\n category.description = this.getLocalizedString(category.description);\r\n }\r\n\r\n private translateNode(node: Node) {\r\n this.translateLabelDefinition(node.label);\r\n // istanbul ignore else\r\n if (node.description)\r\n node.description = this.getLocalizedString(node.description);\r\n }\r\n\r\n private translateLabelDefinition(labelDefinition: LabelDefinition) {\r\n const translateComposite = (compositeValue: LabelCompositeValue) => {\r\n compositeValue.values.map((value) => this.translateLabelDefinition(value));\r\n };\r\n\r\n if (labelDefinition.typeName === LabelDefinition.COMPOSITE_DEFINITION_TYPENAME)\r\n translateComposite(labelDefinition.rawValue as LabelCompositeValue);\r\n else if (labelDefinition.typeName === \"string\") {\r\n labelDefinition.rawValue = this.getLocalizedString(labelDefinition.rawValue as string);\r\n labelDefinition.displayValue = this.getLocalizedString(labelDefinition.displayValue);\r\n }\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"LocalizationHelper.js","sourceRoot":"","sources":["../../../src/presentation-common/LocalizationHelper.ts"],"names":[],"mappings":";AAAA;;;gGAGgG;AAChG;;GAEG;;;AAGH,+CAA4C;AAC5C,qDAAkD;AAGlD,2CAAyE;AAIzE,uDAAyE;AAEzE,MAAM,WAAW,GAAG,6BAA6B,CAAC;AAOlD,gBAAgB;AAChB,MAAa,kBAAkB;IAG7B,YAAY,KAA8B;QACxC,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC,kBAAkB,CAAC;IACtD,CAAC;IAEM,kBAAkB,CAAC,IAAY;QACpC,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;IACjG,CAAC;IAEM,iBAAiB,CAAC,KAAa;QACpC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC;IACpD,CAAC;IAEM,2BAA2B,CAAC,GAAoB;QACrD,OAAO;YACL,GAAG,GAAG;YACN,IAAI,EAAE,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC;YACrC,QAAQ,EAAE,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC,CAAC,CAAC;SACvE,CAAC;IACJ,CAAC;IAEM,6BAA6B,CAAC,KAAwB;QAC3D,OAAO;YACL,GAAG,KAAK;YACR,YAAY,EAAE,IAAI,CAAC,wBAAwB,CAAC,KAAK,CAAC,YAAY,CAAC;SAChE,CAAC;IACJ,CAAC;IAEM,2BAA2B,CAAC,e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/*---------------------------------------------------------------------------------------------\r\n * Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n * See LICENSE.md in the project root for license terms and full copyright notice.\r\n *--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Core\r\n */\r\n\r\nimport { CategoryDescription } from \"./content/Category\";\r\nimport { Content } from \"./content/Content\";\r\nimport { Descriptor } from \"./content/Descriptor\";\r\nimport { Field } from \"./content/Fields\";\r\nimport { Item } from \"./content/Item\";\r\nimport { DisplayValue, DisplayValueGroup, Value } from \"./content/Value\";\r\nimport { ElementProperties } from \"./ElementProperties\";\r\nimport { Node } from \"./hierarchy/Node\";\r\nimport { NodePathElement } from \"./hierarchy/NodePathElement\";\r\nimport { LabelCompositeValue, LabelDefinition } from \"./LabelDefinition\";\r\n\r\nconst KEY_PATTERN = /@[\\w\\d\\-_]+:[\\w\\d\\-\\._]+?@/g;\r\n\r\n/** @internal */\r\nexport interface LocalizationHelperProps {\r\n getLocalizedString: (key: string) => string;\r\n}\r\n\r\n/** @internal */\r\nexport class LocalizationHelper {\r\n private _getLocalizedString: (key: string) => string;\r\n\r\n constructor(props: LocalizationHelperProps) {\r\n this._getLocalizedString = props.getLocalizedString;\r\n }\r\n\r\n public getLocalizedString(text: string) {\r\n return text.replace(KEY_PATTERN, (key) => this._getLocalizedString(key.replace(/^@|@$/g, \"\")));\r\n }\r\n\r\n public getLocalizedNodes(nodes: Node[]): Node[] {\r\n return nodes.map((n) => this.getLocalizedNode(n));\r\n }\r\n\r\n public getLocalizedNodePathElement(npe: NodePathElement): NodePathElement {\r\n return {\r\n ...npe,\r\n node: this.getLocalizedNode(npe.node),\r\n children: npe.children.map((c) => this.getLocalizedNodePathElement(c)),\r\n };\r\n }\r\n\r\n public getLocalizedDisplayValueGroup(group: DisplayValueGroup): DisplayValueGroup {\r\n return {\r\n ...group,\r\n displayValue: this.getLocalizedDisplayValue(group.displayValue),\r\n };\r\n }\r\n\r\n public getLocalizedLabelDefinition(labelDefinition: LabelDefinition): LabelDefinition {\r\n const getLocalizedComposite = (compositeValue: LabelCompositeValue) => ({\r\n ...compositeValue,\r\n values: compositeValue.values.map((value) => this.getLocalizedLabelDefinition(value)),\r\n });\r\n\r\n if (labelDefinition.typeName === LabelDefinition.COMPOSITE_DEFINITION_TYPENAME) {\r\n return {\r\n ...labelDefinition,\r\n rawValue: getLocalizedComposite(labelDefinition.rawValue as LabelCompositeValue),\r\n };\r\n }\r\n if (labelDefinition.typeName === \"string\") {\r\n return {\r\n ...labelDefinition,\r\n rawValue: this.getLocalizedString(labelDefinition.rawValue as string),\r\n displayValue: this.getLocalizedString(labelDefinition.displayValue),\r\n };\r\n }\r\n return labelDefinition;\r\n }\r\n\r\n public getLocalizedLabelDefinitions(labelDefinitions: LabelDefinition[]) {\r\n return labelDefinitions.map((labelDefinition) => this.getLocalizedLabelDefinition(labelDefinition));\r\n }\r\n\r\n public getLocalizedContentDescriptor(descriptor: Descriptor) {\r\n const clone = new Descriptor(descriptor);\r\n clone.fields.forEach((field) => this.getLocalizedContentField(field));\r\n clone.categories.forEach((category) => this.getLocalizedCategoryDescription(category));\r\n return clone;\r\n }\r\n\r\n public getLocalizedContentItems(items: Item[]): Item[] {\r\n return items.map((item) => this.getLocalizedContentItem(item));\r\n }\r\n\r\n public getLocalizedContent(content: Content): Content {\r\n return new Content(this.getLocalizedContentDescriptor(content.descriptor), this.getLocalizedContentItems(content.contentSet));\r\n }\r\n\r\n public getLocalizedElementProperties(elem: ElementProperties): ElementProperties {\r\n return {\r\n ...elem,\r\n label: this.getLocalizedString(elem.label),\r\n };\r\n }\r\n\r\n // warning: this function mutates the item\r\n private getLocalizedContentItem(item: Item): Item {\r\n item.label = this.getLocalizedLabelDefinition(item.label);\r\n item.values = Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {});\r\n item.displayValues = Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});\r\n return item;\r\n }\r\n\r\n private getLocalizedRawValue(value: Value): Value {\r\n if (typeof value === \"string\") {\r\n return this.getLocalizedString(value);\r\n }\r\n if (Value.isNavigationValue(value)) {\r\n return {\r\n ...value,\r\n label: this.getLocalizedLabelDefinition(value.label),\r\n };\r\n }\r\n if (Value.isNestedContent(value)) {\r\n return value.map((item) => ({\r\n ...item,\r\n values: Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {}),\r\n displayValues: Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {}),\r\n }));\r\n }\r\n return value;\r\n }\r\n\r\n // warning: this function mutates the field\r\n private getLocalizedContentField(field: Field) {\r\n field.label = this.getLocalizedString(field.label);\r\n return field;\r\n }\r\n\r\n // warning: this function mutates the category\r\n private getLocalizedCategoryDescription(category: CategoryDescription) {\r\n category.label = this.getLocalizedString(category.label);\r\n category.description = this.getLocalizedString(category.description);\r\n return category;\r\n }\r\n\r\n private getLocalizedNode(node: Node) {\r\n return {\r\n ...node,\r\n label: this.getLocalizedLabelDefinition(node.label),\r\n ...(node.description ? { description: this.getLocalizedString(node.description) } : undefined),\r\n };\r\n }\r\n\r\n private getLocalizedDisplayValue(value: DisplayValue): DisplayValue {\r\n if (typeof value === \"undefined\") {\r\n return undefined;\r\n }\r\n if (typeof value === \"string\") {\r\n return this.getLocalizedString(value);\r\n }\r\n if (Array.isArray(value)) {\r\n return value.map((v) => this.getLocalizedDisplayValue(v));\r\n }\r\n return Object.entries(value).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});\r\n }\r\n}\r\n"]}
@@ -4,8 +4,10 @@
4
4
  import { Content } from "./content/Content";
5
5
  import { Descriptor } from "./content/Descriptor";
6
6
  import { Item } from "./content/Item";
7
+ import { DisplayValueGroup } from "./content/Value";
7
8
  import { ElementProperties } from "./ElementProperties";
8
9
  import { Node } from "./hierarchy/Node";
10
+ import { NodePathElement } from "./hierarchy/NodePathElement";
9
11
  import { LabelDefinition } from "./LabelDefinition";
10
12
  /** @internal */
11
13
  export interface LocalizationHelperProps {
@@ -17,18 +19,19 @@ export declare class LocalizationHelper {
17
19
  constructor(props: LocalizationHelperProps);
18
20
  getLocalizedString(text: string): string;
19
21
  getLocalizedNodes(nodes: Node[]): Node[];
22
+ getLocalizedNodePathElement(npe: NodePathElement): NodePathElement;
23
+ getLocalizedDisplayValueGroup(group: DisplayValueGroup): DisplayValueGroup;
20
24
  getLocalizedLabelDefinition(labelDefinition: LabelDefinition): LabelDefinition;
21
25
  getLocalizedLabelDefinitions(labelDefinitions: LabelDefinition[]): LabelDefinition[];
22
26
  getLocalizedContentDescriptor(descriptor: Descriptor): Descriptor;
23
27
  getLocalizedContentItems(items: Item[]): Item[];
24
28
  getLocalizedContent(content: Content): Content;
25
29
  getLocalizedElementProperties(elem: ElementProperties): ElementProperties;
26
- private translateContentItem;
27
- private translateContentItemDisplayValue;
28
- private translateContentItemValue;
29
- private translateContentDescriptorField;
30
- private translateContentDescriptorCategory;
31
- private translateNode;
32
- private translateLabelDefinition;
30
+ private getLocalizedContentItem;
31
+ private getLocalizedRawValue;
32
+ private getLocalizedContentField;
33
+ private getLocalizedCategoryDescription;
34
+ private getLocalizedNode;
35
+ private getLocalizedDisplayValue;
33
36
  }
34
37
  //# sourceMappingURL=LocalizationHelper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"LocalizationHelper.d.ts","sourceRoot":"","sources":["../../../src/presentation-common/LocalizationHelper.ts"],"names":[],"mappings":"AAIA;;GAEG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAA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EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAuB,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAIzE,gBAAgB;AAChB,MAAM,WAAW,uBAAuB;IACtC,kBAAkB,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CAC7C;AAED,gBAAgB;AAChB,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,mBAAmB,CAA0B;gBAEzC,KAAK,EAAE,uBAAuB;IAInC,kBAAkB,CAAC,IAAI,EAAE,MAAM;IAI/B,iBAAiB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE;IAIxC,2BAA2B,CAAC,GAAG,EAAE,eAAe,GAAG,eAAe;IAQlE,6BAA6B,CAAC,KAAK,EAAE,iBAAiB,GAAG,iBAAiB;IAO1E,2BAA2B,CAAC,eAAe,EAAE,eAAe,GAAG,eAAe;IAsB9E,4BAA4B,CAAC,gBAAgB,EAAE,eAAe,EAAE;IAIhE,6BAA6B,CAAC,UAAU,EAAE,UAAU;IAOpD,wBAAwB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE;IAI/C,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO;IAI9C,6BAA6B,CAAC,IAAI,EAAE,iBAAiB,GAAG,iBAAiB;IAQhF,OAAO,CAAC,uBAAuB;IAO/B,OAAO,CAAC,oBAAoB;IAqB5B,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,+BAA+B;IAMvC,OAAO,CAAC,gBAAgB;IAQxB,OAAO,CAAC,wBAAwB;CAYjC"}
@@ -1,10 +1,12 @@
1
1
  /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
- * See LICENSE.md in the project root for license terms and full copyright notice.
4
- *--------------------------------------------------------------------------------------------*/
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
5
  /** @packageDocumentation
6
6
  * @module Core
7
7
  */
8
+ import { Content } from "./content/Content";
9
+ import { Descriptor } from "./content/Descriptor";
8
10
  import { Value } from "./content/Value";
9
11
  import { LabelDefinition } from "./LabelDefinition";
10
12
  const KEY_PATTERN = /@[\w\d\-_]+:[\w\d\-\._]+?@/g;
@@ -17,102 +19,117 @@ export class LocalizationHelper {
17
19
  return text.replace(KEY_PATTERN, (key) => this._getLocalizedString(key.replace(/^@|@$/g, "")));
18
20
  }
19
21
  getLocalizedNodes(nodes) {
20
- for (const node of nodes)
21
- this.translateNode(node);
22
- return nodes;
22
+ return nodes.map((n) => this.getLocalizedNode(n));
23
+ }
24
+ getLocalizedNodePathElement(npe) {
25
+ return {
26
+ ...npe,
27
+ node: this.getLocalizedNode(npe.node),
28
+ children: npe.children.map((c) => this.getLocalizedNodePathElement(c)),
29
+ };
30
+ }
31
+ getLocalizedDisplayValueGroup(group) {
32
+ return {
33
+ ...group,
34
+ displayValue: this.getLocalizedDisplayValue(group.displayValue),
35
+ };
23
36
  }
24
37
  getLocalizedLabelDefinition(labelDefinition) {
25
- this.translateLabelDefinition(labelDefinition);
38
+ const getLocalizedComposite = (compositeValue) => ({
39
+ ...compositeValue,
40
+ values: compositeValue.values.map((value) => this.getLocalizedLabelDefinition(value)),
41
+ });
42
+ if (labelDefinition.typeName === LabelDefinition.COMPOSITE_DEFINITION_TYPENAME) {
43
+ return {
44
+ ...labelDefinition,
45
+ rawValue: getLocalizedComposite(labelDefinition.rawValue),
46
+ };
47
+ }
48
+ if (labelDefinition.typeName === "string") {
49
+ return {
50
+ ...labelDefinition,
51
+ rawValue: this.getLocalizedString(labelDefinition.rawValue),
52
+ displayValue: this.getLocalizedString(labelDefinition.displayValue),
53
+ };
54
+ }
26
55
  return labelDefinition;
27
56
  }
28
57
  getLocalizedLabelDefinitions(labelDefinitions) {
29
- labelDefinitions.forEach((labelDefinition) => this.translateLabelDefinition(labelDefinition));
30
- return labelDefinitions;
58
+ return labelDefinitions.map((labelDefinition) => this.getLocalizedLabelDefinition(labelDefinition));
31
59
  }
32
60
  getLocalizedContentDescriptor(descriptor) {
33
- descriptor.fields.forEach((field) => this.translateContentDescriptorField(field));
34
- descriptor.categories.forEach((category) => this.translateContentDescriptorCategory(category));
35
- return descriptor;
61
+ const clone = new Descriptor(descriptor);
62
+ clone.fields.forEach((field) => this.getLocalizedContentField(field));
63
+ clone.categories.forEach((category) => this.getLocalizedCategoryDescription(category));
64
+ return clone;
36
65
  }
37
66
  getLocalizedContentItems(items) {
38
- items.forEach((item) => this.translateContentItem(item));
39
- return items;
67
+ return items.map((item) => this.getLocalizedContentItem(item));
40
68
  }
41
69
  getLocalizedContent(content) {
42
- this.getLocalizedContentDescriptor(content.descriptor);
43
- this.getLocalizedContentItems(content.contentSet);
44
- return content;
70
+ return new Content(this.getLocalizedContentDescriptor(content.descriptor), this.getLocalizedContentItems(content.contentSet));
45
71
  }
46
72
  getLocalizedElementProperties(elem) {
47
- elem.label = this.getLocalizedString(elem.label);
48
- return elem;
49
- }
50
- translateContentItem(item) {
51
- for (const key in item.displayValues) {
52
- // istanbul ignore else
53
- if (key)
54
- item.displayValues[key] = this.translateContentItemDisplayValue(item.displayValues[key]);
55
- }
56
- for (const key in item.values) {
57
- // istanbul ignore else
58
- if (key)
59
- item.values[key] = this.translateContentItemValue(item.values[key]);
60
- }
61
- this.translateLabelDefinition(item.label);
73
+ return {
74
+ ...elem,
75
+ label: this.getLocalizedString(elem.label),
76
+ };
62
77
  }
63
- translateContentItemDisplayValue(value) {
64
- // istanbul ignore else
65
- if (typeof value === "string") {
66
- value = this.getLocalizedString(value);
67
- }
68
- return value;
78
+ // warning: this function mutates the item
79
+ getLocalizedContentItem(item) {
80
+ item.label = this.getLocalizedLabelDefinition(item.label);
81
+ item.values = Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {});
82
+ item.displayValues = Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});
83
+ return item;
69
84
  }
70
- translateContentItemValue(value) {
85
+ getLocalizedRawValue(value) {
71
86
  if (typeof value === "string") {
72
- value = this.getLocalizedString(value);
87
+ return this.getLocalizedString(value);
73
88
  }
74
- else if (Value.isNavigationValue(value)) {
75
- this.translateLabelDefinition(value.label);
89
+ if (Value.isNavigationValue(value)) {
90
+ return {
91
+ ...value,
92
+ label: this.getLocalizedLabelDefinition(value.label),
93
+ };
76
94
  }
77
- else if (Value.isNestedContent(value)) {
78
- for (const nestedValue of value) {
79
- for (const key in nestedValue.values) {
80
- // istanbul ignore else
81
- if (key)
82
- nestedValue.values[key] = this.translateContentItemValue(nestedValue.values[key]);
83
- }
84
- for (const key in nestedValue.displayValues) {
85
- // istanbul ignore else
86
- if (key)
87
- nestedValue.displayValues[key] = this.translateContentItemDisplayValue(nestedValue.displayValues[key]);
88
- }
89
- }
95
+ if (Value.isNestedContent(value)) {
96
+ return value.map((item) => ({
97
+ ...item,
98
+ values: Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {}),
99
+ displayValues: Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {}),
100
+ }));
90
101
  }
91
102
  return value;
92
103
  }
93
- translateContentDescriptorField(field) {
104
+ // warning: this function mutates the field
105
+ getLocalizedContentField(field) {
94
106
  field.label = this.getLocalizedString(field.label);
107
+ return field;
95
108
  }
96
- translateContentDescriptorCategory(category) {
109
+ // warning: this function mutates the category
110
+ getLocalizedCategoryDescription(category) {
97
111
  category.label = this.getLocalizedString(category.label);
98
112
  category.description = this.getLocalizedString(category.description);
113
+ return category;
99
114
  }
100
- translateNode(node) {
101
- this.translateLabelDefinition(node.label);
102
- // istanbul ignore else
103
- if (node.description)
104
- node.description = this.getLocalizedString(node.description);
105
- }
106
- translateLabelDefinition(labelDefinition) {
107
- const translateComposite = (compositeValue) => {
108
- compositeValue.values.map((value) => this.translateLabelDefinition(value));
115
+ getLocalizedNode(node) {
116
+ return {
117
+ ...node,
118
+ label: this.getLocalizedLabelDefinition(node.label),
119
+ ...(node.description ? { description: this.getLocalizedString(node.description) } : undefined),
109
120
  };
110
- if (labelDefinition.typeName === LabelDefinition.COMPOSITE_DEFINITION_TYPENAME)
111
- translateComposite(labelDefinition.rawValue);
112
- else if (labelDefinition.typeName === "string") {
113
- labelDefinition.rawValue = this.getLocalizedString(labelDefinition.rawValue);
114
- labelDefinition.displayValue = this.getLocalizedString(labelDefinition.displayValue);
121
+ }
122
+ getLocalizedDisplayValue(value) {
123
+ if (typeof value === "undefined") {
124
+ return undefined;
125
+ }
126
+ if (typeof value === "string") {
127
+ return this.getLocalizedString(value);
128
+ }
129
+ if (Array.isArray(value)) {
130
+ return value.map((v) => this.getLocalizedDisplayValue(v));
115
131
  }
132
+ return Object.entries(value).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});
116
133
  }
117
134
  }
118
135
  //# sourceMappingURL=LocalizationHelper.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"LocalizationHelper.js","sourceRoot":"","sources":["../../../src/presentation-common/LocalizationHelper.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAOH,OAAO,EAAgB,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGtD,OAAO,EAAuB,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzE,MAAM,WAAW,GAAG,6BAA6B,CAAC;AAOlD,gBAAgB;AAChB,MAAM,OAAO,kBAAkB;IAG7B,YAAY,KAA8B;QACxC,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC,kBAAkB,CAAC;IACtD,CAAC;IAEM,kBAAkB,CAAC,IAAY;QACpC,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,GAAG,EAAE,EAAE,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BAAwB,CAAC,KAAK,CAAC,CAAC,CAAC;QAC7E,CAAC,CAAC;QAEF,IAAI,eAAe,CAAC,QAAQ,KAAK,eAAe,CAAC,6BAA6B;YAC5E,kBAAkB,CAAC,eAAe,CAAC,QAA+B,CAAC,CAAC;aACjE,IAAI,eAAe,CAAC,QAAQ,KAAK,QAAQ,EAAE;YAC9C,eAAe,CAAC,QAAQ,GAAG,IAAI,CAAC,kBAAkB,CAAC,eAAe,CAAC,QAAkB,CAAC,CAAC;YACvF,eAAe,CAAC,YAAY,GAAG,IAAI,CAAC,kBAAkB,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;SACtF;IACH,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Core\r\n */\r\n\r\nimport { CategoryDescription } from \"./content/Category\";\r\nimport { Content } from \"./content/Content\";\r\nimport { Descriptor } from \"./content/Descriptor\";\r\nimport { Field } from \"./content/Fields\";\r\nimport { Item } from \"./content/Item\";\r\nimport { DisplayValue, Value } from \"./content/Value\";\r\nimport { ElementProperties } from \"./ElementProperties\";\r\nimport { Node } from \"./hierarchy/Node\";\r\nimport { LabelCompositeValue, LabelDefinition } from \"./LabelDefinition\";\r\n\r\nconst KEY_PATTERN = /@[\\w\\d\\-_]+:[\\w\\d\\-\\._]+?@/g;\r\n\r\n/** @internal */\r\nexport interface LocalizationHelperProps {\r\n getLocalizedString: (key: string) => string;\r\n}\r\n\r\n/** @internal */\r\nexport class LocalizationHelper {\r\n private _getLocalizedString: (key: string) => string;\r\n\r\n constructor(props: LocalizationHelperProps) {\r\n this._getLocalizedString = props.getLocalizedString;\r\n }\r\n\r\n public getLocalizedString(text: string) {\r\n return text.replace(KEY_PATTERN, (key) => this._getLocalizedString(key.replace(/^@|@$/g, \"\")));\r\n }\r\n\r\n public getLocalizedNodes(nodes: Node[]): Node[] {\r\n for (const node of nodes)\r\n this.translateNode(node);\r\n return nodes;\r\n }\r\n\r\n public getLocalizedLabelDefinition(labelDefinition: LabelDefinition): LabelDefinition {\r\n this.translateLabelDefinition(labelDefinition);\r\n return labelDefinition;\r\n }\r\n\r\n public getLocalizedLabelDefinitions(labelDefinitions: LabelDefinition[]) {\r\n labelDefinitions.forEach((labelDefinition) => this.translateLabelDefinition(labelDefinition));\r\n return labelDefinitions;\r\n }\r\n\r\n public getLocalizedContentDescriptor(descriptor: Descriptor) {\r\n descriptor.fields.forEach((field) => this.translateContentDescriptorField(field));\r\n descriptor.categories.forEach((category) => this.translateContentDescriptorCategory(category));\r\n return descriptor;\r\n }\r\n\r\n public getLocalizedContentItems(items: Item[]) {\r\n items.forEach((item) => this.translateContentItem(item));\r\n return items;\r\n }\r\n\r\n public getLocalizedContent(content: Content) {\r\n this.getLocalizedContentDescriptor(content.descriptor);\r\n this.getLocalizedContentItems(content.contentSet);\r\n return content;\r\n }\r\n\r\n public getLocalizedElementProperties(elem: ElementProperties) {\r\n elem.label = this.getLocalizedString(elem.label);\r\n return elem;\r\n }\r\n\r\n private translateContentItem(item: Item) {\r\n for (const key in item.displayValues) {\r\n // istanbul ignore else\r\n if (key)\r\n item.displayValues[key] = this.translateContentItemDisplayValue(item.displayValues[key]);\r\n }\r\n for (const key in item.values) {\r\n // istanbul ignore else\r\n if (key)\r\n item.values[key] = this.translateContentItemValue(item.values[key]);\r\n }\r\n this.translateLabelDefinition(item.label);\r\n }\r\n\r\n private translateContentItemDisplayValue(value: DisplayValue): DisplayValue {\r\n // istanbul ignore else\r\n if (typeof value === \"string\") {\r\n value = this.getLocalizedString(value);\r\n }\r\n return value;\r\n }\r\n\r\n private translateContentItemValue(value: Value): Value {\r\n if (typeof value === \"string\") {\r\n value = this.getLocalizedString(value);\r\n } else if (Value.isNavigationValue(value)) {\r\n this.translateLabelDefinition(value.label);\r\n } else if (Value.isNestedContent(value)) {\r\n for (const nestedValue of value) {\r\n for (const key in nestedValue.values) {\r\n // istanbul ignore else\r\n if (key)\r\n nestedValue.values[key] = this.translateContentItemValue(nestedValue.values[key]);\r\n }\r\n for (const key in nestedValue.displayValues) {\r\n // istanbul ignore else\r\n if (key)\r\n nestedValue.displayValues[key] = this.translateContentItemDisplayValue(nestedValue.displayValues[key]);\r\n }\r\n }\r\n }\r\n return value;\r\n }\r\n\r\n private translateContentDescriptorField(field: Field) {\r\n field.label = this.getLocalizedString(field.label);\r\n }\r\n\r\n private translateContentDescriptorCategory(category: CategoryDescription) {\r\n category.label = this.getLocalizedString(category.label);\r\n category.description = this.getLocalizedString(category.description);\r\n }\r\n\r\n private translateNode(node: Node) {\r\n this.translateLabelDefinition(node.label);\r\n // istanbul ignore else\r\n if (node.description)\r\n node.description = this.getLocalizedString(node.description);\r\n }\r\n\r\n private translateLabelDefinition(labelDefinition: LabelDefinition) {\r\n const translateComposite = (compositeValue: LabelCompositeValue) => {\r\n compositeValue.values.map((value) => this.translateLabelDefinition(value));\r\n };\r\n\r\n if (labelDefinition.typeName === LabelDefinition.COMPOSITE_DEFINITION_TYPENAME)\r\n translateComposite(labelDefinition.rawValue as LabelCompositeValue);\r\n else if (labelDefinition.typeName === \"string\") {\r\n labelDefinition.rawValue = this.getLocalizedString(labelDefinition.rawValue as string);\r\n labelDefinition.displayValue = this.getLocalizedString(labelDefinition.displayValue);\r\n }\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"LocalizationHelper.js","sourceRoot":"","sources":["../../../src/presentation-common/LocalizationHelper.ts"],"names":[],"mappings":"AAAA;;;gGAGgG;AAChG;;GAEG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD,OAAO,EAAmC,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAIzE,OAAO,EAAuB,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzE,M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AAC,IAAU;QACjC,OAAO;YACL,GAAG,IAAI;YACP,KAAK,EAAE,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,KAAK,CAAC;YACnD,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;SAC/F,CAAC;IACJ,CAAC;IAEO,wBAAwB,CAAC,KAAmB;QAClD,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE;YAChC,OAAO,SAAS,CAAC;SAClB;QACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;YAC7B,OAAO,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;SACvC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;YACxB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAC,CAAC,CAAC,CAAC;SAC3D;QACD,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,wBAAwB,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAC5G,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n * Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n * See LICENSE.md in the project root for license terms and full copyright notice.\r\n *--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Core\r\n */\r\n\r\nimport { CategoryDescription } from \"./content/Category\";\r\nimport { Content } from \"./content/Content\";\r\nimport { Descriptor } from \"./content/Descriptor\";\r\nimport { Field } from \"./content/Fields\";\r\nimport { Item } from \"./content/Item\";\r\nimport { DisplayValue, DisplayValueGroup, Value } from \"./content/Value\";\r\nimport { ElementProperties } from \"./ElementProperties\";\r\nimport { Node } from \"./hierarchy/Node\";\r\nimport { NodePathElement } from \"./hierarchy/NodePathElement\";\r\nimport { LabelCompositeValue, LabelDefinition } from \"./LabelDefinition\";\r\n\r\nconst KEY_PATTERN = /@[\\w\\d\\-_]+:[\\w\\d\\-\\._]+?@/g;\r\n\r\n/** @internal */\r\nexport interface LocalizationHelperProps {\r\n getLocalizedString: (key: string) => string;\r\n}\r\n\r\n/** @internal */\r\nexport class LocalizationHelper {\r\n private _getLocalizedString: (key: string) => string;\r\n\r\n constructor(props: LocalizationHelperProps) {\r\n this._getLocalizedString = props.getLocalizedString;\r\n }\r\n\r\n public getLocalizedString(text: string) {\r\n return text.replace(KEY_PATTERN, (key) => this._getLocalizedString(key.replace(/^@|@$/g, \"\")));\r\n }\r\n\r\n public getLocalizedNodes(nodes: Node[]): Node[] {\r\n return nodes.map((n) => this.getLocalizedNode(n));\r\n }\r\n\r\n public getLocalizedNodePathElement(npe: NodePathElement): NodePathElement {\r\n return {\r\n ...npe,\r\n node: this.getLocalizedNode(npe.node),\r\n children: npe.children.map((c) => this.getLocalizedNodePathElement(c)),\r\n };\r\n }\r\n\r\n public getLocalizedDisplayValueGroup(group: DisplayValueGroup): DisplayValueGroup {\r\n return {\r\n ...group,\r\n displayValue: this.getLocalizedDisplayValue(group.displayValue),\r\n };\r\n }\r\n\r\n public getLocalizedLabelDefinition(labelDefinition: LabelDefinition): LabelDefinition {\r\n const getLocalizedComposite = (compositeValue: LabelCompositeValue) => ({\r\n ...compositeValue,\r\n values: compositeValue.values.map((value) => this.getLocalizedLabelDefinition(value)),\r\n });\r\n\r\n if (labelDefinition.typeName === LabelDefinition.COMPOSITE_DEFINITION_TYPENAME) {\r\n return {\r\n ...labelDefinition,\r\n rawValue: getLocalizedComposite(labelDefinition.rawValue as LabelCompositeValue),\r\n };\r\n }\r\n if (labelDefinition.typeName === \"string\") {\r\n return {\r\n ...labelDefinition,\r\n rawValue: this.getLocalizedString(labelDefinition.rawValue as string),\r\n displayValue: this.getLocalizedString(labelDefinition.displayValue),\r\n };\r\n }\r\n return labelDefinition;\r\n }\r\n\r\n public getLocalizedLabelDefinitions(labelDefinitions: LabelDefinition[]) {\r\n return labelDefinitions.map((labelDefinition) => this.getLocalizedLabelDefinition(labelDefinition));\r\n }\r\n\r\n public getLocalizedContentDescriptor(descriptor: Descriptor) {\r\n const clone = new Descriptor(descriptor);\r\n clone.fields.forEach((field) => this.getLocalizedContentField(field));\r\n clone.categories.forEach((category) => this.getLocalizedCategoryDescription(category));\r\n return clone;\r\n }\r\n\r\n public getLocalizedContentItems(items: Item[]): Item[] {\r\n return items.map((item) => this.getLocalizedContentItem(item));\r\n }\r\n\r\n public getLocalizedContent(content: Content): Content {\r\n return new Content(this.getLocalizedContentDescriptor(content.descriptor), this.getLocalizedContentItems(content.contentSet));\r\n }\r\n\r\n public getLocalizedElementProperties(elem: ElementProperties): ElementProperties {\r\n return {\r\n ...elem,\r\n label: this.getLocalizedString(elem.label),\r\n };\r\n }\r\n\r\n // warning: this function mutates the item\r\n private getLocalizedContentItem(item: Item): Item {\r\n item.label = this.getLocalizedLabelDefinition(item.label);\r\n item.values = Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {});\r\n item.displayValues = Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});\r\n return item;\r\n }\r\n\r\n private getLocalizedRawValue(value: Value): Value {\r\n if (typeof value === \"string\") {\r\n return this.getLocalizedString(value);\r\n }\r\n if (Value.isNavigationValue(value)) {\r\n return {\r\n ...value,\r\n label: this.getLocalizedLabelDefinition(value.label),\r\n };\r\n }\r\n if (Value.isNestedContent(value)) {\r\n return value.map((item) => ({\r\n ...item,\r\n values: Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {}),\r\n displayValues: Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {}),\r\n }));\r\n }\r\n return value;\r\n }\r\n\r\n // warning: this function mutates the field\r\n private getLocalizedContentField(field: Field) {\r\n field.label = this.getLocalizedString(field.label);\r\n return field;\r\n }\r\n\r\n // warning: this function mutates the category\r\n private getLocalizedCategoryDescription(category: CategoryDescription) {\r\n category.label = this.getLocalizedString(category.label);\r\n category.description = this.getLocalizedString(category.description);\r\n return category;\r\n }\r\n\r\n private getLocalizedNode(node: Node) {\r\n return {\r\n ...node,\r\n label: this.getLocalizedLabelDefinition(node.label),\r\n ...(node.description ? { description: this.getLocalizedString(node.description) } : undefined),\r\n };\r\n }\r\n\r\n private getLocalizedDisplayValue(value: DisplayValue): DisplayValue {\r\n if (typeof value === \"undefined\") {\r\n return undefined;\r\n }\r\n if (typeof value === \"string\") {\r\n return this.getLocalizedString(value);\r\n }\r\n if (Array.isArray(value)) {\r\n return value.map((v) => this.getLocalizedDisplayValue(v));\r\n }\r\n return Object.entries(value).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});\r\n }\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itwin/presentation-common",
3
- "version": "4.4.0-dev.26",
3
+ "version": "4.4.0-dev.28",
4
4
  "description": "Common pieces for iModel.js presentation packages",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,10 +22,10 @@
22
22
  "module": "lib/esm/presentation-common.js",
23
23
  "typings": "lib/cjs/presentation-common",
24
24
  "peerDependencies": {
25
- "@itwin/core-bentley": "^4.4.0-dev.26",
26
- "@itwin/core-common": "^4.4.0-dev.26",
27
- "@itwin/core-quantity": "^4.4.0-dev.26",
28
- "@itwin/ecschema-metadata": "^4.4.0-dev.26"
25
+ "@itwin/core-bentley": "^4.4.0-dev.28",
26
+ "@itwin/core-common": "^4.4.0-dev.28",
27
+ "@itwin/core-quantity": "^4.4.0-dev.28",
28
+ "@itwin/ecschema-metadata": "^4.4.0-dev.28"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@itwin/eslint-plugin": "4.0.0-dev.44",
@@ -58,11 +58,11 @@
58
58
  "typescript": "~5.0.2",
59
59
  "typescript-json-schema": "^0.55.0",
60
60
  "yargs": "^17.4.0",
61
- "@itwin/build-tools": "4.4.0-dev.26",
62
- "@itwin/core-common": "4.4.0-dev.26",
63
- "@itwin/core-bentley": "4.4.0-dev.26",
64
- "@itwin/core-quantity": "4.4.0-dev.26",
65
- "@itwin/ecschema-metadata": "4.4.0-dev.26"
61
+ "@itwin/build-tools": "4.4.0-dev.28",
62
+ "@itwin/core-bentley": "4.4.0-dev.28",
63
+ "@itwin/core-common": "4.4.0-dev.28",
64
+ "@itwin/core-quantity": "4.4.0-dev.28",
65
+ "@itwin/ecschema-metadata": "4.4.0-dev.28"
66
66
  },
67
67
  "nyc": {
68
68
  "extends": "./node_modules/@itwin/build-tools/.nycrc"