
Company News
Andrew Becherer Joins Socket as Chief Information Security Officer
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.
better-auth-localization
Advanced tools
A custom plugin for better-auth that allows you to localize your error messages.
A localization plugin for Better Auth that automatically translates error messages from the core library and official plugins.
npm install better-auth-localization
# or
yarn add better-auth-localization
# or
pnpm add better-auth-localization
import { betterAuth } from "better-auth";
import { localization } from "better-auth-localization";
export const auth = betterAuth({
// ... your config
plugins: [
localization({
defaultLocale: "pt-BR", // Use built-in Portuguese translations
fallbackLocale: "default" // Fallback to English
})
]
});
The getLocale option lets you dynamically detect the locale for each request.
It receives a raw Request object, so you can read cookies, headers, JWT tokens, database info, whatever makes sense in your app.
This plugin does not know your user/session automatically. You decide how to extract the locale.
localization({
defaultLocale: "pt-BR",
fallbackLocale: "default",
getLocale: async (req) => {
try {
const locale = await getUserLocale(req);
return locale ?? "default";
} catch (err) {
console.warn("Error detecting locale:", err);
return "default";
}
}
});
getUserLocale (choose the strategy you prefer)Below are a few common ways to detect the locale.
Pick one or combine them depending on your app’s architecture.
async function getUserLocale(req: Request): Promise<string | null> {
const cookieHeader = req.headers.get("cookie");
const cookies = parseCookie(cookieHeader);
return cookies.locale ?? null;
}
async function getUserLocale(req: Request): Promise<string | null> {
return req.headers.get("x-user-locale");
}
Useful if your authentication stores user data inside a token.
async function getUserLocale(req: Request): Promise<string | null> {
const cookieHeader = req.headers.get("cookie");
const session = decodeSession(cookieHeader); // your own logic
return session?.user?.locale ?? null;
}
Only makes sense if your session/cookie includes a userId.
async function getUserLocale(req: Request): Promise<string | null> {
const cookieHeader = req.headers.get("cookie");
const session = decodeSession(cookieHeader); // extract userId
const userId = session?.userId;
if (!userId) return null;
const locale = await db.user.getLocale(userId);
return locale ?? null;
}
If you have suggestions for more examples or want to see a built-in helper, feel free to open an issue!
localization({
defaultLocale: "fr",
fallbackLocale: "pt-BR", // Can also use built-in locales as fallback
translations: {
"fr": {
USER_NOT_FOUND: "Utilisateur non trouvé",
INVALID_PASSWORD: "Mot de passe invalide",
INVALID_EMAIL: "Email invalide",
SESSION_EXPIRED: "Session expirée"
},
"es": {
USER_NOT_FOUND: "Usuario no encontrado",
INVALID_PASSWORD: "Contraseña inválida",
INVALID_EMAIL: "Email inválido",
SESSION_EXPIRED: "Sesión expirada"
}
},
});
localization({
defaultLocale: "pt-BR",
translations: {
"pt-BR": {
// Override specific messages
USER_NOT_FOUND: "Usuário não foi encontrado no sistema",
INVALID_PASSWORD: "A senha fornecida está incorreta"
}
}
});
Currently supported languages:
The plugin comes with built-in translations for all Better Auth error codes, including:
Core Library:
Official Plugins:
For a complete and specific list, refer to the Better Auth documentation.
We welcome and appreciate contributions! Help us expand language support by adding new translations.
This repo auto-generates the translations index to avoid PR conflicts when multiple languages are added in parallel.
npm run generate:locale
# or
pnpm generate:locale
# or
yarn generate:locale
nl_NL, fr_CA, es_MX)src/translations/index.ts.The build runs the generator automatically to ensure the index stays up to date.
MIT © Marcel Losso Forte
FAQs
A custom plugin for better-auth that allows you to localize your error messages.
The npm package better-auth-localization receives a total of 3,237 weekly downloads. As such, better-auth-localization popularity was classified as popular.
We found that better-auth-localization 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.
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.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.

Company News
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.

Security News
npm confirmed a tooling bug incorrectly marked several one-character packages as security holders and said it was working on a rollback.