Now that you have setup the config, each .astro page will have additional renders with your other languages. For example, src/pages/about.astro will render as:
/about.astro
/es/about.astro
/fr/about.astro
Please note that the getStaticPaths() function will only run once. This limitation means that you cannot have translated urls, such as /es/acerca-de for /about. However, it also ensures compatibility with @astrojs/sitemap.
The Astro frontmatter is re-run for every translated page. For example, the Astro.url.pathname can be /about, /es/about, /fr/about.
It is up to you to detect which language is being rendered. You can use Astro content collections or any i18n UI framework, such as react-i18next, for your translations. Here is a pure Hello World example:
---
import Layout from "../layouts/Layout.astro";
const locale = Astro.url.pathname.slice(1, 3);
let title: string;
switch (locale) {
case "es":
title = "Β‘Hola Mundo!";
break;
case "fr":
title = "Bonjour Monde!";
break;
default:
title = "Hello World!";
}
---
<Layout title={title}>
<h1>{title}</h1>
</Layout>
Options
include: glob pattern(s) to include (default: ["pages/**/*"])
exclude: glob pattern(s) to exclude (default: ["pages/api/**"])
Other Astro page file types:
β .astro
β .md
β .mdx (with the MDX Integration installed)
β .html
β .jsβ/β.ts (as endpoints)
cannot be translated. If you choose to use them, please add them to the ignore glob patterns. For example, ["pages/api/**", "pages/**/*.md"]
License
MIT Licensed
Contributing
PRs welcome! Thank you for your contributions.
The How
Unfortunately, i18n is not a first-class concern for Astro. While Astro documents i18n in their cookbook, they do no support a defaultLocale.
The other community integrations that Astro links do not support all adapters:
astro-i18next An Astro integration for i18next including some utility components.
astro-i18n A TypeScript-first internationalization library for Astro.
Astro does not easily support two pages having the same content:
Route variables /[lang]/about cannot be undefined or an empty string
Middleware request.url is read-only, so it is not possible to retrieve content from a different url
Configured redirects do not support route transitions like '/article': '/blog/[...slug]', only '/blog/[...slug]': '/articles/[...slug]'
The injectRoute method cannot inject an entryPoint that is already being used in the build command
We duplicate the src/pages folder and use injectRoute as a workaround. You can safely delete any src/astro_tmp_pages_LOCALE folders, but that will be automatically cleaned on every started and completed build.
The npm package astro-i18n-aut receives a total of 1,389 weekly downloads. As such, astro-i18n-aut popularity was classified as popular.
We found that astro-i18n-aut demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer collaborating on the project.
Package last updated on 08 Jun 2023
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."