Skip to content

Commit 76e27b6

Browse files
author
VuXfi
committed
feat: implement localization for OTP messages and add default locale setting
1 parent a83160d commit 76e27b6

File tree

8 files changed

+46
-3
lines changed

8 files changed

+46
-3
lines changed

adapters/otp/default/defaultOTP.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { coreI18n } from "../../../hook/bindLocales";
12
import { OneTimePasswordRecord } from "../../../models/OneTimePassword";
23
import OneTimePasswordAdapter from "../OneTimePasswordAdapter";
34
// todo: fix types model instance to {%ModelName%}Record for OneTimePassword"
@@ -18,7 +19,10 @@ export class DefaultOTP extends OneTimePasswordAdapter {
1819
let mainLoginField = await Settings.get("CORE_LOGIN_FIELD") ?? 'phone';
1920
if (NotificationManager.isChannelExist(mainLoginField === "phone" ? "sms" : mainLoginField)){
2021
try {
21-
await NotificationManager.sendMessageToUser("info", `Your code is ${otp.password}`,{ phone: { code: "", number:login}});
22+
let from = await Settings.get("PROJECT_NAME")
23+
from = from ? from + ": " : ""
24+
const message = from + (await coreI18n(`Your secret login code:`)) + " " + otp.password
25+
await NotificationManager.sendMessageToUser("info", message, { phone: { code: "", number:login}});
2226
} catch (error) {
2327
sails.log.error(`SEND OTP ERROR: ${error}`)
2428
}

hook/bindLocales.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,21 @@ export default async function () {
3131
} catch (e) {
3232
sails.log.warn("restocore bindTranslations > Error:", e);
3333
}
34+
}
35+
36+
export async function coreI18n(key: string, target?: string): Promise<string> {
37+
try {
38+
const locales = sails.hooks.i18n.getLocales();
39+
const currentLocale = target ?? await Settings.get("DEFAULT_LOCALE");
40+
41+
if (locales[currentLocale] && locales[currentLocale][key]) {
42+
return locales[currentLocale][key];
43+
} else {
44+
console.warn(`Translation for key '${key}' not found in locale '${currentLocale}'`);
45+
return key;
46+
}
47+
} catch (error) {
48+
console.error(`Error translating key '${key}':`, error);
49+
return key;
50+
}
3451
}

interfaces/globalTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,6 @@ declare global {
161161
*/
162162
STRICT_PHONE_VALIDATION: boolean
163163
BONUS_BANNER_HTML_CHUNK: string
164+
DEFAULT_LOCALE: string
164165
}
165166
}

libs/locales/de.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Your code is:": "Ihr Code lautet:",
3+
"Your secret login code:": "Ihr geheimer Login-Code:"
4+
}

libs/locales/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{}
1+
{
2+
"Your secret login code:": "Your secret login code:"
3+
}

libs/locales/fr.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Your secret login code:": "Votre code secret de connexion :"
3+
}

libs/locales/ru.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"Delivery not allowed": "Доставка невозможна"
2+
"Delivery not allowed": "Доставка невозможна",
3+
"Your secret login code:": "Ваш секретный код входа:"
34
}

settings/default_locale.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"key": "DEFAULT_LOCALE",
3+
"type": "string",
4+
"name": "Default Locale",
5+
"description": "Default locale for the application. Default is 'en'.",
6+
"jsonSchema": {
7+
"type": "string",
8+
"enum": ["en", "ru", "fr", "de"]
9+
},
10+
"isRequired": true
11+
}

0 commit comments

Comments
 (0)