@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
25 lines • 875 B
JavaScript
import configuration from "@intlayer/config/built";
import { checkIsURLAbsolute } from "../utils/checkIsURLAbsolute.mjs";
const getLocaleFromPath = (inputUrl) => {
const { defaultLocale, locales } = configuration.internationalization;
const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);
let fixedInputUrl = inputUrl;
if (inputUrl.endsWith("/")) {
fixedInputUrl = inputUrl.slice(0, -1);
}
const url = isAbsoluteUrl ? new URL(fixedInputUrl) : new URL(fixedInputUrl, "http://example.com");
const pathname = url.pathname;
if (!pathname.startsWith("/")) {
return defaultLocale;
}
const pathSegments = pathname.split("/");
const firstSegment = pathSegments[1];
if (firstSegment && locales.includes(firstSegment)) {
return firstSegment;
}
return defaultLocale;
};
export {
getLocaleFromPath
};
//# sourceMappingURL=getLocaleFromPath.mjs.map