@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
37 lines (35 loc) • 1.81 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_utils_checkIsURLAbsolute = require('../utils/checkIsURLAbsolute.cjs');
const require_localization_getPrefix = require('./getPrefix.cjs');
//#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 prefix. If no prefix found, assumes default locale.
* - 'prefix-all': Checks path prefix.
* - 'search-params': Checks for 'locale' query parameter.
* - 'no-prefix': Returns the default locale.
*
* @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 } = require_localization_getPrefix.resolveRoutingConfig(options);
if (!defaultLocale || !locales) return defaultLocale;
const isAbsoluteUrl = require_utils_checkIsURLAbsolute.checkIsURLAbsolute(inputUrl);
const fixedInputUrl = inputUrl?.endsWith("/") && inputUrl.length > 1 ? inputUrl.slice(0, -1) : inputUrl;
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