[PR #2917] [CLOSED] feat: Add localization plugin #4553

Closed
opened 2026-03-13 11:50:46 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/2917
Author: @tanosugi
Created: 6/5/2025
Status: Closed

Base: mainHead: add-localization-plugin


📝 Commits (10+)

  • 17d54f9 add locale json files
  • 61a4f03 replace package.json and pnpm-lock for test
  • 35910aa add index.ts, test, update locale json, add generate-plugin-locale-enjson and translate-plugin-locale-other-language to package.json
  • 887ea38 recover catalog in package.json and lock file
  • 5db4595 update option and comments, lint json files
  • 0901af1 add export to package.json
  • 519eff2 add covered-languages.md and some script to package.json
  • 4da86a8 Update packages/better-auth/src/plugins/localization/covered-languages.md
  • f9752bd Update packages/better-auth/src/locales/en.json
  • aa425dc Update packages/better-auth/src/locales/en.json

📊 Changes

32 files changed (+3293 additions, -2 deletions)

View changed files

📝 .gitignore (+3 -0)
📝 packages/better-auth/build.config.ts (+1 -0)
📝 packages/better-auth/package.json (+16 -1)
📝 packages/better-auth/src/adapters/kysely-adapter/test/state.txt (+1 -1)
packages/better-auth/src/locales/ar.json (+122 -0)
packages/better-auth/src/locales/cs.json (+122 -0)
packages/better-auth/src/locales/de.json (+122 -0)
packages/better-auth/src/locales/en.json (+122 -0)
packages/better-auth/src/locales/es.json (+122 -0)
packages/better-auth/src/locales/fr.json (+122 -0)
packages/better-auth/src/locales/hi.json (+122 -0)
packages/better-auth/src/locales/id.json (+122 -0)
packages/better-auth/src/locales/it.json (+122 -0)
packages/better-auth/src/locales/ja.json (+122 -0)
packages/better-auth/src/locales/ko.json (+122 -0)
packages/better-auth/src/locales/nl.json (+122 -0)
packages/better-auth/src/locales/pl.json (+122 -0)
packages/better-auth/src/locales/pt.json (+122 -0)
packages/better-auth/src/locales/ru.json (+122 -0)
packages/better-auth/src/locales/th.json (+122 -0)

...and 12 more files

📄 Description

I made plugin for localization!!
I translate all $ERROR_CODES including plugin and made default translation jsons, implement additional translation functions.
Some of users prefer to localize or use i18n as #1246

Usage:

Example 1: No arguments (default behavior)

Uses Accept-Language header to determine the language and translate.

plugins: [localization()],

Example 2: singleLang

Always uses Japanese for localization, regardless of request headers or URL.

plugins: [localization({
  singleLang: "ja"
})],

Example 3: enabledLocales and multiLangDict (multiple languages)

The message will be translated based on Accept-Language header but limited to the enabledLocales.
simpleDict and multiLangDict are used with priority over default translations.

plugins: [localization({
  enabledLocales: ["en", "ja", "fr"],
  simpleDict: { LOGIN_SUCCESS: "Login successful!" },
  multiLangDict: {
    LOGIN_SUCCESS: {
      en: "Login successful!",
      ja: "ログイン成功!",
      fr: "Connexion réussie !"
    }
  }
})],

Example 4: i18nRouting

The language is determined from the first path segment of the referer URL.

plugins: [localization({
  i18nRouting: ["en", "ja"],
  simpleDict: {
    LOGIN_SUCCESS: "Login successful!"
  }
})],

Example 5: simpleDictWithTFunc and tFuncServer (for dynamic translation)

Used for i18n routing such as next-intl.
The message will be dynamically translated using the t function for the current locale.

plugins: [localization({
  i18nRouting: ["en", "ja"],
  simpleDictWithTFunc: (t) => ({ EMAIL_NOT_VERIFIED: t("Email not verified!!") }),
  tFuncServer: async (locale) => {
    const t = await getTranslations({ locale });
    return (key) => t(key);
  },
})],

Use plugin.onRequest and plugin.onResponse in your auth flow

Covered Languages

Code Language
en English
ja Japanese
fr French
de German
es Spanish
it Italian
nl Dutch
pl Polish
pt Portuguese
ru Russian
th Thai
tr Turkish
uk Ukrainian
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
ko Korean
id Indonesian
hi Hindi
cs Czech
ar Arabic

Automatically create en.json files

cd packages/better-auth
pnpm run generate-plugin-locale-enjson

Automatically add added error code translations

Save google-service-account.json to packages/better-auth/src/plugins/localization/ and then,

cd packages/better-auth
pnpm run translate-plugin-locale-other-language

How to add language

Add XX.json to packages/better-auth/src/locales contents should be

{}

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/2917 **Author:** [@tanosugi](https://github.com/tanosugi) **Created:** 6/5/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `add-localization-plugin` --- ### 📝 Commits (10+) - [`17d54f9`](https://github.com/better-auth/better-auth/commit/17d54f9fd0e6df9e4133a8cd99dd3d8d61d3c1eb) add locale json files - [`61a4f03`](https://github.com/better-auth/better-auth/commit/61a4f03086efc679aa23cdd2fcba5d82236bc58a) replace package.json and pnpm-lock for test - [`35910aa`](https://github.com/better-auth/better-auth/commit/35910aaa3a59e9756fbee9946a9a926c51e5474b) add index.ts, test, update locale json, add generate-plugin-locale-enjson and translate-plugin-locale-other-language to package.json - [`887ea38`](https://github.com/better-auth/better-auth/commit/887ea38b5ab4e70c78993ea448d1a999c30baa50) recover catalog in package.json and lock file - [`5db4595`](https://github.com/better-auth/better-auth/commit/5db45952c5a252b774db824b5d4055f977c6bca2) update option and comments, lint json files - [`0901af1`](https://github.com/better-auth/better-auth/commit/0901af1d85d0c7082a52ff4e1fa14c97a06c7efa) add export to package.json - [`519eff2`](https://github.com/better-auth/better-auth/commit/519eff237e993e8913f9cc9e51fb66ce2ec2aa5c) add covered-languages.md and some script to package.json - [`4da86a8`](https://github.com/better-auth/better-auth/commit/4da86a81b9b1bdf1d2c44685c9e68062c46c17a3) Update packages/better-auth/src/plugins/localization/covered-languages.md - [`f9752bd`](https://github.com/better-auth/better-auth/commit/f9752bd509f42cebcb6b30251aab1580c3a3302b) Update packages/better-auth/src/locales/en.json - [`aa425dc`](https://github.com/better-auth/better-auth/commit/aa425dcd8351c450387652b6fe222f9a420b8b8d) Update packages/better-auth/src/locales/en.json ### 📊 Changes **32 files changed** (+3293 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `.gitignore` (+3 -0) 📝 `packages/better-auth/build.config.ts` (+1 -0) 📝 `packages/better-auth/package.json` (+16 -1) 📝 `packages/better-auth/src/adapters/kysely-adapter/test/state.txt` (+1 -1) ➕ `packages/better-auth/src/locales/ar.json` (+122 -0) ➕ `packages/better-auth/src/locales/cs.json` (+122 -0) ➕ `packages/better-auth/src/locales/de.json` (+122 -0) ➕ `packages/better-auth/src/locales/en.json` (+122 -0) ➕ `packages/better-auth/src/locales/es.json` (+122 -0) ➕ `packages/better-auth/src/locales/fr.json` (+122 -0) ➕ `packages/better-auth/src/locales/hi.json` (+122 -0) ➕ `packages/better-auth/src/locales/id.json` (+122 -0) ➕ `packages/better-auth/src/locales/it.json` (+122 -0) ➕ `packages/better-auth/src/locales/ja.json` (+122 -0) ➕ `packages/better-auth/src/locales/ko.json` (+122 -0) ➕ `packages/better-auth/src/locales/nl.json` (+122 -0) ➕ `packages/better-auth/src/locales/pl.json` (+122 -0) ➕ `packages/better-auth/src/locales/pt.json` (+122 -0) ➕ `packages/better-auth/src/locales/ru.json` (+122 -0) ➕ `packages/better-auth/src/locales/th.json` (+122 -0) _...and 12 more files_ </details> ### 📄 Description I made plugin for localization!! I translate all $ERROR_CODES including plugin and made default translation jsons, implement additional translation functions. Some of users prefer to localize or use i18n as #1246 ## Usage: ### Example 1: No arguments (default behavior) Uses Accept-Language header to determine the language and translate. ```ts plugins: [localization()], ``` ### Example 2: singleLang Always uses Japanese for localization, regardless of request headers or URL. ```ts plugins: [localization({ singleLang: "ja" })], ``` ### Example 3: enabledLocales and multiLangDict (multiple languages) The message will be translated based on Accept-Language header but limited to the enabledLocales. simpleDict and multiLangDict are used with priority over default translations. ```ts plugins: [localization({ enabledLocales: ["en", "ja", "fr"], simpleDict: { LOGIN_SUCCESS: "Login successful!" }, multiLangDict: { LOGIN_SUCCESS: { en: "Login successful!", ja: "ログイン成功!", fr: "Connexion réussie !" } } })], ``` ### Example 4: i18nRouting The language is determined from the first path segment of the referer URL. ```ts plugins: [localization({ i18nRouting: ["en", "ja"], simpleDict: { LOGIN_SUCCESS: "Login successful!" } })], ``` ### Example 5: simpleDictWithTFunc and tFuncServer (for dynamic translation) Used for i18n routing such as next-intl. The message will be dynamically translated using the t function for the current locale. ```ts plugins: [localization({ i18nRouting: ["en", "ja"], simpleDictWithTFunc: (t) => ({ EMAIL_NOT_VERIFIED: t("Email not verified!!") }), tFuncServer: async (locale) => { const t = await getTranslations({ locale }); return (key) => t(key); }, })], ``` Use plugin.onRequest and plugin.onResponse in your auth flow ## Covered Languages | Code | Language | |--------|----------------| | en | English | | ja | Japanese | | fr | French | | de | German | | es | Spanish | | it | Italian | | nl | Dutch | | pl | Polish | | pt | Portuguese | | ru | Russian | | th | Thai | | tr | Turkish | | uk | Ukrainian | | zh-CN | Chinese (Simplified) | | zh-TW | Chinese (Traditional) | | ko | Korean | | id | Indonesian | | hi | Hindi | | cs | Czech | | ar | Arabic | ## Automatically create en.json files ```bash cd packages/better-auth pnpm run generate-plugin-locale-enjson ``` ## Automatically add added error code translations Save google-service-account.json to packages/better-auth/src/plugins/localization/ and then, ```bash cd packages/better-auth pnpm run translate-plugin-locale-other-language ``` ## How to add language Add XX.json to packages/better-auth/src/locales contents should be ``` {} ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 11:50:46 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#4553