@bemedev/i18n
Version:
Internationalization library for Bemedev projects, providing utilities for managing translations and locale-specific content.
1 lines • 9.25 kB
Source Map (JSON)
{"version":3,"file":"helpers.cjs","sources":["../src/helpers.ts"],"sourcesContent":["import { decompose } from '@bemedev/decompose';\nimport { CustomMessage } from './message';\nimport type {\n DefineTransition_F,\n Fn,\n LanguageMessages,\n ParamOptions,\n} from './types';\n\nexport const defineTranslation: DefineTransition_F = (...args) => {\n return new CustomMessage<any, any>(args[0], args[1] || {});\n};\n\nexport const dt = defineTranslation;\n\nexport type FnBasic<Main extends Fn, Tr extends object> = Tr & Main;\n\nexport const addFn = <Main extends Fn, const Tr extends object = object>(\n main: Main,\n extensions?: Tr,\n): FnBasic<Main, Tr> => {\n const out: any = main;\n\n if (extensions) {\n Object.assign(out, extensions);\n }\n\n return out;\n};\n\nexport const innerArgs = (translation: any, KEY: string, args?: any) => {\n const isCustom = translation instanceof CustomMessage;\n const arg = isCustom ? args?.[KEY] : {};\n return arg;\n};\n\nexport function getTranslation(\n locale: string,\n translations: LanguageMessages,\n key: string,\n args?: any,\n): any {\n const translation = getTranslationByKey(translations, key);\n const argObj = args || {};\n\n if (typeof translation === 'string') {\n return performSubstitution(locale, translation, argObj, {});\n }\n\n if (translation instanceof CustomMessage) {\n const { translate, args } = translation;\n\n return performSubstitution(locale, translate, argObj, args);\n }\n\n if (Array.isArray(translation)) {\n return translation.map((t, index) => {\n const KEY = `${key}.[${index}]`;\n\n return getTranslation(\n locale,\n translations,\n KEY,\n innerArgs(t, KEY, args),\n );\n });\n }\n\n const isObject = typeof translation === 'object' && translation !== null;\n if (isObject) {\n const obj: any = {};\n const entries = Object.entries(translation).map(([k, t]) => {\n const KEY = `${key}.${k}`;\n\n const value = getTranslation(\n locale,\n translations,\n KEY,\n innerArgs(t, KEY, args),\n );\n\n return [k, value] as const;\n });\n\n entries.forEach(([k, v]) => {\n obj[k] = v;\n });\n\n return obj;\n }\n\n return undefined;\n}\n\n/**\n * Retrieves a translation value from a nested language messages object by flattening it and accessing the specified key.\n *\n * Use {@link decompose.low}\n *\n * @param obj - The language messages object to search in, typically a nested structure of translations.\n * @param key - The dot-separated key path to the desired translation (e.g., \"section.subsection.key\").\n * @returns The translation value associated with the key, or undefined if not found.\n */\nconst getTranslationByKey = (obj: LanguageMessages, key: string) => {\n const decomposed = decompose.low(obj, {\n start: false,\n object: 'both',\n sep: '.',\n });\n\n return decomposed[key];\n};\n\nfunction performSubstitution(\n locale: string,\n str: string,\n args: Record<string, any>,\n translationParams: ParamOptions,\n) {\n const entries = Object.entries(args);\n\n return entries.reduce((result, [argKey, argValue]) => {\n const match = result.match(`{${argKey}:?([^}]*)?}`);\n const [replaceKey, argType] = match!;\n\n switch (argType) {\n case 'plural': {\n const pluralMap = translationParams.plural?.[argKey];\n const pluralRules = new Intl.PluralRules(locale, {\n type: pluralMap?.type,\n });\n\n const replacement = pluralMap![pluralRules.select(argValue)]!;\n\n const numberFormatter = new Intl.NumberFormat(\n locale,\n translationParams.plural?.[argKey]?.formatter,\n );\n return result.replace(\n replaceKey,\n replacement.replace(`{?}`, numberFormatter.format(argValue)),\n );\n }\n case 'enum': {\n const replacement = translationParams.enum![argKey][argValue];\n\n return result.replace(replaceKey, replacement);\n }\n case 'number': {\n const numberFormat = new Intl.NumberFormat(\n locale,\n translationParams.number?.[argKey],\n );\n\n return result.replace(replaceKey, numberFormat.format(argValue));\n }\n case 'list': {\n const formatter = new Intl.ListFormat(\n locale,\n translationParams.list?.[argKey],\n );\n return result.replace(replaceKey, formatter.format(argValue));\n }\n case 'date': {\n const dateFormat = new Intl.DateTimeFormat(\n locale,\n translationParams.date?.[argKey],\n );\n return result.replace(replaceKey, dateFormat.format(argValue));\n }\n default:\n return result.replace(replaceKey, String(argValue));\n }\n }, str);\n}\n\nexport const createConfig = <const R extends LanguageMessages>(\n func: ((define: typeof defineTranslation) => R) | R,\n) => {\n const isFunction = typeof func === 'function';\n let config: R;\n if (isFunction) config = func(defineTranslation);\n else config = func;\n\n return config;\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