Add i18n features
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.
RecipeSection titled Recipe
Set up pages for each languageSection titled Set up pages for each language
Create a directory for each language you want to support. For example,
fr/if you are supporting English and French:
src/pages/index.astroto 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.redirectto redirect to the default language on the server.
Use collections for translated contentSection titled Use collections for translated content
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:
ディレクトリen/ Blog posts in English
ディレクトリfr/ Blog posts in French
src/content/config.tsfile 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
In static rendering mode, use
getStaticPathsto map each content entry to a page:
In SSR mode, fetch the requested entry directly:
📚 Read more about dynamic routing.
Translate UI stringsSection titled Translate UI strings
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.
src/i18n/ui.tsfile 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
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
langattribute 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
Let users switch between languagesSection titled Let users switch between languages
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:
<LanguagePicker />to your site so it is shown on every page. The example below adds it to the site footer in a base layout:
ResourcesSection titled Resources
Community librariesSection titled Community libraries
- astro-i18next — An Astro integration for i18next including some utility components.
- astro-i18n — A TypeScript-first internationalization library for Astro.
Share State Between Islands
Learn how to share state across components — and frameworks! — with Nano Stores.
Installing a Vite or Rollup plugin
Learn how you can import YAML data by adding a Rollup plugin to your project.
Build Forms With API Routes
Build HTML Forms in Astro Pages
Learn how to build HTML forms and handle submissions in your frontmatter
Use Bun with Astro
Learn how to use Bun with your Astro site.
Verify a Captcha
Learn how to create an API route and fetch it from the client.
Build your Astro Site with Docker
Learn how to build your Astro site using Docker.
Add icons to external links
Learn how to install a rehype plugin to add icons to external links in your Markdown files
Add i18n features
Use dynamic routing and content collections to add internationalization support to your Astro site.
Add Reading Time
Build a remark plugin to add reading time to your Markdown or MDX files.