Skip to content

Commit c0cf54a

Browse files
committed
feat: add messageI18n field to notifications for improved localization support
1 parent 55a766e commit c0cf54a

File tree

8 files changed

+38
-10
lines changed

8 files changed

+38
-10
lines changed

apps/nestjs-backend/src/features/notification/notification.service.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export class NotificationService {
113113
fromUserId,
114114
toUserId,
115115
type,
116-
message: JSON.stringify(message),
116+
message: this.getLocalizationText(message, 'en'),
117+
messageI18n: this.getLocalizationJson(message),
117118
urlPath: notifyPath,
118119
createdBy: fromUserId,
119120
};
@@ -125,6 +126,7 @@ export class NotificationService {
125126
notification: {
126127
id: notifyData.id,
127128
message: notifyData.message,
129+
messageI18n: notifyData.messageI18n,
128130
notifyIcon: userIcon,
129131
notifyType: notifyData.type as NotificationTypeEnum,
130132
url: this.mailConfig.origin + notifyPath,
@@ -164,6 +166,17 @@ export class NotificationService {
164166
}) as string);
165167
}
166168

169+
getLocalizationJson(localization: string | ILocalization) {
170+
return typeof localization === 'string'
171+
? undefined
172+
: JSON.stringify({
173+
// replace first . to :
174+
// eg: common.email.templates -> common:email:templates
175+
i18nKey: localization.i18nKey.replace(/\./, ':'),
176+
context: localization.context,
177+
});
178+
}
179+
167180
async sendHtmlContentNotify(
168181
params: {
169182
path: string;
@@ -194,7 +207,8 @@ export class NotificationService {
194207
type,
195208
urlPath: path,
196209
createdBy: fromUserId,
197-
message: JSON.stringify(params.message),
210+
message: this.getLocalizationText(params.message, 'en'),
211+
messageI18n: this.getLocalizationJson(params.message),
198212
};
199213
const notifyData = await this.createNotify(data);
200214

@@ -216,6 +230,7 @@ export class NotificationService {
216230
notification: {
217231
id: notifyData.id,
218232
message: notifyData.message,
233+
messageI18n: notifyData.messageI18n,
219234
notifyType: type,
220235
url: path,
221236
notifyIcon: systemNotifyIcon,
@@ -281,7 +296,8 @@ export class NotificationService {
281296
type,
282297
urlPath: path,
283298
createdBy: fromUserId,
284-
message: JSON.stringify(params.message),
299+
message: this.getLocalizationText(params.message, 'en'),
300+
messageI18n: this.getLocalizationJson(params.message),
285301
};
286302
const notifyData = await this.createNotify(data);
287303

@@ -303,6 +319,7 @@ export class NotificationService {
303319
notification: {
304320
id: notifyData.id,
305321
message: notifyData.message,
322+
messageI18n: notifyData.messageI18n,
306323
notifyType: type,
307324
url: path,
308325
notifyIcon: systemNotifyIcon,

apps/nextjs-app/src/features/app/components/notifications/notification-component/LinkNotification.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,31 @@ interface LinkNotificationProps {
1111
notifyStatus: NotificationStatesEnum;
1212
}
1313

14-
const getShowMessage = (message: string, t: ILocaleFunction) => {
14+
const getShowMessage = (data: INotificationVo['notifications'][number], t: ILocaleFunction) => {
15+
const { message, messageI18n } = data;
1516
try {
16-
const parsedMessage = JSON.parse(message);
17+
if (!messageI18n) {
18+
return message;
19+
}
20+
const parsedMessage = JSON.parse(messageI18n);
1721
const { i18nKey = '', context } = parsedMessage as ILocalization;
1822
if (!i18nKey) {
1923
return message;
2024
}
21-
// replace first . to :
22-
// eg: common.email.templates -> common:email:templates
23-
return getLocalizationMessage({ i18nKey: i18nKey.replace(/\./, ':'), context }, t);
25+
return getLocalizationMessage({ i18nKey, context }, t);
2426
} catch (error) {
2527
return message;
2628
}
2729
};
2830

2931
export const LinkNotification = (props: LinkNotificationProps) => {
3032
const {
31-
data: { url, message: messageString, notifyType },
33+
data,
34+
data: { url, notifyType },
3235
} = props;
3336

3437
const { t } = useTranslation(['common']);
35-
const message = getShowMessage(messageString, t as ILocaleFunction);
38+
const message = getShowMessage(data, t as ILocaleFunction);
3639

3740
return notifyType !== NotificationTypeEnum.ExportBase ? (
3841
<Link href={url}>

packages/core/src/models/notification/notification.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const notificationSchema = z.object({
3434
notifyType: z.nativeEnum(NotificationTypeEnum),
3535
url: z.string(),
3636
message: z.string(),
37+
messageI18n: z.string().nullable().optional(),
3738
isRead: z.boolean(),
3839
createdTime: z.string(),
3940
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "notification" ADD COLUMN "message_i18n" TEXT;

packages/db-main-prisma/prisma/postgres/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ model Notification {
310310
toUserId String @map("to_user_id")
311311
type String @map("type")
312312
message String @map("message")
313+
messageI18n String? @map("message_i18n")
313314
urlPath String? @map("url_path")
314315
isRead Boolean @default(false) @map("is_read")
315316
createdTime DateTime @default(now()) @map("created_time")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "notification" ADD COLUMN "message_i18n" TEXT;

packages/db-main-prisma/prisma/sqlite/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ model Notification {
310310
toUserId String @map("to_user_id")
311311
type String @map("type")
312312
message String @map("message")
313+
messageI18n String? @map("message_i18n")
313314
urlPath String? @map("url_path")
314315
isRead Boolean @default(false) @map("is_read")
315316
createdTime DateTime @default(now()) @map("created_time")

packages/db-main-prisma/prisma/template.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ model Notification {
310310
toUserId String @map("to_user_id")
311311
type String @map("type")
312312
message String @map("message")
313+
messageI18n String? @map("message_i18n")
313314
urlPath String? @map("url_path")
314315
isRead Boolean @default(false) @map("is_read")
315316
createdTime DateTime @default(now()) @map("created_time")

0 commit comments

Comments
 (0)