next-i18n-auth

Strictly Typed Translation Functions (useTranslation and getTranslation)

Overview

In the Next-i18n-auth system, we’ve replaced the useTranslation hook from react-i18next with a custom version that provides strict typing. This custom useTranslation (and getTranslation for server-side code) offers autocompletion for namespaces and translation keys directly in your IDE, improving the development experience and ensuring that you are using valid keys and namespaces.

How It Works

⚙️ Tip: Types are automatically generated from your translation templates during the smart-i18n generate-types step. See the Automation Docs for details.


Example usage in React:

import { useTranslation } from "@/i18n";

const MyComponent = () => {
  const { t } = useTranslation("app.(ui).page");
  // The namespace passed here must match one from your generated `namespaces.ts` file.

  return <h1>{t("Welcome, ", { username: "John" })}</h1>;
};

Example usage in server-side code:

import { getTranslation } from "@/i18n";

const { t } = await getTranslation("shared.services.api");

const errorMessage = t("ERR_NETWORK");

Advantages