UNPKG

@lucidcms/core

Version:

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

1 lines 3.03 kB
{"version":3,"file":"role-translations.mjs","names":[],"sources":["../../../../src/services/roles/helpers/role-translations.ts"],"sourcesContent":["import type { Insert, LucidRoleTranslations } from \"../../../libs/db/types.js\";\nimport type { AdminCopyInput, Translator } from \"../../../libs/i18n/types.js\";\nimport type { Config } from \"../../../types/config.js\";\nimport type { TranslationsObj } from \"../../../types/shared.js\";\n\nexport type RoleTranslationInput = TranslationsObj[];\n\n/**\n * Reads a specific configured-locale role translation value from normalized input.\n */\nexport const getTranslationValue = (\n\ttranslations: RoleTranslationInput | undefined,\n\tlocaleCode: string,\n) => {\n\treturn translations?.find(\n\t\t(translation) => translation.localeCode === localeCode,\n\t)?.value;\n};\n\n/**\n * Converts config role copy into the same translation array shape used by the role APIs.\n */\nexport const normalizeTranslationArray = (\n\tvalue: AdminCopyInput | undefined,\n\tconfig: Pick<Config, \"i18n\">,\n\ttranslate: Translator,\n): RoleTranslationInput => {\n\tif (value === undefined) return [];\n\n\treturn config.i18n.locales.map((locale) => {\n\t\treturn {\n\t\t\tlocaleCode: locale.code,\n\t\t\tvalue: translate.forLocale(locale.code)(value) ?? null,\n\t\t};\n\t});\n};\n\n/**\n * Finds every configured locale represented across role name and description translations.\n */\nconst collectLocales = (\n\ttranslations: Array<RoleTranslationInput | undefined>,\n) => {\n\tconst locales = new Set<string>();\n\tfor (const translationGroup of translations) {\n\t\tfor (const translation of translationGroup ?? []) {\n\t\t\tif (translation.localeCode !== null) locales.add(translation.localeCode);\n\t\t}\n\t}\n\treturn locales;\n};\n\n/**\n * Builds role translation rows so config sync and API writes share the same persistence shape.\n */\nexport const prepareRoleTranslations = (props: {\n\tname?: RoleTranslationInput;\n\tdescription?: RoleTranslationInput;\n\troleId: number;\n}): Array<Omit<Insert<LucidRoleTranslations>, \"id\">> => {\n\tconst locales = collectLocales([props.name, props.description]);\n\n\treturn Array.from(locales).map((localeCode) => ({\n\t\trole_id: props.roleId,\n\t\tlocale_code: localeCode,\n\t\tname: getTranslationValue(props.name, localeCode) ?? null,\n\t\tdescription: getTranslationValue(props.description, localeCode) ?? null,\n\t}));\n};\n"],"mappings":"AAUA,MAAa,GACZ,EACA,IAEO,GAAc,KACnB,GAAgB,EAAY,aAAe,CAC7C,CAAC,EAAE,MAMS,GACZ,EACA,EACA,IAEI,IAAU,IAAA,GAAkB,CAAC,EAE1B,EAAO,KAAK,QAAQ,IAAK,IACxB,CACN,WAAY,EAAO,KACnB,MAAO,EAAU,UAAU,EAAO,IAAI,CAAC,CAAC,CAAK,GAAK,IACnD,EACA,EAMI,EACL,GACI,CACJ,IAAM,EAAU,IAAI,IACpB,IAAK,IAAM,KAAoB,EAC9B,IAAK,IAAM,KAAe,GAAoB,CAAC,EAC1C,EAAY,aAAe,MAAM,EAAQ,IAAI,EAAY,UAAU,EAGzE,OAAO,CACR,EAKa,EAA2B,GAIgB,CACvD,IAAM,EAAU,EAAe,CAAC,EAAM,KAAM,EAAM,WAAW,CAAC,EAE9D,OAAO,MAAM,KAAK,CAAO,CAAC,CAAC,IAAK,IAAgB,CAC/C,QAAS,EAAM,OACf,YAAa,EACb,KAAM,EAAoB,EAAM,KAAM,CAAU,GAAK,KACrD,YAAa,EAAoB,EAAM,YAAa,CAAU,GAAK,IACpE,EAAE,CACH"}