164 lines
4.1 KiB
Plaintext
164 lines
4.1 KiB
Plaintext
import { appendForwardSlash, joinPaths } from "@astrojs/internal-helpers/path";
|
|
import { shouldAppendForwardSlash } from "../core/build/util.js";
|
|
import { MissingLocale } from "../core/errors/errors-data.js";
|
|
import { AstroError } from "../core/errors/index.js";
|
|
function getLocaleRelativeUrl({
|
|
locale,
|
|
base,
|
|
locales: _locales,
|
|
trailingSlash,
|
|
format,
|
|
path,
|
|
prependWith,
|
|
normalizeLocale = true,
|
|
routing = "pathname-prefix-other-locales",
|
|
defaultLocale
|
|
}) {
|
|
const codeToUse = peekCodePathToUse(_locales, locale);
|
|
if (!codeToUse) {
|
|
throw new AstroError({
|
|
...MissingLocale,
|
|
message: MissingLocale.message(locale)
|
|
});
|
|
}
|
|
const pathsToJoin = [base, prependWith];
|
|
const normalizedLocale = normalizeLocale ? normalizeTheLocale(codeToUse) : codeToUse;
|
|
if (routing === "pathname-prefix-always" || routing === "pathname-prefix-always-no-redirect") {
|
|
pathsToJoin.push(normalizedLocale);
|
|
} else if (locale !== defaultLocale) {
|
|
pathsToJoin.push(normalizedLocale);
|
|
}
|
|
pathsToJoin.push(path);
|
|
if (shouldAppendForwardSlash(trailingSlash, format)) {
|
|
return appendForwardSlash(joinPaths(...pathsToJoin));
|
|
} else {
|
|
return joinPaths(...pathsToJoin);
|
|
}
|
|
}
|
|
function getLocaleAbsoluteUrl({ site, ...rest }) {
|
|
const locale = getLocaleRelativeUrl(rest);
|
|
if (site) {
|
|
return joinPaths(site, locale);
|
|
} else {
|
|
return locale;
|
|
}
|
|
}
|
|
function getLocaleRelativeUrlList({
|
|
base,
|
|
locales: _locales,
|
|
trailingSlash,
|
|
format,
|
|
path,
|
|
prependWith,
|
|
normalizeLocale = false,
|
|
routing = "pathname-prefix-other-locales",
|
|
defaultLocale
|
|
}) {
|
|
const locales = toPaths(_locales);
|
|
return locales.map((locale) => {
|
|
const pathsToJoin = [base, prependWith];
|
|
const normalizedLocale = normalizeLocale ? normalizeTheLocale(locale) : locale;
|
|
if (routing === "pathname-prefix-always" || routing === "pathname-prefix-always-no-redirect") {
|
|
pathsToJoin.push(normalizedLocale);
|
|
} else if (locale !== defaultLocale) {
|
|
pathsToJoin.push(normalizedLocale);
|
|
}
|
|
pathsToJoin.push(path);
|
|
if (shouldAppendForwardSlash(trailingSlash, format)) {
|
|
return appendForwardSlash(joinPaths(...pathsToJoin));
|
|
} else {
|
|
return joinPaths(...pathsToJoin);
|
|
}
|
|
});
|
|
}
|
|
function getLocaleAbsoluteUrlList({ site, ...rest }) {
|
|
const locales = getLocaleRelativeUrlList(rest);
|
|
return locales.map((locale) => {
|
|
if (site) {
|
|
return joinPaths(site, locale);
|
|
} else {
|
|
return locale;
|
|
}
|
|
});
|
|
}
|
|
function getPathByLocale(locale, locales) {
|
|
for (const loopLocale of locales) {
|
|
if (typeof loopLocale === "string") {
|
|
if (loopLocale === locale) {
|
|
return loopLocale;
|
|
}
|
|
} else {
|
|
for (const code of loopLocale.codes) {
|
|
if (code === locale) {
|
|
return loopLocale.path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
function getLocaleByPath(path, locales) {
|
|
for (const locale of locales) {
|
|
if (typeof locale !== "string") {
|
|
if (locale.path === path) {
|
|
const code = locale.codes.at(0);
|
|
return code;
|
|
}
|
|
} else if (locale === path) {
|
|
return locale;
|
|
}
|
|
}
|
|
return void 0;
|
|
}
|
|
function normalizeTheLocale(locale) {
|
|
return locale.replaceAll("_", "-").toLowerCase();
|
|
}
|
|
function toCodes(locales) {
|
|
const codes = [];
|
|
for (const locale of locales) {
|
|
if (typeof locale === "string") {
|
|
codes.push(locale);
|
|
} else {
|
|
for (const code of locale.codes) {
|
|
codes.push(code);
|
|
}
|
|
}
|
|
}
|
|
return codes;
|
|
}
|
|
function toPaths(locales) {
|
|
return locales.map((loopLocale) => {
|
|
if (typeof loopLocale === "string") {
|
|
return loopLocale;
|
|
} else {
|
|
return loopLocale.path;
|
|
}
|
|
});
|
|
}
|
|
function peekCodePathToUse(locales, locale) {
|
|
for (const loopLocale of locales) {
|
|
if (typeof loopLocale === "string") {
|
|
if (loopLocale === locale) {
|
|
return loopLocale;
|
|
}
|
|
} else {
|
|
for (const code of loopLocale.codes) {
|
|
if (code === locale) {
|
|
return loopLocale.path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return void 0;
|
|
}
|
|
export {
|
|
getLocaleAbsoluteUrl,
|
|
getLocaleAbsoluteUrlList,
|
|
getLocaleByPath,
|
|
getLocaleRelativeUrl,
|
|
getLocaleRelativeUrlList,
|
|
getPathByLocale,
|
|
normalizeTheLocale,
|
|
toCodes,
|
|
toPaths
|
|
};
|