npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

astro-react-i18next

v0.1.3

Published

Integrates i18next and react-i18next seamlessly into your Astro website to provide robust i18n support for React components.

Downloads

377

Readme

astro-react-i18next

Integrates i18next and react-i18next seamlessly into your Astro website to provide robust i18n support for React components.

npm version GitHub License

Examples

| Example | | | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | SSG | Open in StackBlitz | | SSR | Open in StackBlitz |

Installation

npx astro add astro-react-i18next

Follow the prompts to install the necessary dependencies and add the required configuration to your Astro project.

You will see the following changes in your astro.config.mjs file:

import { defineConfig } from "astro/config";
import reactI18next from "astro-react-i18next";

export default defineConfig({
  // ...
  integrations: [
    // ...
+   reactI18next(),
  ],
});

Configuration

The initialization function accepts an optional configuration object with the following options:

| Option | Type | Description | Default | | --------------------- | ---------- | ---------------------------------------------------------------------------------- | ------------ | | defaultLocale | string | The default locale to use when no locale is specified. | "en-US" | | locales | string[] | An array of locales to support. | ["en-US"] | | defaultNamespace | string | The default namespace to use when no namespace is specified. | "common" | | namespaces | string[] | An array of namespaces to support. | ["common"] | | prefixDefaultLocale | boolean | Whether to prefix the default locale with the locale code. | false | | localesDir | string | The directory where the locale files are stored, relative to the public directory. | "locales" |

Here is an example of how to configure the integration:

import { defineConfig } from "astro/config";
import reactI18next from "astro-react-i18next";

export default defineConfig({
  // ...
  integrations: [
    // ...
-   reactI18next(),
+   reactI18next({
+     defaultLocale: "en-US",
+     locales: ["en-US", "fr-FR", "zh-TW"],
+     defaultNamespace: "app",
+     namespaces: ["app"],
+   }),
  ],
});

It also supports Server (SSR) Mode, such as using the @astrojs/node adapter:

import { defineConfig } from "astro/config";
+import node from '@astrojs/node';
import reactI18next from "astro-react-i18next";

export default defineConfig({
  // ...
  integrations: [
    // ...
    reactI18next({
      defaultLocale: "en-US",
      locales: ["en-US", "fr-FR", "zh-TW"],
      defaultNamespace: "app",
      namespaces: ["app"],
    }),
  ],
+ output: "server",
+ adapter: node({
+   mode: "standalone",
+ }),
});

Locale Resources

Create locale files for each locale and namespace in the localesDir directory.

For example, create the following files:

/
├── public/
│   └── locales/
│       ├── en-US/
│       │   └── common.json
│       ├── fr-FR/
│       │   └── common.json
│       └── zh-TW/
│           └── common.json
├── src/
└── package.json

The content of the locales/en-US/common.json file should look like this:

{
  "hello_world": "Hello, World!"
}

Dynamic Routes for Locales

To manage dynamic routes for each locale, create a root route named [...locale] in the pages directory.

/
├── public/
├── src/
│   └── pages/
│       └── [...locale]/
│           ├── index.astro
│           ├── page-a.astro
│           └── page-b.astro
└── package.json

Static Paths for Locales

If you're using Static (SSG) Mode, static paths are required. You can easily generate them by using the buildStaticPaths utility function provided by this integration.

---
import { buildStaticPaths } from "astro-react-i18next/utils";

export function getStaticPaths() {
  return buildStaticPaths();
}
---

<html>
  ...
</html>

Translating Content in Astro Components

Use the i18next instance to translate content in your Astro components.

---
import i18n from "i18next";
---

<html lang={i18n.language}>
  <p>{i18n.t("hello_world")}</p>
</html>

Translating Content in React Components

Use the useTranslation hook to translate content in your React components.

import { useTranslation } from "react-i18next";

export function MyComponent() {
  const { t } = useTranslation();
  return <p>{t("hello_world")}</p>;
}

Utilities

The integration provides utility functions to help manage locales and translations.

All utility functions are available in the astro-react-i18next/utils module.

| Function | Description | Returns | | -------------------------------------------------- | -------------------------------------------------------- | ---------------------------------------------------------------------------- | | getLocaleConfig() | Returns the locale configuration object. | { defaultLocale: string, locales: string[], prefixDefaultLocale: boolean } | | getLocalizedPathname(pathname = "", locale = "") | Returns the localized pathname for the specified locale. | string | | buildStaticPaths() | Generates static paths for each locale. | { params: { locale: string \| undefined; }; }[] | | changeLocale(nextLocale = "", shallow = true) | Changes the current locale. | |

License

Licensed under the MIT License.