Skip to content

Commit

Permalink
different typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Feb 22, 2024
1 parent 659a6c0 commit 8e44d7c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
33 changes: 18 additions & 15 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 19 additions & 14 deletions packages/astro/src/i18n/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export function computeCurrentLocale(
}

export type RoutingStrategies =
| 'manual'
| 'pathname-prefix-always'
| 'pathname-prefix-other-locales'
| 'pathname-prefix-always-no-redirect'
Expand All @@ -201,25 +202,29 @@ export function toRoutingStrategy(i18n: NonNullable<AstroConfig['i18n']>) {
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';
}
}

Expand Down
10 changes: 4 additions & 6 deletions packages/astro/src/virtual-modules/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -268,7 +268,7 @@ export const redirectToDefaultLocale =
*
*/
export const noFoundForNonLocaleRoute =
i18n?.routing?.strategy === 'manual'
i18n?.routing === 'manual'
? I18nInternals.noFoundForNonLocaleRoute({
base,
trailingSlash,
Expand All @@ -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,
Expand Down

0 comments on commit 8e44d7c

Please sign in to comment.