Skip to content

Commit 6a92cef

Browse files
committed
refactor: enhance translation function to support placeholder values and update related components
1 parent 6ae051f commit 6a92cef

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

src/app/dashboard/_components/ArticleList.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ const ArticleList = () => {
7474

7575
return { previousData };
7676
},
77-
onSuccess(data, variables, context) {
78-
// data
79-
},
8077
onError: (err, variables, context) => {
8178
if (context?.previousData) {
8279
queryClient.setQueryData(["dashboard-articles"], context.previousData);
@@ -113,7 +110,7 @@ const ArticleList = () => {
113110

114111
return { previousData };
115112
},
116-
onError: (err, variables, context) => {
113+
onError: (_, __, context) => {
117114
if (context?.previousData) {
118115
queryClient.setQueryData(["dashboard-articles"], context.previousData);
119116
}
@@ -157,12 +154,12 @@ const ArticleList = () => {
157154
</Link>
158155
{article?.delete_scheduled_at && (
159156
<p className="text-destructive text-sm">
160-
Article will be deleted within{" "}
161-
{differenceInHours(
162-
new Date(article?.delete_scheduled_at!),
163-
new Date()
164-
)}{" "}
165-
hours
157+
{_t("Article will be deleted within $ days", [
158+
differenceInHours(
159+
new Date(article?.delete_scheduled_at!),
160+
new Date()
161+
),
162+
])}
166163
</p>
167164
)}
168165

src/i18n/_t.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
"use server";
22

3-
import {cookies} from "next/headers";
3+
import { cookies } from "next/headers";
44
import bn from "@/i18n/bn.json";
55

66
const dictionaries: {
77
[key: string]: any;
88
} = { bn };
99

10-
const _t = async (key: string) => {
10+
const _t = async (key: string, placeholderValues?: (string | number)[]) => {
1111
const cookiesStore = await cookies();
1212
const _lang = cookiesStore.get("language")?.value || "en";
13-
return dictionaries[_lang]?.[key] || key;
13+
let translation = dictionaries[_lang]?.[key] || key;
14+
15+
if (placeholderValues && placeholderValues.length > 0) {
16+
placeholderValues.forEach((value) => {
17+
translation = translation.replace("$", String(value));
18+
});
19+
}
20+
21+
return translation;
1422
};
1523

1624
export const getLang = async () => {

src/i18n/bn.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@
108108
"Login Sessions": "লগইন সেশন সমূহ",
109109
"These are the login sessions of your account. You can use this to revoke access to your account": "এটি আপনার একাউন্ট এর লগইন সেশন তালিকা। আপনি অন্য ডিভাইস থেকে লগাউট করে দিতে পারবেন",
110110
"You are not logged in": "আপনি লগইন করেননি",
111-
"To do this, you must be logged in": "এটি করতে হলে আপনাকে লগইন করতে হবে"
111+
"To do this, you must be logged in": "এটি করতে হলে আপনাকে লগইন করতে হবে",
112+
"Article will be deleted within $ days": "আর্টিক্যাল $ দিনের মধ্যে মুছে যাবে"
112113
}

src/i18n/use-translation.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ const dictionaries: {
99
export const useTranslation = () => {
1010
const [lang, setLang] = useAtom(i18nLangAtom);
1111
return {
12-
_t: (key: string) => dictionaries?.[lang || "en"]?.[key] || key,
12+
_t: (key: string, placeholderValues?: (string | number)[]) => {
13+
let translation = dictionaries?.[lang || "en"]?.[key] || key;
14+
15+
if (placeholderValues && placeholderValues.length > 0) {
16+
placeholderValues.forEach((value) => {
17+
translation = translation.replace("$", String(value));
18+
});
19+
}
20+
21+
return translation;
22+
},
1323
lang,
1424
toggle: async () => {
1525
setLang(lang === "en" ? "bn" : "en");

0 commit comments

Comments
 (0)