fix locale issues

This commit is contained in:
leca 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 fs from "fs";
import { I18n } from "i18n-js"; 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(); const config = readConfig();
@ -79,6 +74,11 @@ client.on("room.message", async (roomId, event) => {
try { try {
if (event.sender === await client.getUserId()) return; 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)) { if (!await doesUserExist(roomId)) {
i18n.locale = 'en' i18n.locale = 'en'
await client.sendText(roomId, i18n.t(["errors", "usernotexists"])); await client.sendText(roomId, i18n.t(["errors", "usernotexists"]));
@ -333,6 +333,11 @@ client.on("room.invite", async (roomId, event) => {
await client.leaveRoom(roomId); 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}`); logInfo(`Bot has joined a room with ID ${roomId}`);
let mx_id = event.sender; let mx_id = event.sender;

View File

@ -32,12 +32,7 @@ const maxAmountOfPhotoesPerUser = config.maxAmountOfPhotoesPerUser;
import { I18n } from "i18n-js"; 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 = { const requiresLengths = {
"location": 32, "location": 32,
@ -47,6 +42,11 @@ const requiresLengths = {
const processRequest = async (client, roomId, question, answer, nextQuestion) => { 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); let preferredLanguage = await getUserLanguage(roomId);
if (!preferredLanguage) preferredLanguage = i18n.defaultLocale; if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
i18n.locale = preferredLanguage; 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]); await db.query(`UPDATE users SET ${q[0]} = $1 WHERE room_id = $2`, [answer, roomId]);
else else
await db.query(`UPDATE users SET ${question} = $1 WHERE room_id = $2`, [answer, roomId]); await db.query(`UPDATE users SET ${question} = $1 WHERE room_id = $2`, [answer, roomId]);
await client.sendText(roomId, i18n.t(["general", "setopt"], { opt: answer })); await client.sendText(roomId, i18n.t(["general", "setopt"], { opt: answer }));
if (nextQuestion == "menu") if (nextQuestion == "menu")
await client.sendText(roomId, i18n.t(["general", "menu"])); await client.sendText(roomId, i18n.t(["general", "menu"]));
@ -98,6 +99,11 @@ ${chosenProfile.description}`;
} }
const showRandomProfileToUser = async (client, roomId) => { 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); let preferredLanguage = await getUserLanguage(roomId);
if (!preferredLanguage) preferredLanguage = i18n.defaultLocale; if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
i18n.locale = preferredLanguage; i18n.locale = preferredLanguage;
@ -112,10 +118,18 @@ const showRandomProfileToUser = async (client, roomId) => {
await showProfile(client, roomId, chosenProfile); await showProfile(client, roomId, chosenProfile);
await setUserCurrentlyViewingProfile(roomId, chosenProfile.room_id); await setUserCurrentlyViewingProfile(roomId, chosenProfile.room_id);
await client.sendText(roomId, i18n.t(["general", "rate"])); await client.sendText(roomId, i18n.t(["general", "rate"]));
}; };
const showProfileToUser = async (client, roomId, profileId) => { 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); let preferredLanguage = await getUserLanguage(roomId);
if (!preferredLanguage) preferredLanguage = i18n.defaultLocale; if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
i18n.locale = preferredLanguage; i18n.locale = preferredLanguage;
@ -140,6 +154,11 @@ const showNewLikes = async (client, roomId) => {
}; };
const wait_start = async (client, roomId, current_action, answer, update) => { 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(" "); let a = answer.split(" ");
if (a[0] !== "!start") return; if (a[0] !== "!start") return;
if (a[1] !== "ru" && a[1] !== "en") 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 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); let number = parseInt(answer);
if (!number) { if (!number) {
let cities = await findCity(answer); 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 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]); answer = parseInt(answer.split(" ")[0]);
if (!answer && answer != 0) { if (!answer && answer != 0) {
await client.sendText(roomId, i18n.t(["setup", "range"])); 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 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); answer = parseInt(answer);
if (!answer) { if (!answer) {
await client.sendText(roomId, i18n.t(["setup", "age"])); 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 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(); answer = answer.toLowerCase().trim();
if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") { if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") {
await client.sendText(roomId, i18n.t(["errors", "twosexes"])); 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 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(); answer = answer.toLowerCase().trim();
if (answer != "male" && answer != "female" && answer != "both") { if (answer != "male" && answer != "female" && answer != "both") {
await client.sendText(roomId, i18n.t(["errors", "didntunderstand"])); 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 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; const msgtype = event.content.msgtype;
if (event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') { if (event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') {
await client.sendText(roomId, i18n.t(["setup", "done"])); await client.sendText(roomId, i18n.t(["setup", "done"]));

View File

@ -2,7 +2,7 @@
"general": { "general": {
"welcome": "Hello! I am a bot for new meetings in Matrix!\nTo start, please, type \"!start <language>\", where language are 'en' or 'ru'.\nI'll ask some questions to fill your profile. You can change that info later, if you will.\nThen, I'll start showing you another people's profiles.\nYou can like other profile, skip or send a message to the person.\nIf you mutually liked each other, you'll be given each other's MXID to start a DM.\nMy source code can be found on gitea: https://git.foxarmy.org/leca/heart2heart.\n\nGood luck!", "welcome": "Hello! I am a bot for new meetings in Matrix!\nTo start, please, type \"!start <language>\", where language are 'en' or 'ru'.\nI'll ask some questions to fill your profile. You can change that info later, if you will.\nThen, I'll start showing you another people's profiles.\nYou can like other profile, skip or send a message to the person.\nIf you mutually liked each other, you'll be given each other's MXID to start a DM.\nMy source code can be found on gitea: https://git.foxarmy.org/leca/heart2heart.\n\nGood luck!",
"rate": "Decide wether you like (👍️, ❤️ or 1), dislike (👎️, 💔 or 2) or wanna send a messsage (💌 or 3). Write '🏠️' or 4 to quit to menu. Any other response counts as dislike.", "rate": "Decide wether you like (👍️, ❤️ or 1), dislike (👎️, 💔 or 2) or wanna send a messsage (💌 or 3). Write '🏠️' or 4 to quit to menu. Any other response counts as dislike.",
"menu": "Pick an action:\n0) Show your profile\n1) Start watching profiles\n2) Check for mutual likes\n3) Check for messages\n4) Change location\n5) Change range\n6) Change name\n7) Change age\n8) Change sex\n9) Change interest\n10) Change description\n11) Change pictures\n12) Change langage", "menu": "Pick an action:\n0) Show your profile\n1) Start watching profiles\n2) Check for mutual likes\n3) Check for messages\n4) Change location\n5) Change range\n6) Change name\n7) Change age\n8) Change sex\n9) Change interest\n10) Change description\n11) Change pictures\n12) Change language",
"newlikes": "You have new mutual like(s)! Go into menu (🏠️) to see!", "newlikes": "You have new mutual like(s)! Go into menu (🏠️) to see!",
"showalike": "This person and you mutually liked each other!", "showalike": "This person and you mutually liked each other!",
"mxid": "Go drop them a line! Their MXID is: %{mxid}", "mxid": "Go drop them a line! Their MXID is: %{mxid}",