UNPKG

@bemedev/i18n

Version:

Internationalization library for Bemedev projects, providing utilities for managing translations and locale-specific content.

1 lines 9.09 kB
{"version":3,"file":"helpers.cjs","sources":["../src/helpers.ts"],"sourcesContent":["import type {\n ExtractParamOptions,\n Fn,\n LanguageMessages,\n ParamOptions,\n} from './types';\n\nexport const defineTranslation = <\n S extends string,\n O extends ExtractParamOptions<S>,\n>(\n string: S,\n options: O,\n): [S, O] => {\n return [string, options];\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 function getTranslation(\n locale: string,\n translations: LanguageMessages,\n key: string,\n args?: any,\n) {\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 (Array.isArray(translation)) {\n const canReturn =\n translation.length !== 2 || typeof translation[1] === 'string';\n\n if (canReturn) return translation;\n const [str, options] = translation;\n\n return performSubstitution(locale, str, argObj, options);\n }\n\n const isObject = typeof translation === 'object' && translation !== null;\n if (isObject) {\n const obj: any = {};\n const entries = Object.entries(translation)\n .filter(([, v]) => {\n const notValid =\n Array.isArray(v) && v.length === 2 && typeof v[1] === 'object';\n return !notValid;\n })\n .map(([k]) => {\n return [\n k,\n getTranslation(locale, translations, `${key}.${k}`, {}),\n ] as const;\n });\n entries.forEach(([k, v]) => {\n obj[k] = v;\n });\n\n return obj;\n }\n\n return undefined;\n}\n\nfunction getTranslationByKey(obj: LanguageMessages, key: string) {\n const keys = key.split('.');\n let currentObj: any = obj;\n const len = keys.length - 1;\n\n let out = undefined;\n\n for (let i = 0; i <= len; i++) {\n const k = keys[i];\n const newObj = currentObj[k];\n if (!newObj) return undefined;\n\n const canReturn =\n typeof newObj === 'string' ||\n (Array.isArray(newObj) &&\n newObj.length === 2 &&\n typeof newObj[0] === 'string');\n\n if (canReturn) {\n out = newObj;\n break;\n }\n if (i === len) {\n out = newObj;\n break;\n }\n\n currentObj = newObj;\n }\n\n return out;\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;\n};\n"],"names":[],"mappings":";;MAOa,iBAAiB,GAAG,CAI/B,MAAS,EACT,OAAU,KACA;AACV,IAAA,OAAO,CAAC,MAAM,EAAE,OAAO,CAA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