fix locale issues

This commit is contained in:
2024-08-13 22:12:17 +03:00
parent ab1fb6ef88
commit ce92b7ef1c
3 changed files with 65 additions and 11 deletions

View File

@@ -58,11 +58,6 @@ import { db } from "./db.js";
import fs from "fs";
import { I18n } from "i18n-js";
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
i18n.defaultLocale = "en";
const config = readConfig();
@@ -79,6 +74,11 @@ client.on("room.message", async (roomId, event) => {
try {
if (event.sender === await client.getUserId()) return;
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
if (!await doesUserExist(roomId)) {
i18n.locale = 'en'
await client.sendText(roomId, i18n.t(["errors", "usernotexists"]));
@@ -333,6 +333,11 @@ client.on("room.invite", async (roomId, event) => {
await client.leaveRoom(roomId);
}
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
logInfo(`Bot has joined a room with ID ${roomId}`);
let mx_id = event.sender;

View File

@@ -32,12 +32,7 @@ const maxAmountOfPhotoesPerUser = config.maxAmountOfPhotoesPerUser;
import { I18n } from "i18n-js";
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
i18n.defaultLocale = "en";
const requiresLengths = {
"location": 32,
@@ -47,6 +42,11 @@ const requiresLengths = {
const processRequest = async (client, roomId, question, answer, nextQuestion) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
let preferredLanguage = await getUserLanguage(roomId);
if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
i18n.locale = preferredLanguage;
@@ -60,6 +60,7 @@ const processRequest = async (client, roomId, question, answer, nextQuestion) =>
await db.query(`UPDATE users SET ${q[0]} = $1 WHERE room_id = $2`, [answer, roomId]);
else
await db.query(`UPDATE users SET ${question} = $1 WHERE room_id = $2`, [answer, roomId]);
await client.sendText(roomId, i18n.t(["general", "setopt"], { opt: answer }));
if (nextQuestion == "menu")
await client.sendText(roomId, i18n.t(["general", "menu"]));
@@ -98,6 +99,11 @@ ${chosenProfile.description}`;
}
const showRandomProfileToUser = async (client, roomId) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
let preferredLanguage = await getUserLanguage(roomId);
if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
i18n.locale = preferredLanguage;
@@ -112,10 +118,18 @@ const showRandomProfileToUser = async (client, roomId) => {
await showProfile(client, roomId, chosenProfile);
await setUserCurrentlyViewingProfile(roomId, chosenProfile.room_id);
await client.sendText(roomId, i18n.t(["general", "rate"]));
};
const showProfileToUser = async (client, roomId, profileId) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
let preferredLanguage = await getUserLanguage(roomId);
if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
i18n.locale = preferredLanguage;
@@ -140,6 +154,11 @@ const showNewLikes = async (client, roomId) => {
};
const wait_start = async (client, roomId, current_action, answer, update) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
let a = answer.split(" ");
if (a[0] !== "!start") return;
if (a[1] !== "ru" && a[1] !== "en") return;
@@ -150,6 +169,11 @@ const wait_start = async (client, roomId, current_action, answer, update) => {
}
const loc = async (client, roomId, current_action, answer, update) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
let number = parseInt(answer);
if (!number) {
let cities = await findCity(answer);
@@ -190,6 +214,11 @@ const loc = async (client, roomId, current_action, answer, update) => {
}
const range = async (client, roomId, current_action, answer, update) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
answer = parseInt(answer.split(" ")[0]);
if (!answer && answer != 0) {
await client.sendText(roomId, i18n.t(["setup", "range"]));
@@ -199,6 +228,11 @@ const range = async (client, roomId, current_action, answer, update) => {
}
const age = async (client, roomId, current_action, answer, update) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
answer = parseInt(answer);
if (!answer) {
await client.sendText(roomId, i18n.t(["setup", "age"]));
@@ -215,6 +249,11 @@ const age = async (client, roomId, current_action, answer, update) => {
}
const sex = async (client, roomId, current_action, answer, update) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
answer = answer.toLowerCase().trim();
if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") {
await client.sendText(roomId, i18n.t(["errors", "twosexes"]));
@@ -224,6 +263,11 @@ const sex = async (client, roomId, current_action, answer, update) => {
}
const interest = async (client, roomId, current_action, answer, update) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
answer = answer.toLowerCase().trim();
if (answer != "male" && answer != "female" && answer != "both") {
await client.sendText(roomId, i18n.t(["errors", "didntunderstand"]));
@@ -233,6 +277,11 @@ const interest = async (client, roomId, current_action, answer, update) => {
}
const pictures = async (client, roomId, current_action, event, update) => {
const i18n = new I18n({
en: JSON.parse(fs.readFileSync("./translations/en.json")),
ru: JSON.parse(fs.readFileSync("./translations/ru.json"))
});
const msgtype = event.content.msgtype;
if (event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') {
await client.sendText(roomId, i18n.t(["setup", "done"]));