UNPKG

@intlayer/core

Version:

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

1 lines 2.99 kB
{"version":3,"file":"getLocaleFromPath.cjs","names":["configuration","Locales","checkIsURLAbsolute"],"sources":["../../../src/localization/getLocaleFromPath.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { checkIsURLAbsolute } from '../utils/checkIsURLAbsolute';\n\n/**\n * Extracts the locale segment from the given URL or pathname if present.\n * If no locale is present, returns the default locale (en).\n *\n * Example:\n *\n * ```ts\n * getLocaleFromPath('/en/dashboard') // Returns 'en'\n * getLocaleFromPath('/fr/dashboard') // Returns 'fr'\n * getLocaleFromPath('/dashboard') // Returns 'en'\n * getLocaleFromPath('dashboard') // Returns 'en'\n * getLocaleFromPath('https://example.com/es/dashboard') // Returns 'es'\n * getLocaleFromPath('https://example.com/fr/dashboard') // Returns 'fr'\n * getLocaleFromPath('https://example.com/dashboard') // Returns 'en'\n * ```\n *\n * @param inputUrl - The complete URL string or pathname to process.\n * @returns The detected locale or default (en) if no locale is found\n */\nexport const getLocaleFromPath = (inputUrl: string): Locale => {\n // Define supported locales array\n const { defaultLocale, locales } = configuration?.internationalization ?? {};\n\n if (!defaultLocale || !locales) {\n return Locales.ENGLISH;\n }\n\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);\n\n let fixedInputUrl = inputUrl;\n\n if (inputUrl.endsWith('/')) {\n fixedInputUrl = inputUrl.slice(0, -1);\n }\n\n // Initialize a URL object if the URL is absolute\n // For relative URLs, use a dummy base to leverage the URL API\n const url = isAbsoluteUrl\n ? new URL(fixedInputUrl)\n : new URL(fixedInputUrl, 'http://example.com');\n\n const pathname = url.pathname;\n\n // Ensure the pathname starts with '/'\n if (!pathname.startsWith('/')) {\n // If not, return the default locale\n return defaultLocale;\n }\n\n // Split the pathname to extract the first segment\n const pathSegments = pathname.split('/');\n const firstSegment = pathSegments[1]; // The segment after the first '/'\n\n // Check if the first segment is a supported locale\n if (firstSegment && locales.includes(firstSegment as Locale)) {\n // Return the detected locale\n return firstSegment as Locale;\n }\n\n // Return the default locale if no locale is found in the path\n return defaultLocale;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAa,qBAAqB,aAA6B;CAE7D,MAAM,EAAE,eAAe,YAAYA,iCAAe,wBAAwB,EAAE;AAE5E,KAAI,CAAC,iBAAiB,CAAC,QACrB,QAAOC,yBAAQ;CAIjB,MAAM,gBAAgBC,oDAAmB,SAAS;CAElD,IAAI,gBAAgB;AAEpB,KAAI,SAAS,SAAS,IAAI,CACxB,iBAAgB,SAAS,MAAM,GAAG,GAAG;CASvC,MAAM,YAJM,gBACR,IAAI,IAAI,cAAc,GACtB,IAAI,IAAI,eAAe,qBAAqB,EAE3B;AAGrB,KAAI,CAAC,SAAS,WAAW,IAAI,CAE3B,QAAO;CAKT,MAAM,eADe,SAAS,MAAM,IAAI,CACN;AAGlC,KAAI,gBAAgB,QAAQ,SAAS,aAAuB,CAE1D,QAAO;AAIT,QAAO"}