@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 4.25 kB
Source Map (JSON)
{"version":3,"file":"translations.mjs","names":["coreEnAdmin","coreEnServer"],"sources":["../../../src/libs/i18n/translations.ts"],"sourcesContent":["import coreEnAdmin from \"../../translations/en.admin.json\" with {\n\ttype: \"json\",\n};\nimport coreEnServer from \"../../translations/en.server.json\" with {\n\ttype: \"json\",\n};\nimport type {\n\tTranslationBundle,\n\tTranslationBundles,\n\tTranslationValues,\n} from \"./types.js\";\n\n/**\n * Creates a complete, empty locale bundle so merge operations can safely add\n * admin and server keys independently.\n */\nconst emptyBundle = (): TranslationBundle => ({\n\tadmin: {},\n\tserver: {},\n});\n\n/**\n * Built-in Lucid English translations.\n *\n * Most application code should use `copy`, `translate`, or\n * `createTranslator` instead of reading this object directly.\n */\nexport const coreTranslations: TranslationBundles = {\n\ten: {\n\t\tadmin: coreEnAdmin,\n\t\tserver: coreEnServer,\n\t},\n};\n\n/**\n * Ensures a partial locale bundle always has both admin and server scopes.\n *\n * @example\n * ```ts\n * const bundle = normalizeTranslationBundle({\n * admin: { \"collections.posts.name\": \"Posts\" },\n * });\n * ```\n */\nexport const normalizeTranslationBundle = (\n\tbundle: Partial<TranslationBundle> | undefined,\n): TranslationBundle => ({\n\tadmin: { ...(bundle?.admin ?? {}) },\n\tserver: { ...(bundle?.server ?? {}) },\n});\n\n/**\n * Normalizes all locale bundles so every locale contains admin and server\n * scopes.\n *\n * @example\n * ```ts\n * const translations = normalizeTranslationBundles({\n * fr: { server: { \"posts.not.found\": \"Article introuvable.\" } },\n * });\n * ```\n */\nexport const normalizeTranslationBundles = (\n\tbundles?: Record<string, Partial<TranslationBundle>>,\n): TranslationBundles => {\n\tconst normalized: TranslationBundles = {};\n\n\tfor (const [locale, bundle] of Object.entries(bundles ?? {})) {\n\t\tnormalized[locale] = normalizeTranslationBundle(bundle);\n\t}\n\n\treturn normalized;\n};\n\n/**\n * Merges translation bundles in order, with later sources overriding earlier\n * ones.\n *\n * Use this in build tooling when combining generated, plugin, or project\n * translation bundles before creating a translation store.\n *\n * @example\n * ```ts\n * const translations = mergeTranslationBundles(coreTranslations, pluginTranslations);\n * ```\n */\nexport const mergeTranslationBundles = (\n\t...sources: Array<Record<string, Partial<TranslationBundle>> | undefined>\n): TranslationBundles => {\n\tconst merged: TranslationBundles = {};\n\n\tfor (const source of sources) {\n\t\tfor (const [locale, bundle] of Object.entries(source ?? {})) {\n\t\t\tmerged[locale] = merged[locale] ?? emptyBundle();\n\t\t\tmerged[locale].admin = {\n\t\t\t\t...merged[locale].admin,\n\t\t\t\t...(bundle.admin ?? {}),\n\t\t\t};\n\t\t\tmerged[locale].server = {\n\t\t\t\t...merged[locale].server,\n\t\t\t\t...(bundle.server ?? {}),\n\t\t\t};\n\t\t}\n\t}\n\n\treturn merged;\n};\n\n/**\n * Replaces `{{value}}` placeholders with the provided interpolation data.\n *\n * @example\n * ```ts\n * formatTranslation(\"Hello {{name}}\", { name: \"Ada\" });\n * ```\n */\nexport const formatTranslation = (\n\ttranslation: string,\n\tvalues?: TranslationValues,\n) => {\n\tif (!values) return translation;\n\n\treturn translation.replace(\n\t\t/\\{\\{(\\w+)\\}\\}/g,\n\t\t(_, key) => values[key as keyof TranslationValues]?.toString() ?? \"\",\n\t);\n};\n"],"mappings":"+FAgBA,MAAM,OAAwC,CAC7C,MAAO,CAAC,EACR,OAAQ,CAAC,CACV,GAQa,EAAuC,CACnD,GAAI,CACH,MAAOA,EACP,OAAQC,CACT,CACD,EAYa,EACZ,IACwB,CACxB,MAAO,CAAE,GAAI,GAAQ,OAAS,CAAC,CAAG,EAClC,OAAQ,CAAE,GAAI,GAAQ,QAAU,CAAC,CAAG,CACrC,GAaa,EACZ,GACwB,CACxB,IAAM,EAAiC,CAAC,EAExC,IAAK,GAAM,CAAC,EAAQ,KAAW,OAAO,QAAQ,GAAW,CAAC,CAAC,EAC1D,EAAW,GAAU,EAA2B,CAAM,EAGvD,OAAO,CACR,EAca,GACZ,GAAG,IACqB,CACxB,IAAM,EAA6B,CAAC,EAEpC,IAAK,IAAM,KAAU,EACpB,IAAK,GAAM,CAAC,EAAQ,KAAW,OAAO,QAAQ,GAAU,CAAC,CAAC,EACzD,EAAO,GAAU,EAAO,IAAW,EAAY,EAC/C,EAAO,EAAO,CAAC,MAAQ,CACtB,GAAG,EAAO,EAAO,CAAC,MAClB,GAAI,EAAO,OAAS,CAAC,CACtB,EACA,EAAO,EAAO,CAAC,OAAS,CACvB,GAAG,EAAO,EAAO,CAAC,OAClB,GAAI,EAAO,QAAU,CAAC,CACvB,EAIF,OAAO,CACR,EAUa,GACZ,EACA,IAEK,EAEE,EAAY,QAClB,kBACC,EAAG,IAAQ,EAAO,EAA+B,EAAE,SAAS,GAAK,EACnE,EALoB"}