@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
33 lines • 1.63 kB
JavaScript
import configuration from "@intlayer/config/built";
import { checkIsURLAbsolute } from "../utils/checkIsURLAbsolute.mjs";
import { getPathWithoutLocale } from "./getPathWithoutLocale.mjs";
const getMultilingualUrls = (url, locales = configuration.internationalization.locales, defaultLocale = configuration.internationalization.defaultLocale, prefixDefault = configuration.middleware.prefixDefault) => {
const urlWithoutLocale = getPathWithoutLocale(url, locales);
const isAbsoluteUrl = checkIsURLAbsolute(urlWithoutLocale);
const parsedUrl = isAbsoluteUrl ? new URL(urlWithoutLocale) : new URL(urlWithoutLocale, "http://example.com");
let pathname = parsedUrl.pathname;
if (!pathname.startsWith("/")) {
pathname = `/${pathname}`;
}
const baseUrl = isAbsoluteUrl ? `${parsedUrl.protocol}//${parsedUrl.host}` : "";
const multilingualUrls = locales.reduce(
(acc, locale) => {
const isDefaultLocale = locale.toString() === defaultLocale.toString();
const shouldPrefix = prefixDefault || !isDefaultLocale;
let localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;
if (localizedPath.length > 1 && localizedPath.endsWith("/")) {
localizedPath = localizedPath.slice(0, -1);
}
const localizedUrl = isAbsoluteUrl ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}` : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;
acc[locale] = localizedUrl;
return acc;
},
{}
// Initialize an empty object
);
return multilingualUrls;
};
export {
getMultilingualUrls
};
//# sourceMappingURL=getMultilingualUrls.mjs.map