Skip to content

Commit 4be3e5a

Browse files
authored
Merge pull request #27 from marcellosso/fix/translations-on-server-side
fix/translations-on-server-side
2 parents 4e5c119 + 1ead0af commit 4be3e5a

25 files changed

Lines changed: 39 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# better-auth-localization
22

3+
## 2.1.7
4+
5+
### Patch Changes
6+
7+
- Fixed issue where translations were not applied on server side requests
8+
- Added support for Danish (Thanks @BjornFrancke)
9+
310
## 2.1.6
411

512
### Patch Changes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ This repo auto-generates the translations index to avoid PR conflicts when multi
161161
1. Create a new file in `src/translations/`, for example `nl.ts`.
162162
2. Export your translation object and a `LOCALES` map from that file:
163163
```ts
164-
import { ErrorCodesType } from "../types";
164+
import type { ErrorCodesType } from "../types";
165165

166166
export const NL_NL = { /* ...all error codes... */ } satisfies ErrorCodesType;
167167

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "better-auth-localization",
3-
"version": "2.1.6",
3+
"version": "2.1.7",
44
"author": "Marcel Losso",
55
"description": " A custom plugin for better-auth that allows you to localize your error messages.",
66
"main": "dist/index.js",

src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { BetterAuthPlugin } from "better-auth";
1+
import type { BetterAuthPlugin, Status } from "better-auth";
22
import { createAuthMiddleware } from "better-auth/plugins";
33
import { defaultTranslations } from "./translations";
44
import type {
55
AvailableLocales,
6+
ErrorCodesType,
67
LocalizationOptions,
78
PartialErrorCodesType,
89
} from "./types";
@@ -76,7 +77,7 @@ export const localization = <
7677
);
7778

7879
const resolveLocale = getLocale
79-
? async (request: Request) => {
80+
? async (request: Request | undefined) => {
8081
try {
8182
const locale = (await getLocale(
8283
request,
@@ -111,13 +112,11 @@ export const localization = <
111112
handler: createAuthMiddleware(async (ctx) => {
112113
const { request } = ctx;
113114

114-
if (!request) return;
115-
116115
const { returned } = ctx.context;
117116

118117
if (!hasBody(returned) || !isErrorBody(returned.body)) return;
119118

120-
const { body } = returned;
119+
const { body, statusCode } = returned as { body: { code: keyof ErrorCodesType; message: string }; statusCode: Status };
121120

122121
const locale = await resolveLocale(request);
123122

@@ -132,7 +131,9 @@ export const localization = <
132131
);
133132

134133
if (translatedMessage) {
135-
body.message = translatedMessage;
134+
return ctx.error(statusCode || "UNPROCESSABLE_ENTITY", {
135+
message: translatedMessage,
136+
});
136137
}
137138
}),
138139
},

src/translations/ar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorCodesType } from "../types";
1+
import type { ErrorCodesType } from "../types";
22

33
export const AR_SA = {
44
// User related errors

src/translations/da.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorCodesType } from "../types";
1+
import type { ErrorCodesType } from "../types";
22

33
export const DA_DK = {
44
// User related errors

src/translations/de.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorCodesType } from "../types";
1+
import type { ErrorCodesType } from "../types";
22

33
export const DE_DE_INFORMAL = {
44
// User related errors

src/translations/el.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorCodesType } from "../types";
1+
import type { ErrorCodesType } from "../types";
22

33
export const EL_GR = {
44
// User related errors

src/translations/es.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorCodesType } from "../types";
1+
import type { ErrorCodesType } from "../types";
22

33
export const ES_ES = {
44
// User related errors

src/translations/fa.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ErrorCodesType } from "../types";
1+
import type { ErrorCodesType } from "../types";
22

33
export const FA_IR = {
44
// User related errors

0 commit comments

Comments
 (0)