Skip to content

Commit 7b8828f

Browse files
author
VuXfi
committed
feat: add coreI18n function for localization and integrate it into OTP message sending
1 parent 76e27b6 commit 7b8828f

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

adapters/otp/default/defaultOTP.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
66
exports.DefaultOTP = void 0;
7+
const bindLocales_1 = require("../../../hook/bindLocales");
78
const OneTimePasswordAdapter_1 = __importDefault(require("../OneTimePasswordAdapter"));
89
// todo: fix types model instance to {%ModelName%}Record for OneTimePassword"
910
class DefaultOTP extends OneTimePasswordAdapter_1.default {
@@ -22,7 +23,10 @@ class DefaultOTP extends OneTimePasswordAdapter_1.default {
2223
let mainLoginField = await Settings.get("CORE_LOGIN_FIELD") ?? 'phone';
2324
if (NotificationManager.isChannelExist(mainLoginField === "phone" ? "sms" : mainLoginField)) {
2425
try {
25-
await NotificationManager.sendMessageToUser("info", `Your code is ${otp.password}`, { phone: { code: "", number: login } });
26+
let from = await Settings.get("PROJECT_NAME");
27+
from = from ? from + ": " : "";
28+
const message = from + (await (0, bindLocales_1.coreI18n)(`Your secret login code:`)) + " " + otp.password;
29+
await NotificationManager.sendMessageToUser("info", message, { phone: { code: "", number: login } });
2630
}
2731
catch (error) {
2832
sails.log.error(`SEND OTP ERROR: ${error}`);

hook/bindLocales.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export default function (): Promise<void>;
2+
export declare function coreI18n(key: string, target?: string): Promise<string>;

hook/bindLocales.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
3434
})();
3535
Object.defineProperty(exports, "__esModule", { value: true });
3636
exports.default = default_1;
37+
exports.coreI18n = coreI18n;
3738
const path = __importStar(require("path"));
3839
const fs = __importStar(require("fs"));
3940
async function default_1() {
@@ -67,3 +68,20 @@ async function default_1() {
6768
sails.log.warn("restocore bindTranslations > Error:", e);
6869
}
6970
}
71+
async function coreI18n(key, target) {
72+
try {
73+
const locales = sails.hooks.i18n.getLocales();
74+
const currentLocale = target ?? await Settings.get("DEFAULT_LOCALE");
75+
if (locales[currentLocale] && locales[currentLocale][key]) {
76+
return locales[currentLocale][key];
77+
}
78+
else {
79+
console.warn(`Translation for key '${key}' not found in locale '${currentLocale}'`);
80+
return key;
81+
}
82+
}
83+
catch (error) {
84+
console.error(`Error translating key '${key}':`, error);
85+
return key;
86+
}
87+
}

interfaces/globalTypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ declare global {
148148
*/
149149
STRICT_PHONE_VALIDATION: boolean;
150150
BONUS_BANNER_HTML_CHUNK: string;
151+
DEFAULT_LOCALE: string;
151152
}
152153
}
153154
export {};

0 commit comments

Comments
 (0)