Astro doesn’t have built-in internationalization (i18n) support, but you can build your own i18n solution. In this recipe, you’ll learn how to use content collections and dynamic routing to serve content for different languages.
This example serves each language at its own subpath, e.g. example.com/en/blog
for English and example.com/fr/blog
for French.
Create a directory for each language you want to support. For example, en/
and fr/
if you are supporting English and French:
Set up src/pages/index.astro
to redirect to your default language.
This approach uses a meta refresh and will work however you deploy your site. Some static hosts also let you configure server redirects with a custom configuration file. See your deploy platform’s documentation for more details.
If you are using an SSR adapter, you can use Astro.redirect
to redirect to the default language on the server.
Create a folder in src/content/
for each type of content you want to include and add subdirectories for each supported language. For example, to support English and French blog posts:
Create a src/content/config.ts
file and export a collection for each type of content.
📚 Read more about Content Collections.
Use dynamic routes to fetch and render content based on a lang
and a slug
parameter.
In static rendering mode, use getStaticPaths
to map each content entry to a page:
In SSR mode, fetch the requested entry directly:
📚 Read more about dynamic routing.
Create dictionaries of terms to translate the labels for UI elements around your site. This allows your visitors to experience your site fully in their language.
Create a src/i18n/ui.ts
file to store your translation strings:
Create two helper functions: one to detect the page language based on the current URL, and one to get translations strings for different parts of the UI in src/i18n/utils.ts
:
Import the helpers where needed and use them to choose the UI string that corresponds to the current language. For example, a nav component might look like:
Each page must have a lang
attribute on the <html>
element that matches the language on the page. In this example, a reusable layout extracts the language from the current route:
You can then use this base layout to ensure that pages use the correct lang
attribute automatically.
Create links to the different languages you support so users can choose the language they want to read your site in.
Create a component to show a link for each language:
Add <LanguagePicker />
to your site so it is shown on every page. The example below adds it to the site footer in a base layout:
defaultLocale
without page generation. The integration is adapter agnostic and UI framework agnostic.Learn how to share state across framework components with Nano Stores.
Add an RSS feed to your Astro site to let users subscribe to your content.
Learn how you can import YAML data by adding a Rollup plugin to your project.
Learn how to use JavaScript to send form submissions to an API Route
Learn how to build HTML forms and handle submissions in your frontmatter
Learn how to use Bun with your Astro site.
Learn how to call endpoints from the server in Astro.
Learn how to create an API route and fetch it from the client.
Learn how to build your Astro site using Docker.
Learn how to install a rehype plugin to add icons to external links in your Markdown files
Use dynamic routing and content collections to add internationalization support to your Astro site.
Build a remark plugin to add reading time to your Markdown or MDX files.
Learn how to share state across Astro components with Nano Stores.
© 2021 Fred K. Schott
Licensed under the MIT License.
https://docs.astro.build/en/recipes/i18n/