UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

1 lines 11.9 kB
{"version":3,"file":"insertContentInDictionary.cjs","names":["NodeType","newLocaleContent: any","result: any"],"sources":["../../../src/deepTransformPlugins/insertContentInDictionary.ts"],"sourcesContent":["import { type Dictionary, type LocalesValues, NodeType } from '@intlayer/types';\n\n// {\n// {\n// title: 'このページ',\n// linkLabel: 'セクションへ移動',\n// collapseButton: { label: '折りたたむ' }\n// },\n// }\n// {\n// dictionary: {\n// key: 'aside-navigation',\n// content: {\n// title: {\n// nodeType: 'translation',\n// translation: {\n// ar: 'في هذه الصفحة',\n// de: 'Auf dieser Seite',\n// en: 'In this page',\n// 'en-GB': 'On this page',\n// es: 'En esta página',\n// fr: 'Dans cette page',\n// hi: 'इस पृष्ठ में',\n// it: 'In questa pagina',\n// ko: '이 페이지에서',\n// pt: 'Nesta página',\n// ru: 'На этой странице',\n// tr: 'Bu sayfada',\n// zh: '在此页面'\n// }\n// },\n// linkLabel: {\n// nodeType: 'translation',\n// translation: {\n// ar: 'اذهب إلى القسم',\n// de: 'Gehe zur Sektion',\n// en: 'Go to section',\n// 'en-GB': 'Go to section',\n// es: 'Ir a la sección',\n// fr: 'Aller à la section',\n// hi: 'सेक्शन पर जाएं',\n// it: 'Vai alla sezione',\n// ja: 'セクションへ行く',\n// ko: '섹션으로 이동',\n// pt: 'Ir para a seção',\n// ru: 'Перейти к разделу',\n// tr: 'Bölüme git',\n// zh: '转到节'\n// }\n// },\n// collapseButton: {\n// label: {\n// nodeType: 'translation',\n// translation: {\n// en: 'Collapse',\n// fr: 'Réduire',\n// es: 'Colapsar',\n// 'en-GB': 'Collapse',\n// de: 'Zuklappen',\n// ja: '折りたたむ',\n// ko: '접기',\n// zh: '折叠',\n// it: 'Comprimi',\n// pt: 'Recolher',\n// hi: 'संकुचित करें',\n// ar: 'اطوي التوسيع',\n// ru: 'Свернуть',\n// tr: 'Daralt'\n// }\n// }\n// }\n// },\n// localId: 'aside-navigation::local::src/components/AsideNavigation/asideNavigation.content.ts',\n// location: 'local',\n// filePath: 'src/components/AsideNavigation/asideNavigation.content.ts'\n// }\n// }\n\n/**\n * Deep merge helper that handles translation blocks\n * @param target - The target object (dictionary content)\n * @param source - The source object (new content to merge)\n * @param locale - Optional locale to target specific translation\n * @returns Merged content\n */\nconst deepMergeContent = (\n target: any,\n source: any,\n locale?: LocalesValues\n): any => {\n // If source is null or undefined, return target\n if (source === null || source === undefined) return target;\n\n // If target is null or undefined, initialize based on source type\n if (target === null || target === undefined) {\n if (Array.isArray(source)) {\n target = [];\n } else if (typeof source === 'object') {\n target = {};\n } else {\n return source;\n }\n }\n\n // Handle translation nodes FIRST (before arrays) to support arrays within translations\n if (\n target &&\n typeof target === 'object' &&\n target.nodeType === NodeType.Translation\n ) {\n // Target is a translation block\n if (locale) {\n // Update only the specific locale - create new object to preserve order\n const existingLocaleContent = target.translation[locale];\n let newLocaleContent: any;\n\n if (typeof source === 'object' && !Array.isArray(source)) {\n // Source is an object, need to deep merge for this locale\n newLocaleContent = deepMergeContent(\n existingLocaleContent,\n source,\n undefined // Don't pass locale down for nested content\n );\n } else if (Array.isArray(source)) {\n // Source is an array, set it directly for this locale\n newLocaleContent = source;\n } else {\n // Source is a primitive, directly set it\n newLocaleContent = source;\n }\n\n // Return new object with locale appended at the end (preserve insertion order)\n return {\n ...target,\n translation: {\n ...target.translation,\n [locale]: newLocaleContent,\n },\n };\n } else {\n // No locale specified, replace/merge the entire translation\n if (\n typeof source === 'object' &&\n !Array.isArray(source) &&\n source.nodeType === NodeType.Translation\n ) {\n // Source is also a translation node, merge translations\n return {\n ...target,\n translation: {\n ...target.translation,\n ...source.translation,\n },\n };\n } else {\n // Source is not a translation node, wrap it\n return processNewContent(source, locale);\n }\n }\n }\n\n // Handle arrays\n if (Array.isArray(source)) {\n if (locale && Array.isArray(target)) {\n // When locale is specified and target is also an array, merge element by element\n const result = [];\n const maxLength = Math.max(source.length, target.length);\n\n for (let i = 0; i < maxLength; i++) {\n if (i < source.length) {\n if (i < target.length && target[i] !== undefined) {\n // Both source and target have element at this index - merge them\n result[i] = deepMergeContent(target[i], source[i], locale);\n } else {\n // Only source has element at this index - process new content\n result[i] = processNewContent(source[i], locale);\n }\n } else {\n // Only target has element at this index - keep it\n result[i] = target[i];\n }\n }\n\n return result;\n } else {\n // No locale or target is not an array - replace entirely\n return source.map((item) => {\n // Process each item in case it needs locale wrapping\n if (\n item &&\n typeof item === 'object' &&\n item.nodeType === NodeType.Translation\n ) {\n return item;\n }\n return processNewContent(item, locale);\n });\n }\n }\n\n // Handle objects\n if (typeof source === 'object' && !Array.isArray(source)) {\n if (typeof target !== 'object' || Array.isArray(target)) {\n target = {};\n }\n\n // Create new object to preserve key order and avoid mutation\n const result: any = { ...target };\n\n for (const key in source) {\n if (Object.hasOwn(source, key)) {\n if (target[key] !== undefined) {\n result[key] = deepMergeContent(target[key], source[key], locale);\n } else {\n result[key] = processNewContent(source[key], locale);\n }\n }\n }\n\n return result;\n }\n\n // For primitives, just return the source\n return source;\n};\n\n/**\n * Process new content that doesn't exist in target\n * @param content - New content to process\n * @param locale - Optional locale\n * @returns Processed content\n */\nconst processNewContent = (content: any, locale?: LocalesValues): any => {\n // Handle null and undefined\n if (content === null || content === undefined) {\n return content;\n }\n\n // If content is already a translation node, return as-is\n if (\n content &&\n typeof content === 'object' &&\n content.nodeType === NodeType.Translation\n ) {\n return content;\n }\n\n // Handle arrays\n if (Array.isArray(content)) {\n return content.map((item) => processNewContent(item, locale));\n }\n\n // Handle objects\n if (content && typeof content === 'object') {\n const result: any = {};\n\n for (const key in content) {\n if (Object.hasOwn(content, key)) {\n result[key] = processNewContent(content[key], locale);\n }\n }\n return result;\n }\n\n // Handle primitives\n if (locale) {\n // Wrap in translation node with the specific locale\n return {\n nodeType: NodeType.Translation,\n translation: {\n [locale]: content,\n },\n };\n } else {\n // If no locale, just return the content as-is (don't wrap)\n return content;\n }\n};\n\n/**\n * Insert content into a dictionary with deep merge support\n * Handles translation blocks and nested structures\n *\n * @param dictionary - The dictionary to insert content into\n * @param content - The content to insert\n * @param locale - Optional locale to target specific translation\n * @returns Updated dictionary\n *\n * @example\n * // Without locale - deep merge all content\n * insertContentInDictionary(dictionary, { title: 'New Title' })\n *\n * @example\n * // With locale - insert content for specific locale\n * insertContentInDictionary(dictionary, { title: 'このページ' }, 'ja')\n */\nexport const insertContentInDictionary = (\n dictionary: Dictionary,\n content: Dictionary['content'],\n locale?: LocalesValues\n): Dictionary => {\n const mergedContent = deepMergeContent(\n Array.isArray(dictionary.content)\n ? [...dictionary.content]\n : { ...dictionary.content },\n content,\n locale\n );\n\n return {\n ...dictionary,\n content: mergedContent,\n };\n};\n"],"mappings":";;;;;;;;;;;;AAqFA,MAAM,oBACJ,QACA,QACA,WACQ;AAER,KAAI,WAAW,QAAQ,WAAW,OAAW,QAAO;AAGpD,KAAI,WAAW,QAAQ,WAAW,OAChC,KAAI,MAAM,QAAQ,OAAO,CACvB,UAAS,EAAE;UACF,OAAO,WAAW,SAC3B,UAAS,EAAE;KAEX,QAAO;AAKX,KACE,UACA,OAAO,WAAW,YAClB,OAAO,aAAaA,0BAAS,YAG7B,KAAI,QAAQ;EAEV,MAAM,wBAAwB,OAAO,YAAY;EACjD,IAAIC;AAEJ,MAAI,OAAO,WAAW,YAAY,CAAC,MAAM,QAAQ,OAAO,CAEtD,oBAAmB,iBACjB,uBACA,QACA,OACD;WACQ,MAAM,QAAQ,OAAO,CAE9B,oBAAmB;MAGnB,oBAAmB;AAIrB,SAAO;GACL,GAAG;GACH,aAAa;IACX,GAAG,OAAO;KACT,SAAS;IACX;GACF;YAIC,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,OAAO,IACtB,OAAO,aAAaD,0BAAS,YAG7B,QAAO;EACL,GAAG;EACH,aAAa;GACX,GAAG,OAAO;GACV,GAAG,OAAO;GACX;EACF;KAGD,QAAO,kBAAkB,QAAQ,OAAO;AAM9C,KAAI,MAAM,QAAQ,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