UNPKG

@intlayer/core

Version:

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

45 lines (43 loc) 2.44 kB
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs'); const require_utils_checkIsURLAbsolute = require('../utils/checkIsURLAbsolute.cjs'); let _intlayer_config_client = require("@intlayer/config/client"); let _intlayer_config_built = require("@intlayer/config/built"); _intlayer_config_built = require_rolldown_runtime.__toESM(_intlayer_config_built); //#region src/localization/getLocaleFromPath.ts /** * Extracts the locale segment from the given URL or pathname based on the routing mode. * * Mode Behaviors: * - 'prefix-no-default': Checks path prefiIf no prefix found, assumes default locale. * - 'prefix-all': Checks path prefix. * - 'search-params': Checks for 'locale' query parameter. * - 'no-prefix': Returns undefined (or default if fallback is true). * * @param inputUrl - The complete URL string or pathname to process. * @returns The detected locale, default locale (if fallback/implicit), or undefined. */ const getLocaleFromPath = (inputUrl = "/", options) => { const { defaultLocale, locales, mode } = { defaultLocale: _intlayer_config_built.default?.internationalization?.defaultLocale ?? _intlayer_config_client.DefaultValues.Internationalization.DEFAULT_LOCALE, mode: _intlayer_config_built.default?.routing?.mode ?? _intlayer_config_client.DefaultValues.Routing.ROUTING_MODE, locales: _intlayer_config_built.default?.internationalization?.locales ?? _intlayer_config_client.DefaultValues.Internationalization.LOCALES, ...options }; if (!defaultLocale || !locales) return _intlayer_config_client.DefaultValues.Internationalization.DEFAULT_LOCALE; const isAbsoluteUrl = require_utils_checkIsURLAbsolute.checkIsURLAbsolute(inputUrl); let fixedInputUrl = inputUrl; if (inputUrl?.endsWith("/") && inputUrl.length > 1) fixedInputUrl = inputUrl.slice(0, -1); const url = isAbsoluteUrl ? new URL(fixedInputUrl) : new URL(fixedInputUrl, "http://example.com"); if (mode === "search-params") { const localeParam = url.searchParams.get("locale"); if (localeParam && locales.includes(localeParam)) return localeParam; return defaultLocale; } if (mode === "no-prefix") return defaultLocale; const firstSegment = url.pathname.split("/")[1]; if (firstSegment && locales.includes(firstSegment)) return firstSegment; if (mode === "prefix-no-default") return defaultLocale; }; //#endregion exports.getLocaleFromPath = getLocaleFromPath; //# sourceMappingURL=getLocaleFromPath.cjs.map