diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 4945afd3ed1af..d1f8665571fc7 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -338,21 +338,24 @@ export const AstroConfigSchema = z.object({ .optional(), fallback: z.record(z.string(), z.string()).optional(), routing: z - .object({ - prefixDefaultLocale: z.boolean().default(false), - redirectToDefaultLocale: z.boolean().default(true), - strategy: z.enum(['automatic', 'manual']).default('automatic'), - }) - .default({}) - .refine( - ({ prefixDefaultLocale, redirectToDefaultLocale }) => { - return !(prefixDefaultLocale === false && redirectToDefaultLocale === false); - }, - { - message: - 'The option `i18n.redirectToDefaultLocale` is only useful when the `i18n.prefixDefaultLocale` is set to `true`. Remove the option `i18n.redirectToDefaultLocale`, or change its value to `true`.', - } - ), + .enum(['manual']) + .or( + z + .object({ + prefixDefaultLocale: z.boolean().default(false), + redirectToDefaultLocale: z.boolean().default(true), + }) + .refine( + ({ prefixDefaultLocale, redirectToDefaultLocale }) => { + return !(prefixDefaultLocale === false && redirectToDefaultLocale === false); + }, + { + message: + 'The option `i18n.redirectToDefaultLocale` is only useful when the `i18n.prefixDefaultLocale` is set to `true`. Remove the option `i18n.redirectToDefaultLocale`, or change its value to `true`.', + } + ) + ) + .default({}), }) .optional() .superRefine((i18n, ctx) => { diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index 83d9751defb36..bfa4efc47195f 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -1033,7 +1033,7 @@ export const MissingIndexForInternationalization = { /** * @docs * @description - * + * Some internationalization functions can't be exposed unless the default routing is disabled */ export const IncorrectStrategy = { name: 'IncorrectStrategy', diff --git a/packages/astro/src/i18n/utils.ts b/packages/astro/src/i18n/utils.ts index d3dddba1f8c5c..b45c37ee7186d 100644 --- a/packages/astro/src/i18n/utils.ts +++ b/packages/astro/src/i18n/utils.ts @@ -191,6 +191,7 @@ export function computeCurrentLocale( } export type RoutingStrategies = + | 'manual' | 'pathname-prefix-always' | 'pathname-prefix-other-locales' | 'pathname-prefix-always-no-redirect' @@ -201,25 +202,29 @@ export function toRoutingStrategy(i18n: NonNullable) { let { routing, domains } = i18n; let strategy: RoutingStrategies; const hasDomains = domains ? Object.keys(domains).length > 0 : false; - if (!hasDomains) { - if (routing?.prefixDefaultLocale === true) { - if (routing.redirectToDefaultLocale) { - strategy = 'pathname-prefix-always'; + if (routing === 'manual') { + strategy = 'manual'; + } else { + if (!hasDomains) { + if (routing?.prefixDefaultLocale === true) { + if (routing.redirectToDefaultLocale) { + strategy = 'pathname-prefix-always'; + } else { + strategy = 'pathname-prefix-always-no-redirect'; + } } else { - strategy = 'pathname-prefix-always-no-redirect'; + strategy = 'pathname-prefix-other-locales'; } } else { - strategy = 'pathname-prefix-other-locales'; - } - } else { - if (routing?.prefixDefaultLocale === true) { - if (routing.redirectToDefaultLocale) { - strategy = 'domains-prefix-always'; + if (routing?.prefixDefaultLocale === true) { + if (routing.redirectToDefaultLocale) { + strategy = 'domains-prefix-always'; + } else { + strategy = 'domains-prefix-always-no-redirect'; + } } else { - strategy = 'domains-prefix-always-no-redirect'; + strategy = 'domains-prefix-other-locales'; } - } else { - strategy = 'domains-prefix-other-locales'; } } diff --git a/packages/astro/src/virtual-modules/i18n.ts b/packages/astro/src/virtual-modules/i18n.ts index 788c0413548c0..2e6d92189924d 100644 --- a/packages/astro/src/virtual-modules/i18n.ts +++ b/packages/astro/src/virtual-modules/i18n.ts @@ -242,7 +242,7 @@ export const pathHasLocale = (path: string) => I18nInternals.pathHasLocale(path, * @param {ValidRedirectStatus?} statusCode An optional status code for the redirect. */ export const redirectToDefaultLocale = - i18n?.routing?.strategy === 'manual' + i18n?.routing === 'manual' ? I18nInternals.redirectToDefaultLocale({ base, trailingSlash, @@ -268,7 +268,7 @@ export const redirectToDefaultLocale = * */ export const noFoundForNonLocaleRoute = - i18n?.routing?.strategy === 'manual' + i18n?.routing === 'manual' ? I18nInternals.noFoundForNonLocaleRoute({ base, trailingSlash, @@ -289,12 +289,10 @@ export const noFoundForNonLocaleRoute = * */ export const requestHasLocale = - i18n?.routing?.strategy === 'manual' - ? I18nInternals.requestHasLocale(locales) - : noop('requestHasLocale'); + i18n?.routing === 'manual' ? I18nInternals.requestHasLocale(locales) : noop('requestHasLocale'); export const useFallback: UseFallback = - i18n?.routing?.strategy === 'manual' + i18n?.routing === 'manual' ? I18nInternals.useFallback({ base, trailingSlash,