Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): consider routingStrategy when computing the URLs #9008

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type GetLocaleRelativeUrl = GetLocaleOptions & {
locales: string[];
trailingSlash: AstroConfig['trailingSlash'];
format: AstroConfig['build']['format'];
routingStrategy?: 'prefix-always' | 'prefix-other-locales';
defaultLocale: string;
};

export type GetLocaleOptions = {
Expand Down Expand Up @@ -43,19 +45,27 @@ export function getLocaleRelativeUrl({
path,
prependWith,
normalizeLocale = true,
routingStrategy = 'prefix-other-locales',
defaultLocale,
}: GetLocaleRelativeUrl) {
if (!locales.includes(locale)) {
throw new AstroError({
...MissingLocale,
message: MissingLocale.message(locale, locales),
});
}
const pathsToJoin = [base, prependWith];
if (routingStrategy === 'prefix-always') {
pathsToJoin.push(normalizeTheLocale(locale, normalizeLocale));
} else if (locale !== defaultLocale) {
pathsToJoin.push(normalizeTheLocale(locale, normalizeLocale));
}
pathsToJoin.push(path);

const normalizedLocale = normalizeTheLocale(locale, normalizeLocale);
if (shouldAppendForwardSlash(trailingSlash, format)) {
return appendForwardSlash(joinPaths(base, prependWith, normalizedLocale, path));
return appendForwardSlash(joinPaths(...pathsToJoin));
} else {
return joinPaths(base, prependWith, normalizedLocale, path);
return joinPaths(...pathsToJoin);
}
}

Expand All @@ -76,6 +86,8 @@ type GetLocalesBaseUrl = GetLocaleOptions & {
locales: string[];
trailingSlash: AstroConfig['trailingSlash'];
format: AstroConfig['build']['format'];
routingStrategy?: 'prefix-always' | 'prefix-other-locales';
defaultLocale: string;
};

export function getLocaleRelativeUrlList({
Expand All @@ -86,13 +98,21 @@ export function getLocaleRelativeUrlList({
path,
prependWith,
normalizeLocale = false,
routingStrategy = 'prefix-other-locales',
defaultLocale,
}: GetLocalesBaseUrl) {
return locales.map((locale) => {
const normalizedLocale = normalizeTheLocale(locale, normalizeLocale);
const pathsToJoin = [base, prependWith];
if (routingStrategy === 'prefix-always') {
pathsToJoin.push(normalizeTheLocale(locale, normalizeLocale));
} else if (locale !== defaultLocale) {
pathsToJoin.push(normalizeTheLocale(locale, normalizeLocale));
}
pathsToJoin.push(path);
if (shouldAppendForwardSlash(trailingSlash, format)) {
return appendForwardSlash(joinPaths(base, prependWith, normalizedLocale, path));
return appendForwardSlash(joinPaths(...pathsToJoin));
} else {
return joinPaths(base, prependWith, normalizedLocale, path);
return joinPaths(...pathsToJoin);
}
});
}
Expand Down
30 changes: 23 additions & 7 deletions packages/astro/src/i18n/vite-plugin-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,35 @@ export default function astroInternalization({ settings }: AstroInternalization)

} from "astro/i18n";

const defaultLocale = ${JSON.stringify(settings.config.experimental.i18n!.defaultLocale)};
const locales = ${JSON.stringify(settings.config.experimental.i18n!.locales)};
const fallback = ${JSON.stringify(settings.config.experimental.i18n!.fallback)};
const base = ${JSON.stringify(settings.config.base)};
const trailingSlash = ${JSON.stringify(settings.config.trailingSlash)};
const format = ${JSON.stringify(settings.config.build.format)};
const site = ${JSON.stringify(settings.config.site)};
const i18n = ${JSON.stringify(settings.config.experimental.i18n)};

export const getLocaleRelativeUrl = (locale, path = "", opts) => _getLocaleRelativeUrl({ locale, path, base, locales, trailingSlash, format, ...opts });
export const getLocaleAbsoluteUrl = (locale, path = "", opts) => _getLocaleAbsoluteUrl({ locale, path, base, locales, trailingSlash, format, site, ...opts });
export const getLocaleRelativeUrl = (locale, path = "", opts) => _getLocaleRelativeUrl({
locale,
path,
base,
trailingSlash,
format,
...i18n,
...opts
});
export const getLocaleAbsoluteUrl = (locale, path = "", opts) => _getLocaleAbsoluteUrl({
locale,
path,
base,
trailingSlash,
format,
site,
...i18n,
...opts
});

export const getLocaleRelativeUrlList = (path = "", opts) => _getLocaleRelativeUrlList({ base, path, locales, trailingSlash, format, ...opts });
export const getLocaleAbsoluteUrlList = (path = "", opts) => _getLocaleAbsoluteUrlList({ base, path, locales, trailingSlash, format, site, ...opts });
export const getLocaleRelativeUrlList = (path = "", opts) => _getLocaleRelativeUrlList({
base, path, trailingSlash, format, ...i18n, ...opts });
export const getLocaleAbsoluteUrlList = (path = "", opts) => _getLocaleAbsoluteUrlList({ base, path, trailingSlash, format, site, ...i18n, ...opts });
`;
}
},
Expand Down
Loading