Add translation support

Also converts some things to use it.
This commit is contained in:
7cc5c4f330d47060 2024-07-22 20:33:16 -04:00
parent 658d6b1a6e
commit 29e59ed0d0
8 changed files with 65 additions and 14 deletions

View file

@ -2,6 +2,7 @@ const fs=require("fs");
const Command=require("../util/Command.js");
const hashcheck=require("../util/hashcheck.js");
const settings = require("../settings.json");
const getMessage = require('../util/lang.js');
let cmds=Object.create(null);
module.exports={
load:()=>{
@ -17,6 +18,7 @@ module.exports={
return;
}
const cmd=text.split(" ");
let lang=settings.defaultLang;
let verify=hashcheck(cmd);
if(verify>0){
text=cmd.slice(0,cmd.length-1).join(" ");
@ -25,33 +27,33 @@ module.exports={
const command = cmds[cmd[0].toLowerCase()];
if(command.level!==undefined && command.level>verify){
b.tellraw(uuid,{
text:"You do not have permission to run this command. If you have permission, please make sure you put the command hash at the end, or ran the command through your client's hashing system."
text:getMessage(lang,"command.disallowed.perms")
});
b.tellraw(uuid,{
text:"Your permission level: "+verify
text:getMessage(lang,"command.disallowed.perms.yourLevel",[verify+""])
});
b.tellraw(uuid,{
text:"Command requires: "+command.level
text:getMessage(lang,"command.disallowed.perms.cmdLevel",[command.level+""])
});
return;
}
try{
cmds[cmd[0].toLowerCase()].execute(new Command(uuid,name,"nick N/A",text,prefix,b,verify))
} catch(e) {
console.log(e); b.chat("An error occured (check console for more info)")
console.log(e); b.chat(getMessage(lang,"command.error"))
}
}
}
b.printHelp=(uuid,prefix)=>{
b.printHelp=(uuid,prefix,lang)=>{
let helpCmds=[];
for(const i in cmds){
if(cmds[i].hidden) continue;
helpCmds.push(prefix+i)
}
b.tellraw(uuid,{"text":"Commands: "+helpCmds.join(" ")});
b.tellraw(uuid,{"text":getMessage(lang,"command.help.cmdList",[helpCmds.join(" ")])});
}
b.printCmdHelp=(uuid,cmd)=>{
b.tellraw(uuid,{"text":cmd+cmds[cmd].usage+" - "+cmds[cmd].desc});
b.printCmdHelp=(uuid,cmd,lang)=>{
b.tellraw(uuid,{"text":getMessage(lang,"command.help.commandInfo",[cmd,getMessage(lang,`command.${cmd}.usage`),getMessage(lang,"command.${cmd}.desc")])});
}
},
loadCMD:()=>{

View file

@ -1,12 +1,13 @@
const version = require("../../version.json")
const settings = require('../../settings.json')
const getMessage = require('../../util/lang.js')
module.exports = {
execute: function (c) {
c.reply({text:`${settings.name} - a minecraft bot made by 77c8f4699b732c11 / a5a06d596f15c7db`});
c.reply({text:getMessage(c.lang,"command.about.author",[settings.name])});
c.reply({text:""});
c.reply({text:`Version ${version.bot}`});
c.reply({text:getMessage(c.lang,"command.about.version",[version.bot])});
c.reply({text:""});
c.reply({text:"To view system information, run the command \"serverinfo\"."})
c.reply({text:getMessage(c.lang,"command.about.serverinfo")})
},
desc: 'About the bot',
usage: '',

View file

@ -2,6 +2,7 @@
"secret":"./settings_s.json",
"name": "Minecraft Bot",
"version_mc": "1.20.4",
"defaultLang": "en-US",
"prefix":[
"ubot:",
"\""

View file

@ -1,7 +1,8 @@
//HOW TO WRITE CLASS JS
const parse = require("../util/chatparse.js")
const parse = require("../util/chatparse.js");
const settings = require("../settings.json");
class Command{
constructor (uuid,user,nick,cmd,prefix,bot,verify){
constructor (uuid,user,nick,cmd,prefix,bot,verify,lang = settings.defaultLang){
this.send = (text,uuid)=>{bot.tellraw(uuid?uuid:"@a",text)};
this.reply = text => bot.tellraw(uuid,text);
this.uuid = uuid;
@ -16,6 +17,7 @@ class Command{
this.verify = verify;
this.host = bot.host.host;
this.port = bot.host.port;
this.lang = lang;
}
}

View file

@ -1,7 +1,8 @@
//HOW TO WRITE CLASS JS
const index = require('../index.js')
const { cmds } = require('../plugins/command.js');
const parse = require("../util/chatparse.js")
const parse = require("../util/chatparse.js");
const settings = require("../settings.json");
class ConsoleCommand{
constructor (cmd,index2){
this.send = ()=>{}; //not needed for console
@ -30,6 +31,7 @@ class ConsoleCommand{
this.verify = 3;
this.host = "";
this.port = "3"; // :3
this.lang = settings.defaultLang;
}
}

27
util/lang.js Normal file
View file

@ -0,0 +1,27 @@
const fs=require("fs")
let languages={};
const loadplug = (botno) => {
const bpl = fs.readdirSync('util/lang')
for (const i in bpl) {
if (!bpl[i].endsWith('.json')) {
continue
}
try {
languages[bpl[i].split(".")[0]]=require(`./lang/${bpl[i]}`)
} catch (e) { console.log(e) }
}
}
loadplug()
module.exports = function (l, msg, with2) {
let message=msg.replace(/%%/g, '\ue123');
if (languages[l][message] !== undefined) {
message = languages[l][message].replace(/%%/g, '\ue123')
} else if (languages["en-US"][message] !== undefined) {
message = languages["en-US"][message].replace(/%%/g, '\ue123')
}
for(i in with2){
message = message.replace(/%s/, with2[i].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
message = message.replaceAll(`%${+i+1}$s`, with2[i].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
}
return message.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
}

11
util/lang/en-US.json Normal file
View file

@ -0,0 +1,11 @@
{
"command.about.author": "%s - a Minecraft bot made by 77c8f4699b732c11 / a5a06d596f15c7db",
"command.about.version": "Version %s",
"command.about.serverinfo": "To view system information, run the command \"serverinfo\".",
"command.help.cmdList": "Commands: %s",
"command.help.commandInfo": "%s%s - %s",
"command.error": "An error occured (check console for more info)",
"command.disallowed.perms": "You do not have permission to run this command. If you have permission, please make sure you put the command hash at the end, or ran the command through your client's hashing system.",
"command.disallowed.perms.yourLevel": "Your permission level: %s",
"command.disallowed.perms.cmdLevel": "Command requires: %s"
}

5
util/lang/he-IL.json Normal file
View file

@ -0,0 +1,5 @@
{
"command.about.author": "%s - a Minecwaft bot made by 77c8f4699b732c11 / a5a06d596f15c7db",
"command.about.version": "Vewsion %s",
"command.about.serverinfo": "To view system infowmation, wun the command \"serverinfo\"."
}