Internationalization API Reference
本頁內容尚未翻譯。
Added in:
astro@3.5.0
This module provides functions to help you create URLs using your project’s configured locales.
Creating routes for your project with the i18n router will depend on certain configuration values you have set that affect your page routes. When creating routes with these functions, be sure to take into account your individual settings for:
Also, note that the returned URLs created by these functions for your defaultLocale
will reflect your i18n.routing
configuration.
For features and usage examples, see our i18n routing guide.
Imports from astro:i18n
Section titled Imports from astro:i18ngetRelativeLocaleUrl()
Section titled getRelativeLocaleUrl()Type: (locale: string, path?: string, options?: GetLocaleOptions) => string
Use this function to retrieve a relative path for a locale. If the locale doesn’t exist, Astro throws an error.
getAbsoluteLocaleUrl()
Section titled getAbsoluteLocaleUrl()Type: (locale: string, path: string, options?: GetLocaleOptions) => string
Use this function to retrieve an absolute path for a locale when [site
] has a value. If [site
] isn’t configured, the function returns a relative URL. If the locale doesn’t exist, Astro throws an error.
getRelativeLocaleUrlList()
Section titled getRelativeLocaleUrlList()Type: (path?: string, options?: GetLocaleOptions) => string[]
Use this like getRelativeLocaleUrl
to return a list of relative paths for all the locales.
getAbsoluteLocaleUrlList()
Section titled getAbsoluteLocaleUrlList()Type: (path?: string, options?: GetLocaleOptions) => string[]
Use this like getAbsoluteLocaleUrl
to return a list of absolute paths for all the locales.
getPathByLocale()
Section titled getPathByLocale()Type: (locale: string) => string
A function that returns the path
associated to one or more codes
when custom locale paths are configured.
getLocaleByPath()
Section titled getLocaleByPath()Type: (path: string) => string
A function that returns the code
associated to a locale path
.
redirectToDefaultLocale()
Section titled redirectToDefaultLocale()Type: (context: APIContext, statusCode?: ValidRedirectStatus) => Promise<Response>
astro@4.6.0
Available only when i18n.routing
is set to "manual"
A function that returns a Response
that redirects to the defaultLocale
configured. It accepts an optional valid redirect status code.
redirectToFallback()
Section titled redirectToFallback()Type: (context: APIContext, response: Response) => Promise<Response>
astro@4.6.0
Available only when i18n.routing
is set to "manual"
A function that allows you to use your i18n.fallback
configuration in your own middleware.
notFound()
Section titled notFound()Type: (context: APIContext, response?: Response) => Promise<Response> | undefined
astro@4.6.0
Available only when i18n.routing
is set to "manual"
Use this function in your routing middleware to return a 404 when:
- the current path isn’t a root. e.g.
/
or/<base>
- the URL doesn’t contain a locale
When a Response
is passed, the new Response
emitted by this function will contain the same headers of the original response.
middleware()
Section titled middleware()Type: (options: { prefixDefaultLocale: boolean, redirectToDefaultLocale: boolean }) => MiddlewareHandler
astro@4.6.0
Available only when i18n.routing
is set to "manual"
A function that allows you to programmatically create the Astro i18n middleware.
This is useful when you still want to use the default i18n logic, but add only a few exceptions to your website.
requestHasLocale()
Section titled requestHasLocale()Type: (context: APIContext) => boolean
astro@4.6.0
Available only when i18n.routing
is set to "manual"
Checks whether the current URL contains a configured locale. Internally, this function will use APIContext#url.pathname
.