@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
1 lines • 1.72 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/interpreter/getTranslation.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/config/client';\nimport { LanguageContent } from '../types';\n\n/**\n *\n * Allow to pick a content based on a locale.\n * If not locale found, it will return the content related to the default locale.\n *\n * Return either the content editor, or the content itself depending on the configuration.\n *\n * Usage:\n *\n * ```ts\n * const content = getTranslation<string>({\n * en: 'Hello',\n * fr: 'Bonjour',\n * }, 'fr');\n * // 'Bonjour'\n * ```\n *\n * Using TypeScript:\n * - this function will require each locale to be defined if defined in the project configuration.\n * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.\n */\nexport const getTranslation = <Content = string>(\n languageContent: LanguageContent<Content>,\n locale?: LocalesValues,\n fallback: boolean = true\n): Content => {\n const { defaultLocale } = configuration?.internationalization;\n\n let result =\n languageContent[\n (locale ?? defaultLocale) as unknown as keyof typeof languageContent\n ];\n\n if (fallback && !result) {\n result =\n languageContent[defaultLocale as unknown as keyof typeof languageContent];\n }\n\n return result as unknown as Content;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAyBnB,MAAM,iBAAiB,CAC5B,iBACA,QACA,WAAoB,SACR;AACZ,QAAM,EAAE,cAAc,IAAI,aAAAA,SAAe;AAEzC,MAAI,SACF,gBACG,UAAU,aACb;AAEF,MAAI,YAAY,CAAC,QAAQ;AACvB,aACE,gBAAgB,aAAwD;AAAA,EAC5E;AAEA,SAAO;AACT;","names":["configuration"]}