UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

1 lines 1.62 kB
{"version":3,"file":"getLocale.cjs","names":["configuration","Locales"],"sources":["../../../src/localization/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { getLocaleFromStorage, localeDetector } from '@intlayer/core';\nimport { type Locale, Locales } from '@intlayer/types';\n\nexport type RequestContext = {\n getHeader?: (name: string) => string | null | undefined;\n getCookie?: (name: string) => string | null | undefined;\n getAllHeaders?: () =>\n | Record<string, string>\n | Promise<Record<string, string>>;\n};\n\nexport const getLocale = async (ctx: RequestContext = {}): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? Locales.ENGLISH;\n\n // Try locale from storage (cookie or header)\n const storedLocale = getLocaleFromStorage({\n getCookie: ctx.getCookie,\n getHeader: ctx.getHeader,\n });\n\n if (storedLocale) return storedLocale as Locale;\n\n // Fallback to Accept-Language negotiation\n const allHeaders = (await ctx.getAllHeaders?.()) ?? (ctx.getHeader ? {} : {}); // fallback empty object if none provided\n\n const userFallbackLocale = localeDetector(allHeaders);\n if (userFallbackLocale) return userFallbackLocale as Locale;\n\n // Default locale\n return defaultLocale;\n};\n"],"mappings":";;;;;;;AAYA,MAAa,YAAY,OAAO,MAAsB,EAAE,KAAsB;CAC5E,MAAM,gBACJA,iCAAe,sBAAsB,iBAAiBC,yBAAQ;CAGhE,MAAM,yDAAoC;EACxC,WAAW,IAAI;EACf,WAAW,IAAI;EAChB,CAAC;AAEF,KAAI,aAAc,QAAO;CAKzB,MAAM,yDAFc,MAAM,IAAI,iBAAiB,KAAM,IAAI,YAAY,EAAE,GAAG,EAAE,EAEvB;AACrD,KAAI,mBAAoB,QAAO;AAG/B,QAAO"}