@itwin/presentation-common
Version:
Common pieces for iModel.js presentation packages
153 lines • 6.45 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Core
*/
import { Content } from "./content/Content.js";
import { Descriptor } from "./content/Descriptor.js";
import { DisplayValue, Value } from "./content/Value.js";
import { COMPOSITE_LABEL_DEFINITION_TYPENAME } from "./LabelDefinition.js";
const KEY_PATTERN = /@[\w\d\-_]+:[\w\d\-\._]+?@/g;
/** @internal */
export class LocalizationHelper {
_getLocalizedString;
constructor(props) {
this._getLocalizedString = props.getLocalizedString;
}
getLocalizedString(text) {
return text.replace(KEY_PATTERN, (key) => this._getLocalizedString(key.replace(/^@|@$/g, "")));
}
getLocalizedNodes(nodes) {
return nodes.map((n) => this.getLocalizedNode(n));
}
getLocalizedNodePathElement(npe) {
return {
...npe,
node: this.getLocalizedNode(npe.node),
children: npe.children.map((c) => this.getLocalizedNodePathElement(c)),
};
}
getLocalizedDisplayValueGroup(group) {
return {
...group,
displayValue: this.getLocalizedDisplayValue(group.displayValue),
};
}
getLocalizedLabelDefinition(labelDefinition) {
const getLocalizedComposite = (compositeValue) => ({
...compositeValue,
values: compositeValue.values.map((value) => this.getLocalizedLabelDefinition(value)),
});
if (labelDefinition.typeName === COMPOSITE_LABEL_DEFINITION_TYPENAME) {
return {
...labelDefinition,
rawValue: getLocalizedComposite(labelDefinition.rawValue),
};
}
if (labelDefinition.typeName === "string") {
return {
...labelDefinition,
rawValue: this.getLocalizedString(labelDefinition.rawValue),
displayValue: this.getLocalizedString(labelDefinition.displayValue),
};
}
return labelDefinition;
}
getLocalizedLabelDefinitions(labelDefinitions) {
return labelDefinitions.map((labelDefinition) => this.getLocalizedLabelDefinition(labelDefinition));
}
getLocalizedContentDescriptor(descriptor) {
const clone = new Descriptor(descriptor);
clone.fields.forEach((field) => this.getLocalizedContentField(field));
clone.categories.forEach((category) => this.getLocalizedCategoryDescription(category));
return clone;
}
getLocalizedContentItems(items) {
return items.map((item) => this.getLocalizedContentItem(item));
}
getLocalizedContent(content) {
return new Content(this.getLocalizedContentDescriptor(content.descriptor), this.getLocalizedContentItems(content.contentSet));
}
getLocalizedElementProperties(elem) {
return {
...elem,
label: this.getLocalizedString(elem.label),
};
}
// warning: this function mutates the item
getLocalizedContentItem(item) {
item.label = this.getLocalizedLabelDefinition(item.label);
item.values = Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {});
item.displayValues = Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});
return item;
}
getLocalizedRawValue(value) {
if (typeof value === "string") {
return this.getLocalizedString(value);
}
if (Value.isNavigationValue(value)) {
return {
...value,
label: this.getLocalizedLabelDefinition(value.label),
};
}
if (Value.isNestedContent(value)) {
return value.map((item) => ({
...item,
values: Object.entries(item.values).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {}),
displayValues: Object.entries(item.displayValues).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {}),
}));
}
if (Value.isArray(value)) {
return value.map((v) => this.getLocalizedRawValue(v));
}
if (Value.isMap(value)) {
return Object.entries(value).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedRawValue(v) }), {});
}
return value;
}
// warning: this function mutates the field
getLocalizedContentField(field) {
field.label = this.getLocalizedString(field.label);
if (field.isPropertiesField()) {
if (field.isStructPropertiesField()) {
field.memberFields = field.memberFields.map((m) => this.getLocalizedContentField(m));
}
else if (field.isArrayPropertiesField()) {
field.itemsField = this.getLocalizedContentField(field.itemsField);
}
}
else if (field.isNestedContentField()) {
field.nestedFields = field.nestedFields.map((m) => this.getLocalizedContentField(m));
}
return field;
}
// warning: this function mutates the category
getLocalizedCategoryDescription(category) {
category.label = this.getLocalizedString(category.label);
category.description = this.getLocalizedString(category.description);
return category;
}
getLocalizedNode(node) {
return {
...node,
label: this.getLocalizedLabelDefinition(node.label),
...(node.description ? { description: this.getLocalizedString(node.description) } : undefined),
};
}
getLocalizedDisplayValue(value) {
if (typeof value === "undefined") {
return undefined;
}
if (typeof value === "string") {
return this.getLocalizedString(value);
}
if (DisplayValue.isArray(value)) {
return value.map((v) => this.getLocalizedDisplayValue(v));
}
return Object.entries(value).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {});
}
}
//# sourceMappingURL=LocalizationHelper.js.map