UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 4.97 kB
{"version":3,"file":"store.mjs","names":[],"sources":["../../../src/libs/i18n/store.ts"],"sourcesContent":["import { parseCopyKey } from \"./copy.js\";\nimport {\n\tcoreTranslations,\n\tformatTranslation,\n\tmergeTranslationBundles,\n\tnormalizeTranslationBundles,\n} from \"./translations.js\";\nimport type {\n\tBoundTranslateCopyOptions,\n\tBoundTranslateOptions,\n\tTranslatableCopy,\n\tTranslationBundles,\n\tTranslationScope,\n\tTranslationStore,\n} from \"./types.js\";\n\n/**\n * Resolves one key from the requested locale, then configured default locale,\n * then English, before falling back to `defaultMessage` or the key itself.\n */\nconst resolveTranslation = (props: {\n\tbundles: TranslationBundles;\n\tdefaultLocale: string;\n\tscope: TranslationScope;\n\tkey: string;\n\tlocale?: string;\n\toptions?: BoundTranslateOptions;\n}) => {\n\tconst targetLocale = props.locale ?? props.defaultLocale;\n\tconst translation =\n\t\tprops.bundles[targetLocale]?.[props.scope][props.key] ??\n\t\tprops.bundles[props.defaultLocale]?.[props.scope][props.key] ??\n\t\tprops.bundles.en?.[props.scope][props.key] ??\n\t\tprops.options?.defaultMessage ??\n\t\tprops.key;\n\n\treturn formatTranslation(translation, props.options?.data);\n};\n\n/**\n * Resolves descriptor values by combining descriptor data with call-site data.\n * Call-site data wins so API boundaries can add or override interpolation.\n */\nconst translateCopy = (\n\tvalue: string | TranslatableCopy | undefined,\n\tprops: {\n\t\tbundles: TranslationBundles;\n\t\tdefaultLocale: string;\n\t\tlocale?: string;\n\t\toptions?: BoundTranslateCopyOptions;\n\t},\n) => {\n\tif (!value) return value;\n\tif (typeof value === \"string\") {\n\t\tconst { scope, key } = parseCopyKey(value);\n\t\treturn resolveTranslation({\n\t\t\tbundles: props.bundles,\n\t\t\tdefaultLocale: props.defaultLocale,\n\t\t\tscope,\n\t\t\tkey,\n\t\t\tlocale: props.locale,\n\t\t\toptions: props.options,\n\t\t});\n\t}\n\tif (value.type === \"lucid.literal\") {\n\t\treturn formatTranslation(value.value, {\n\t\t\t...(value.values ?? {}),\n\t\t\t...(props.options?.data ?? {}),\n\t\t});\n\t}\n\n\treturn resolveTranslation({\n\t\tbundles: props.bundles,\n\t\tdefaultLocale: props.defaultLocale,\n\t\tscope: value.scope,\n\t\tkey: value.key,\n\t\tlocale: props.locale,\n\t\toptions: {\n\t\t\t...props.options,\n\t\t\tdata: {\n\t\t\t\t...(value.values ?? {}),\n\t\t\t\t...(props.options?.data ?? {}),\n\t\t\t},\n\t\t\tdefaultMessage: props.options?.defaultMessage ?? value.defaultMessage,\n\t\t},\n\t});\n};\n\n/**\n * Creates the runtime translation store used by request/service translators.\n * Keeping bundles here prevents loaded translation data from becoming part of\n * the general config object that every subsystem receives.\n */\nexport const createTranslationStore = (props: {\n\tdefaultLocale: string;\n\tbundles: TranslationBundles;\n}): TranslationStore => {\n\tconst bundles = normalizeTranslationBundles(\n\t\tmergeTranslationBundles(coreTranslations, props.bundles),\n\t);\n\n\treturn {\n\t\tdefaultLocale: props.defaultLocale,\n\t\tbundles,\n\t\tresolve: ({ scope, key, locale, options }) =>\n\t\t\tresolveTranslation({\n\t\t\t\tbundles,\n\t\t\t\tdefaultLocale: props.defaultLocale,\n\t\t\t\tscope,\n\t\t\t\tkey,\n\t\t\t\tlocale,\n\t\t\t\toptions,\n\t\t\t}),\n\t\tcopy: (value, copyProps) =>\n\t\t\ttranslateCopy(value, {\n\t\t\t\tbundles,\n\t\t\t\tdefaultLocale: props.defaultLocale,\n\t\t\t\tlocale: copyProps?.locale,\n\t\t\t\toptions: copyProps?.options,\n\t\t\t}),\n\t\tadmin: ({ locale }) => ({\n\t\t\t...(bundles.en?.admin ?? {}),\n\t\t\t...(bundles[props.defaultLocale]?.admin ?? {}),\n\t\t\t...(bundles[locale]?.admin ?? {}),\n\t\t}),\n\t};\n};\n"],"mappings":"qLAoBA,MAAM,EAAsB,GAOtB,CACL,IAAM,EAAe,EAAM,QAAU,E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