-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(i18n): manual routing #10193
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
feat(i18n): manual routing #10193
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
53c5764
feat(i18n): manual routing
ematipico 0a2f112
Merge remote-tracking branch 'origin/main' into feat/i18n-manual-routing
ematipico 659a6c0
one more function
ematipico 247eafd
different typing
ematipico 9e95b0f
tests
ematipico 5f0e19c
Merge remote-tracking branch 'origin/main' into feat/i18n-manual-routing
ematipico 2eb86db
fix merge
ematipico 1fd976f
Merge remote-tracking branch 'origin/main' into feat/i18n-manual-routing
ematipico b65f2d0
throw error for missing middleware
ematipico 5e7097c
rename function
ematipico 0da463a
Merge remote-tracking branch 'origin/main' into feat/i18n-manual-routing
ematipico 3d14524
fix conflicts
ematipico c5e02dc
lock file update
ematipico c6fcdc4
fix options, error thrown and added tests
ematipico 7849d78
Merge remote-tracking branch 'origin/main' into feat/i18n-manual-routing
ematipico 65c216b
rebase
ematipico 7bed201
add tests
ematipico 51bcf56
docs
ematipico fc24ba4
lock file black magic
ematipico e43e04a
increase timeout?
ematipico e87d968
fix regression
ematipico 62d4293
Merge remote-tracking branch 'origin/main' into feat/i18n-manual-routing
ematipico a377295
merge conflict
ematipico 4057715
add changeset
ematipico bda256c
chore: apply suggestions
ematipico d3a8d18
apply suggestion
ematipico 4d9f6a7
Update .changeset/little-hornets-give.md
ematipico ba18514
chore: address feedback
ematipico e43236a
fix regression of last commit
ematipico 742c627
update name
ematipico e75b46c
add comments
ematipico 6c4aa8d
fix regression
ematipico fe0c01a
remove unused code
ematipico dbcee6e
Apply suggestions from code review
ematipico d746753
chore: update reference
ematipico 1516cbd
Update packages/astro/src/@types/astro.ts
ematipico bbb3787
chore: improve types
ematipico 52fcbe0
fix regression in tests
ematipico e781cb2
apply Sarah's suggestion
ematipico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great example |
||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.