forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): manual routing (withastro#10193)
* feat(i18n): manual routing * one more function * different typing * tests * fix merge * throw error for missing middleware * rename function * fix conflicts * lock file update * fix options, error thrown and added tests * rebase * add tests * docs * lock file black magic * increase timeout? * fix regression * merge conflict * add changeset * chore: apply suggestions * apply suggestion * Update .changeset/little-hornets-give.md Co-authored-by: Erika <[email protected]> * chore: address feedback * fix regression of last commit * update name * add comments * fix regression * remove unused code * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> * chore: update reference * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <[email protected]> * chore: improve types * fix regression in tests * apply Sarah's suggestion --------- Co-authored-by: Erika <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
1892772
commit 5d086c3
Showing
45 changed files
with
1,176 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
"astro": minor | ||
--- | ||
|
||
Adds a new i18n routing option `manual` to allow you to write your own i18n middleware: | ||
|
||
```js | ||
import { defineConfig } from "astro/config" | ||
// astro.config.mjs | ||
export default defineConfig({ | ||
i18n: { | ||
locales: ["en", "fr"], | ||
defaultLocale: "fr", | ||
routing: "manual" | ||
} | ||
}) | ||
``` | ||
|
||
Adding `routing: "manual"` to your i18n config disables Astro's own i18n middleware and provides you with helper functions to write your own: `redirectToDefaultLocale`, `notFound`, and `redirectToFallback`: | ||
|
||
```js | ||
// middleware.js | ||
import { redirectToDefaultLocale } from "astro:i18n"; | ||
export const onRequest = defineMiddleware(async (context, next) => { | ||
if (context.url.startsWith("/about")) { | ||
return next() | ||
} else { | ||
return redirectToDefaultLocale(context, 302); | ||
} | ||
}) | ||
``` | ||
|
||
Also adds a `middleware` function that manually creates Astro's i18n middleware. This allows you to extend Astro's i18n routing instead of completely replacing it. Run `middleware` in combination with your own middleware, using the `sequence` utility to determine the order: | ||
|
||
```js title="src/middleware.js" | ||
import {defineMiddleware, sequence} from "astro:middleware"; | ||
import { middleware } from "astro:i18n"; // Astro's own i18n routing config | ||
|
||
export const userMiddleware = defineMiddleware(); | ||
|
||
export const onRequest = sequence( | ||
userMiddleware, | ||
middleware({ | ||
redirectToDefaultLocale: false, | ||
prefixDefaultLocale: true | ||
}) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.