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.
If you prefer the default language to not be visible in the URL unlike other languages, there are instructions to hide the default language below.
Recipe
Section titled RecipeSet up pages for each language
Section titled Set up pages for each language-
Create a directory for each language you want to support. For example,
en/
andfr/
if you are supporting English and French:Directorysrc/
Directorypages/
Directoryen/
- about.astro
- index.astro
Directoryfr/
- about.astro
- index.astro
- index.astro
-
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.
Use collections for translated content
Section 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:Directorysrc/
Directorycontent/
Directoryblog/
Directoryen/ Blog posts in English
- post-1.md
- post-2.md
Directoryfr/ Blog posts in French
- post-1.md
- post-2.md
-
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 aslug
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.
Translate UI strings
Section titled Translate UI stringsCreate 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.
Let users switch between languages
Section titled Let users switch between languagesCreate 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:
Hide default language in the URL
Section titled Hide default language in the URL-
Create a directory for each language except the default language. For example, store your default language pages directly in
pages/
, and your translated pages infr/
:Directorysrc/
Directorypages/
- about.astro
- index.astro
Directoryfr/
- about.astro
- index.astro
-
Add another line to the
src/i18n/ui.ts
file to toggle the feature: -
Add a helper function to
src/i18n/utils.ts
, to translate paths based on the current language: -
Import the helper where needed. For example, a
nav
component might look like: -
The helper function can also be used to translate paths for a specific language. For example, when users switch between languages:
Translate Routes
Section titled Translate RoutesTranslate the routes of your pages for each language.
-
Add route mappings to
src/i18n/ui.ts
: -
Update the
useTranslatedPath
helper function insrc/i18n/utils.ts
to add router translation logic. -
Create a helper function to get the route, if it exists based on the current URL, in
src/i18n/utils.ts
: -
The helper function can be used to get a translated route. For example, when no translated route is defined, the user will be redirected to the home page:
Resources
Section titled ResourcesCommunity libraries
Section titled Community libraries- astro-i18next — An Astro integration for i18next including some utility components.
- astro-i18n — A TypeScript-first internationalization library for Astro.
- astro-i18n-aut — An Astro integration for i18n that supports the
defaultLocale
without page generation. The integration is adapter agnostic and UI framework agnostic.
More recipes
-
Share State Between Islands
Learn how to share state across framework components with Nano Stores.
-
Add an RSS feed
Add an RSS feed to your Astro site to let users subscribe to your content.
-
Installing a Vite or Rollup plugin
Learn how you can import YAML data by adding a Rollup plugin to your project.
-
Build a custom image component
Learn how to build a custom image component that supports media queries using the getImage function
-
Build Forms With API Routes
Learn how to use JavaScript to send form submissions to an API Route
-
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.
-
Call endpoints from the server
Learn how to call endpoints from the server in Astro.
-
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 Last Modified Time
Build a remark plugin to add the last modified time to your Markdown and MDX.
-
Add Reading Time
Build a remark plugin to add reading time to your Markdown or MDX files.
-
Share State Between Astro Components
Learn how to share state across Astro components with Nano Stores.
-
Using streaming to improve page performance
Learn how to use streaming to improve page performance.
-
Style Rendered Markdown with Tailwind Typography
Learn how to use @tailwind/typography to style your rendered Markdown