@bemedev/i18n
Version:
Internationalization library for Bemedev projects, providing utilities for managing translations and locale-specific content.
1 lines • 2.51 kB
Source Map (JSON)
{"version":3,"file":"translation.cjs","sources":["../src/translation.ts"],"sourcesContent":["import { create } from './class';\nimport { defineTranslation } from './helpers';\nimport {\n infer18,\n type LanguageMessages,\n type RequiredTranslations,\n type Simple18,\n type Translate_F2,\n} from './types';\n\nconst _translation = <const R extends LanguageMessages>(\n config: R,\n locale: string,\n) => {\n const _class = create(config, locale);\n\n const out = (...args: any[]) =>\n (_class as any).translate(...args).to(locale);\n out.config = config;\n\n return out as Translate_F2<R>;\n};\n\n/**\n * Creates a translation object from a function or object.\n * Utility helper to create type-safe translations that match a root config.\n *\n * @param func - A function that receives defineTranslation or a plain translation object\n * @param _ - Optional root config for type inference (not used at runtime)\n * @returns The resolved translation object\n */\nexport const translation = <const R extends LanguageMessages>(\n func: ((define: typeof defineTranslation) => R) | R,\n locale = 'en-US',\n) => {\n const isFunction = typeof func === 'function';\n const config = isFunction ? func(defineTranslation) : func;\n\n return _translation(config, locale);\n};\n\ntranslation.derived = <const R extends LanguageMessages>(\n func:\n | ((define: typeof defineTranslation) => RequiredTranslations<R>)\n | RequiredTranslations<R>,\n locale = 'en-US',\n) => {\n return translation(func, locale);\n};\n\ntranslation.fromMachine = <const T extends Simple18>(\n func: ((define: typeof defineTranslation) => infer18<T>) | infer18<T>,\n locale = 'en-US',\n) => translation(func, locale);\n"],"names":["_class","create","defineTranslation"],"mappings":";;;;;AAUA,MAAM,YAAY,GAAG,CACnB,MAAS,EACT,MAAc,KACZ;IACF,MAAMA,QAAM,GAAGC,aAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAErC,MAAM,GAAG,GAAG,CAAC,GAAG,IAAW,KACxBD,QAAc,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC;AAC/C,IAAA,GAAG,CAAC,MAAM,GAAG,MAAM;AAEnB,IAAA,OAAO,GAAsB;AAC/B,CAAC;AAED;;;;;;;AAOG;AACI,MAAM,WAAW,GAAG,CACzB,IAAmD,EACnD,MAAM,GAAG,OAAO,KACd;AACF,IAAA,MAAM,UAAU,GAAG,OAAO,IAAI,KAAK,UAAU;AAC7C,IAAA,MAAM,MAAM,GAAG,UAAU,GAAG,IAAI,CAACE,yBAAiB,CAAC,GAAG,IAAI;AAE1D,IAAA,OAAO,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC;AACrC;AAEA,WAAW,CAAC,OAAO,GAAG,CACpB,IAE2B,EAC3B,MAAM,GAAG,OAAO,KACd;AACF,IAAA,OAAO,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC;AAClC,CAAC;AAED,WAAW,CAAC,WAAW,GAAG,CACxB,IAAqE,EACrE,MAAM,GAAG,OAAO,KACb,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC;;;;"}