UNPKG

@vendure/core

Version:

A modern, headless ecommerce framework

118 lines 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.translateEntity = translateEntity; exports.translateDeep = translateDeep; exports.translateTree = translateTree; const constants_1 = require("../../../common/constants"); const errors_1 = require("../../../common/error/errors"); /** * Converts a Translatable entity into the public-facing entity by unwrapping * the translated strings from the matching Translation entity. */ function translateEntity(translatable, languageCode) { let translation; if (translatable.translations) { if (Array.isArray(languageCode)) { for (const lc of languageCode) { translation = translatable.translations.find(t => t.languageCode === lc); if (translation) break; } } else { translation = translatable.translations.find(t => t.languageCode === languageCode); } if (!translation && languageCode !== constants_1.DEFAULT_LANGUAGE_CODE) { translation = translatable.translations.find(t => t.languageCode === constants_1.DEFAULT_LANGUAGE_CODE); } if (!translation) { // If we cannot find any suitable translation, just return the first one to at least // prevent graphql errors when returning the entity. translation = translatable.translations[0]; } } if (!translation) { throw new errors_1.InternalServerError('error.entity-has-no-translation-in-language', { entityName: translatable.constructor.name, languageCode: Array.isArray(languageCode) ? languageCode.join() : languageCode, }); } const translated = Object.create(Object.getPrototypeOf(translatable), Object.getOwnPropertyDescriptors(translatable)); for (const [key, value] of Object.entries(translation)) { if (key === 'customFields') { if (!translated.customFields) { translated.customFields = {}; } Object.assign(translated.customFields, value); } else if (key !== 'base' && key !== 'id' && key !== 'createdAt' && key !== 'updatedAt') { translated[key] = value !== null && value !== void 0 ? value : ''; } } return translated; } /** * Translates an entity and its deeply-nested translatable properties. Supports up to 2 levels of nesting. */ function translateDeep(translatable, languageCode, translatableRelations = []) { let translatedEntity; try { translatedEntity = translateEntity(translatable, languageCode); } catch (e) { translatedEntity = translatable; } for (const path of translatableRelations) { let object; let property; let value; if (Array.isArray(path) && path.length === 2) { const [path0, path1] = path; const valueLevel0 = translatable[path0]; if (Array.isArray(valueLevel0)) { valueLevel0.forEach((nested1, index) => { object = translatedEntity[path0][index]; property = path1; object[property] = translateLeaf(object, property, languageCode); }); property = ''; object = null; } else { object = translatedEntity[path0]; property = path1; value = translateLeaf(object, property, languageCode); } } else { object = translatedEntity; property = path; value = translateLeaf(object, property, languageCode); } if (object && property) { object[property] = value; } } return translatedEntity; } function translateLeaf(object, property, languageCode) { if (object && object[property]) { if (Array.isArray(object[property])) { return object[property].map((nested2) => translateEntity(nested2, languageCode)); } else if (object[property]) { return translateEntity(object[property], languageCode); } } } /** * Translates a tree structure of Translatable entities */ function translateTree(node, languageCode, translatableRelations = []) { const output = translateDeep(node, languageCode, translatableRelations); if (Array.isArray(output.children)) { output.children = output.children.map(child => translateTree(child, languageCode, translatableRelations)); } return output; } //# sourceMappingURL=translate-entity.js.map