Skip to content

Commit

Permalink
fix(i18n): consider routingStrategy when computing the URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Nov 6, 2023
1 parent 9e2801d commit d18b3c5
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 100 deletions.
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

0 comments on commit d18b3c5

Please sign in to comment.