@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
1 lines • 2.1 kB
Source Map (JSON)
{"version":3,"file":"getLocalizedContent.mjs","names":["plugins: Plugins[]"],"sources":["../../../src/deepTransformPlugins/getLocalizedContent.ts"],"sourcesContent":["import type {\n ContentNode,\n DeclaredLocales,\n Dictionary,\n LocalesValues,\n} from '@intlayer/types';\nimport {\n type DeepTransformContent,\n type NodeProps,\n type Plugins,\n translationPlugin,\n} from '../interpreter';\nimport { deepTransformNode } from '../interpreter/getContent/deepTransform';\n\n/**\n * Transforms a node in a single pass, applying each plugin as needed.\n * In comparison to `getContent`, this function will only apply the translation plugin.\n * It will not transform enumerations, insertions, or other content types.\n *\n * @param node The node to transform.\n * @param locale The locale to use if your transformers need it (e.g. for translations).\n */\nexport const getLocalizedContent = <\n T extends ContentNode,\n L extends LocalesValues = DeclaredLocales,\n>(\n node: T,\n locale: L,\n nodeProps: NodeProps,\n fallback?: LocalesValues // fallback mean that if field is not translated, it will use the default locale\n) => {\n const plugins: Plugins[] = [\n translationPlugin(locale, fallback),\n ...(nodeProps.plugins ?? []),\n ];\n\n return deepTransformNode(node, {\n ...nodeProps,\n plugins,\n }) as DeepTransformContent<T>;\n};\n\nexport const getPerLocaleDictionary = <\n T extends Dictionary,\n L extends LocalesValues,\n>(\n dictionary: T,\n locale: L,\n fallback?: LocalesValues\n): Dictionary => ({\n ...dictionary,\n locale,\n content: getLocalizedContent(\n dictionary.content,\n locale,\n {\n dictionaryKey: dictionary.key,\n keyPath: [],\n plugins: [],\n },\n fallback\n ),\n});\n"],"mappings":";;;;;;;;;;;;AAsBA,MAAa,uBAIX,MACA,QACA,WACA,aACG;CACH,MAAMA,UAAqB,CACzB,kBAAkB,QAAQ,SAAS,EACnC,GAAI,UAAU,WAAW,EAAE,CAC5B;AAED,QAAO,kBAAkB,MAAM;EAC7B,GAAG;EACH;EACD,CAAC;;AAGJ,MAAa,0BAIX,YACA,QACA,cACgB;CAChB,GAAG;CACH;CACA,SAAS,oBACP,WAAW,SACX,QACA;EACE,eAAe,WAAW;EAC1B,SAAS,EAAE;EACX,SAAS,EAAE;EACZ,EACD,SACD;CACF"}