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 247eafd
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 99 deletions.
122 changes: 62 additions & 60 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1474,66 +1474,68 @@ export interface AstroUserConfig {
*
* Controls the routing strategy to determine your site URLs. Set this based on your folder/URL path configuration for your default language.
*/
routing?: {
/**
* @docs
* @name i18n.routing.prefixDefaultLocale
* @kind h4
* @type {boolean}
* @default `false`
* @version 3.7.0
* @description
*
* When `false`, only non-default languages will display a language prefix.
* The `defaultLocale` will not show a language prefix and content files do not exist in a localized folder.
* URLs will be of the form `example.com/[locale]/content/` for all non-default languages, but `example.com/content/` for the default locale.
*
* When `true`, all URLs will display a language prefix.
* URLs will be of the form `example.com/[locale]/content/` for every route, including the default language.
* Localized folders are used for every language, including the default.
*/
prefixDefaultLocale?: boolean;

/**
* @docs
* @name i18n.routing.redirectToDefaultLocale
* @kind h4
* @type {boolean}
* @default `true`
* @version 4.2.0
* @description
*
* Configures whether or not the home URL (`/`) generated by `src/pages/index.astro`
* will redirect to `/[defaultLocale]` when `prefixDefaultLocale: true` is set.
*
* Set `redirectToDefaultLocale: false` to disable this automatic redirection at the root of your site:
* ```js
* // astro.config.mjs
* export default defineConfig({
* i18n:{
* defaultLocale: "en",
* locales: ["en", "fr"],
* routing: {
* prefixDefaultLocale: true,
* redirectToDefaultLocale: false
* }
* }
* })
*```
* */
redirectToDefaultLocale?: boolean;

/**
* @name i18n.routing.strategy
* @type {"pathname"}
* @default `"pathname"`
* @version 3.7.0
* @description
*
* - `"pathname": The strategy is applied to the pathname of the URLs
*/
strategy?: 'pathname';
};
routing?:
| 'manual'
| {
/**
* @docs
* @name i18n.routing.prefixDefaultLocale
* @kind h4
* @type {boolean}
* @default `false`
* @version 3.7.0
* @description
*
* When `false`, only non-default languages will display a language prefix.
* The `defaultLocale` will not show a language prefix and content files do not exist in a localized folder.
* URLs will be of the form `example.com/[locale]/content/` for all non-default languages, but `example.com/content/` for the default locale.
*
* When `true`, all URLs will display a language prefix.
* URLs will be of the form `example.com/[locale]/content/` for every route, including the default language.
* Localized folders are used for every language, including the default.
*/
prefixDefaultLocale?: boolean;

/**
* @docs
* @name i18n.routing.redirectToDefaultLocale
* @kind h4
* @type {boolean}
* @default `true`
* @version 4.2.0
* @description
*
* Configures whether or not the home URL (`/`) generated by `src/pages/index.astro`
* will redirect to `/[defaultLocale]` when `prefixDefaultLocale: true` is set.
*
* Set `redirectToDefaultLocale: false` to disable this automatic redirection at the root of your site:
* ```js
* // astro.config.mjs
* export default defineConfig({
* i18n:{
* defaultLocale: "en",
* locales: ["en", "fr"],
* routing: {
* prefixDefaultLocale: true,
* redirectToDefaultLocale: false
* }
* }
* })
*```
* */
redirectToDefaultLocale?: boolean;

/**
* @name i18n.routing.strategy
* @type {"pathname"}
* @default `"pathname"`
* @version 3.7.0
* @description
*
* - `"pathname": The strategy is applied to the pathname of the URLs
*/
strategy?: 'pathname';
};

/**
* @name i18n.domains
Expand Down
10 changes: 7 additions & 3 deletions packages/astro/src/core/base-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ export abstract class Pipeline {
*/
readonly site = manifest.site
) {
this.internalMiddleware = [
createI18nMiddleware(i18n, manifest.base, manifest.trailingSlash, manifest.buildFormat),
];
this.internalMiddleware = [];
// we do use our middleware only if the user isn't using the manual setup
if (i18n?.strategy !== 'manual') {
this.internalMiddleware.push(
createI18nMiddleware(i18n, manifest.base, manifest.trailingSlash, manifest.buildFormat)
);
}
}

abstract headElements(routeData: RouteData): Promise<HeadElements> | HeadElements;
Expand Down
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 247eafd

Please sign in to comment.