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.
npm install better-auth-localizationVerified import paths — ran on the pinned version, not inferred.
Initializes `better-auth` with the `localization` plugin, setting a default locale and a fallback. This demonstrates basic integration.
Update `better-auth` to `>=1.5.0` in your project's `package.json` and reinstall dependencies.
Implement a `getLocale: async (req: Request) => Promise<string | null>` function within the `localization` plugin options to parse the locale from the incoming request (e.g., `req.headers.get('x-user-locale')` or from cookies/JWTs).If upgrading from an older version and relying on partial custom translations, ensure you are on `v2.1.6` or later. For older versions, you would need to provide a complete translation object for any language you wish to customize.
Ensure `localization(...)` is added to the `plugins: [...]` array when initializing `betterAuth`.
Change `import betterAuth from 'better-auth';` to `import { betterAuth } from 'better-auth';`Upgrade your `better-auth` package to version `1.5.0` or higher using `npm install better-auth@latest` or `pnpm add better-auth@latest`.
Verify that `localization` is included in the `betterAuth` plugins array. Ensure `defaultLocale` and `fallbackLocale` are correctly set, or implement a `getLocale` function that returns a supported locale.
Ensure the `Request` object provided to `getLocale` matches the expected `Request` interface (e.g., from `@types/node` or a specific web framework's types). You may need to cast the request object or adjust your type definitions.