UNPKG

@itwin/presentation-common

Version:

Common pieces for iModel.js presentation packages

157 lines 6.7 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * 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 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalizationHelper = void 0; const Content_js_1 = require("./content/Content.js"); const Descriptor_js_1 = require("./content/Descriptor.js"); const Value_js_1 = require("./content/Value.js"); const LabelDefinition_js_1 = require("./LabelDefinition.js"); const KEY_PATTERN = /@[\w\d\-_]+:[\w\d\-\._]+?@/g; /** @internal */ 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 === LabelDefinition_js_1.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_js_1.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_js_1.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_js_1.Value.isNavigationValue(value)) { return { ...value, label: this.getLocalizedLabelDefinition(value.label), }; } if (Value_js_1.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_js_1.Value.isArray(value)) { return value.map((v) => this.getLocalizedRawValue(v)); } if (Value_js_1.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 (Value_js_1.DisplayValue.isArray(value)) { return value.map((v) => this.getLocalizedDisplayValue(v)); } return Object.entries(value).reduce((o, [k, v]) => ({ ...o, [k]: this.getLocalizedDisplayValue(v) }), {}); } } exports.LocalizationHelper = LocalizationHelper; //# sourceMappingURL=LocalizationHelper.js.map