Skip to content

fix(i18n): incorrect validation #13552

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

Merged
merged 7 commits into from
Apr 4, 2025
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
5 changes: 5 additions & 0 deletions .changeset/old-animals-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where Astro validated the i18n configuration incorrectly, causing false positives in downstream libraries.
1 change: 1 addition & 0 deletions packages/astro/src/core/config/schemas/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export const AstroConfigSchema = z.object({
.or(
z.object({
prefixDefaultLocale: z.boolean().optional().default(false),
// TODO: Astro 6.0 change to false
redirectToDefaultLocale: z.boolean().optional().default(true),
fallbackType: z.enum(['redirect', 'rewrite']).optional().default('redirect'),
}),
Expand Down
28 changes: 15 additions & 13 deletions packages/astro/src/core/config/schemas/refined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ export const AstroConfigRefinedSchema = z.custom<AstroConfig>().superRefine((con
}
}

if (
config.i18n &&
typeof config.i18n.routing !== 'string' &&
!config.i18n.routing.redirectToDefaultLocale &&
!config.i18n.routing.prefixDefaultLocale
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'The option `i18n.routing.redirectToDefaultLocale` is only useful when the `i18n.routing.prefixDefaultLocale` is set to `true`. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `true`.',
path: ['i18n', 'routing', 'redirectToDefaultLocale'],
});
}
// TODO: Astro 6.0
// Uncomment this validation check, and change the default value of redirectToDefaultLocale to false
// if (
// config.i18n &&
// typeof config.i18n.routing !== 'string' &&
// config.i18n.routing.prefixDefaultLocale === false &&
// config.i18n.routing.redirectToDefaultLocale === true
// ) {
// ctx.addIssue({
// code: z.ZodIssueCode.custom,
// message:
// 'The option `i18n.routing.redirectToDefaultLocale` can be used only when `i18n.routing.prefixDefaultLocale` is set to `true`, otherwise redirects might cause infinite loops. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `false`.',
// path: ['i18n', 'routing', 'redirectToDefaultLocale'],
// });
// }

if (config.outDir.toString().startsWith(config.publicDir.toString())) {
ctx.addIssue({
Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/integrations/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ export async function runHookConfigSetup({
if (astroJSXRenderer) {
updatedSettings.renderers.push(astroJSXRenderer);
}

// TODO: Astro 6.0
// Remove this hack to avoid breaking changes, and change the default value of redirectToDefaultLocale
if (updatedConfig.i18n && typeof updatedConfig.i18n.routing !== "string") {
updatedConfig.i18n.routing.redirectToDefaultLocale ??= updatedConfig.i18n.routing.prefixDefaultLocale || false;
}

updatedSettings.config = updatedConfig;
return updatedSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineConfig({
}
],
routing: {
prefixDefaultLocale: true
prefixDefaultLocale: true,
}
},
base: "/new-site"
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/test/units/config/config-validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,21 @@ describe('Config Validation', () => {
);
});

it('errors if `i18n.prefixDefaultLocale` is `false` and `i18n.redirectToDefaultLocale` is `true`', async () => {
it('errors if `i18n.prefixDefaultLocale` is `false` and `i18n.redirectToDefaultLocale` is `true`', { todo: "Enable in Astro 6.0", skip: "Removed validation"}, async () => {
const configError = await validateConfig({
i18n: {
defaultLocale: 'en',
locales: ['es', 'en'],
routing: {
prefixDefaultLocale: false,
redirectToDefaultLocale: false,
redirectToDefaultLocale: true,
},
},
}).catch((err) => err);
assert.equal(configError instanceof z.ZodError, true);
assert.equal(
configError.errors[0].message,
'The option `i18n.routing.redirectToDefaultLocale` is only useful when the `i18n.routing.prefixDefaultLocale` is set to `true`. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `true`.',
'The option `i18n.routing.redirectToDefaultLocale` can be used only when `i18n.routing.prefixDefaultLocale` is set to `true`, otherwise redirects might cause infinite loops. Remove the option `i18n.routing.redirectToDefaultLocale`, or change its value to `false`.'
);
});

Expand Down