Now that you have set up the config, each .astro page will have additional renders with your other languages. For example, src/pages/about.astro will render as:
/about/
/es/about/
/fr/about/
If you have enabled redirectDefaultLocale (true by default) in the integration and middleware, redirects will be:
/en/about/ => /about/
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 and page content is re-run for every translated page. For example, the Astro.url.pathname will 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 { getLocale } from "astro-i18n-aut";
import Layout from "../layouts/Layout.astro";
const locale = getLocale(Astro.url);
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>
Several helper functions are included to make handling locales easier.
Astro config options
Please see the official Astro docs for more details:
All these options are related and must be set together. They affect whether your urls are:
/about/
/about
If you choose /about/, then /about will 404 and vice versa.
Integration options
locales: A record of all language locales.
defaultLocale: The default language locale. The value must present in locales keys.
redirectDefaultLocale - Assuming the defaultLocale: "en", whether /en/about/ redirects to /about/ (default: 308).
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 in the pages directory, please add them to the ignore glob patterns. For example, ["pages/api/**/*", "pages/**/*.md"]
With this library and Astro Content Collections, you can keep your Markdown separate and organized in content, while using pages/blog/index.astro and pages/blog/[slug].astro to render all of your content, even with a defaultLocale! Here is an example folder structure:
Astro does not support .tsx or .jsx as page file types.
For UI frameworks like React and Vue, use them how you normally would with Astro by importing them as components.
Feel free to pass the translated content title={t('title')} or locale locale={locale} as props.
Endpoints
By default, all pages in pages/api/**/* are ignored.
For .ts and .js endpoints, how you handle multiple locales is up to you. As endpoints are not user-facing and there are many different ways to use endpoints, we leave the implementation up to your preferences.
License
MIT Licensed
Contributing
PRs welcome! Thank you for your help. Read more in the contributing guide for reporting bugs and making PRs.
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 12 Sep 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."