Better Auth Localization Plugin

3.0.0 · active · verified Wed Apr 22

The `better-auth-localization` plugin provides comprehensive internationalization (i18n) capabilities for error messages generated by the `better-auth` library and its official plugins. It aims to offer a seamless developer experience by automatically translating error messages without requiring changes to application logic. Currently at v3.0.0, this package regularly releases patch updates for new language support and minor fixes, with major versions tied to significant updates in its peer dependency, `better-auth`. Key features include multi-language support, automatic error message translation, full TypeScript type safety with autocomplete for custom translations, a robust fallback system for missing translations, flexible locale detection strategies (e.g., from headers, cookies, or database), and zero runtime overhead as translations are bundled at build time. It differentiates itself by tightly integrating with the `better-auth` ecosystem to provide a specialized localization solution.

Common errors

Warnings

Install

Imports

Quickstart

Initializes `better-auth` with the `localization` plugin, setting a default locale and a fallback. This demonstrates basic integration.

import { betterAuth } from 'better-auth';
import { localization } from 'better-auth-localization';

// Example: Simulate a basic better-auth config
const exampleConfig = {
  secret: process.env.AUTH_SECRET ?? 'super-secret-development-key',
  cookie: {
    secure: process.env.NODE_ENV === 'production'
  }
};

export const auth = betterAuth({
  ...exampleConfig,
  plugins: [
    localization({
      defaultLocale: 'pt-BR', // Use built-in Portuguese translations
      fallbackLocale: 'default', // Fallback to English
      // For dynamic locale detection, you would add a getLocale function here:
      // getLocale: async (req) => {
      //   // Imagine reading locale from a cookie or header
      //   const cookieHeader = (req as any)?.headers?.get("cookie");
      //   const cookies = cookieHeader ? Object.fromEntries(cookieHeader.split('; ').map(c => c.split('='))) : {};
      //   return cookies.locale ?? 'default';
      // }
    })
  ]
});

// To demonstrate usage (this part is illustrative and not part of the quickstart plugin setup):
// try {
//   // Simulate an authentication call that might throw an error
//   // const user = await auth.signIn('test@example.com', 'wrong-password');
// } catch (error: any) {
//   console.log('Localized error:', error.message); // Should output translated message
// }

view raw JSON →