Language loading => async + moved livestatus file

This commit is contained in:
DJj123dj
2024-11-25 19:43:58 +01:00
parent 3e928ed06e
commit d0ef2a9873
8 changed files with 139 additions and 53 deletions
+2
View File
@@ -76,6 +76,8 @@ export interface ODEventIds_Default {
//languages
"onLanguageLoad": ODEvent_Default<(languages:ODLanguageManager_Default) => ODPromiseVoid>
"afterLanguagesLoaded": ODEvent_Default<(languages:ODLanguageManager_Default) => ODPromiseVoid>
"onLanguageInit": ODEvent_Default<(languages:ODLanguageManager_Default) => ODPromiseVoid>
"afterLanguagesInitiated": ODEvent_Default<(languages:ODLanguageManager_Default) => ODPromiseVoid>
"onLanguageSelect": ODEvent_Default<(languages:ODLanguageManager_Default) => ODPromiseVoid>
"afterLanguagesSelected": ODEvent_Default<(main:ODLanguage|null, backup:ODLanguage|null, languages:ODLanguageManager_Default) => ODPromiseVoid>
+3
View File
@@ -40,6 +40,8 @@ export interface ODDefaults {
/**Load the default open ticket languages. */
languageLoading:boolean,
/**Enable the default initializer for open ticket languages. */
languageInitiating:boolean,
/**Enable selecting the current language from `config/general.json`. */
languageSelection:boolean,
/**Set the backup language when the primary language is missing a property. */
@@ -240,6 +242,7 @@ export class ODDefaultsManager {
sessionLoading:true,
languageLoading:true,
languageInitiating:true,
languageSelection:true,
backupLanguage:"openticket:english",
languageList:[],
+53 -27
View File
@@ -1,7 +1,7 @@
///////////////////////////////////////
//LANGUAGE MODULE
///////////////////////////////////////
import { ODId, ODManager, ODManagerData, ODSystemError, ODValidId } from "./base"
import { ODId, ODManager, ODManagerData, ODPromiseVoid, ODSystemError, ODValidId } from "./base"
import nodepath from "path"
import { ODDebugger } from "./console"
import fs from "fs"
@@ -14,32 +14,6 @@ export interface ODLanguageMetadata {
automated:boolean
}
export class ODLanguage extends ODManagerData {
/**The name of the file with `.json` extension. */
file: string
/**The path to the file relative to the main directory. */
path: string
/**The raw object data of the translation. */
data: any
/**The metadata of the language if available. */
metadata: ODLanguageMetadata|null = null
constructor(id:ODValidId, file:string, customPath?:string){
super(id)
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./languages/",this.file)
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
try{
this.data = JSON.parse(fs.readFileSync(this.path).toString())
}catch(err){
process.emit("uncaughtException",err)
throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\"!")
}
if (this.data["_TRANSLATION"]) this.metadata = this.data["_TRANSLATION"]
}
}
export class ODLanguageManager extends ODManager<ODLanguage> {
current: ODLanguage|null = null
backup: ODLanguage|null = null
@@ -130,4 +104,56 @@ export class ODLanguageManager extends ODManager<ODLanguage> {
})
return translation
}
/**Init all language files. */
async init(){
for (const language of this.getAll()){
try{
await language.init()
}catch(err){
process.emit("uncaughtException",new ODSystemError(err))
}
}
}
}
export class ODLanguage extends ODManagerData {
/**The name of the file with extension. */
file: string = ""
/**The path to the file relative to the main directory. */
path: string = ""
/**The raw object data of the translation. */
data: any
/**The metadata of the language if available. */
metadata: ODLanguageMetadata|null = null
constructor(id:ODValidId, data:any){
super(id)
this.data = data
}
/**Init the language. */
init(): ODPromiseVoid {
//nothing
}
}
export class ODJsonLanguage extends ODLanguage {
constructor(id:ODValidId, file:string, customPath?:string){
super(id,{})
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./languages/",this.file)
}
/**Init the langauge. */
init(): ODPromiseVoid {
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
try{
this.data = JSON.parse(fs.readFileSync(this.path).toString())
}catch(err){
process.emit("uncaughtException",err)
throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\"!")
}
if (this.data["_TRANSLATION"]) this.metadata = this.data["_TRANSLATION"]
}
}
+2
View File
@@ -32,6 +32,8 @@ export const loadAllEvents = () => {
//languages
"onLanguageLoad",
"afterLanguagesLoaded",
"onLanguageInit",
"afterLanguagesInitiated",
"onLanguageSelect",
"afterLanguagesSelected",
+17 -23
View File
@@ -2,32 +2,26 @@ import {openticket, api, utilities} from "../../index"
export const loadAllLanguages = async () => {
//register languages
openticket.languages.add(new api.ODLanguage("openticket:custom","custom.json"))
openticket.languages.add(new api.ODLanguage("openticket:english","english.json"))
openticket.languages.add(new api.ODLanguage("openticket:dutch","dutch.json"))
openticket.languages.add(new api.ODLanguage("openticket:portuguese","portuguese.json"))
openticket.languages.add(new api.ODLanguage("openticket:czech","czech.json"))
openticket.languages.add(new api.ODLanguage("openticket:german","german.json"))
openticket.languages.add(new api.ODLanguage("openticket:catalan","catalan.json"))
openticket.languages.add(new api.ODLanguage("openticket:hungarian","hungarian.json"))
openticket.languages.add(new api.ODLanguage("openticket:spanish","spanish.json"))
openticket.languages.add(new api.ODLanguage("openticket:romanian","romanian.json"))
openticket.languages.add(new api.ODLanguage("openticket:ukrainian","ukrainian.json"))
openticket.languages.add(new api.ODLanguage("openticket:indonesian","indonesian.json"))
openticket.languages.add(new api.ODLanguage("openticket:italian","italian.json"))
openticket.languages.add(new api.ODLanguage("openticket:estonian","estonian.json"))
openticket.languages.add(new api.ODLanguage("openticket:finnish","finnish.json"))
openticket.languages.add(new api.ODLanguage("openticket:danish","danish.json"))
openticket.languages.add(new api.ODLanguage("openticket:thai","thai.json"))
//list for config checker
const languageList = openticket.defaults.getDefault("languageList")
languageList.push("custom","english","dutch","portuguese","czech","german","catalan","hungarian","spanish","romanian","ukrainian","indonesian","italian","estonian","finnish","danish","thai")
openticket.defaults.setDefault("languageList",languageList)
openticket.languages.add(new api.ODJsonLanguage("openticket:custom","custom.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:english","english.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:dutch","dutch.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:portuguese","portuguese.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:czech","czech.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:german","german.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:catalan","catalan.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:hungarian","hungarian.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:spanish","spanish.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:romanian","romanian.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:ukrainian","ukrainian.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:indonesian","indonesian.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:italian","italian.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:estonian","estonian.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:finnish","finnish.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:danish","danish.json"))
openticket.languages.add(new api.ODJsonLanguage("openticket:thai","thai.json"))
/** How to add more languages?
* - Register the language to the manager (see above)
* - Add the language to the list (see above)
* - Add the language to the list in the "ODLanguageManagerIds_Default" interface (./src/core/api/defaults/language.ts)
* - Add the language to the list in the README.md
*/
-1
View File
@@ -9,7 +9,6 @@ export const loadAllTickets = async () => {
const tickets = await ticketDatabase.getCategory("openticket:ticket")
if (!tickets) return
for (const ticket of tickets){
console.log(ticket.value)
try {
openticket.tickets.add(await loadTicket(ticket.value))
}catch (err){
+19 -1
View File
@@ -168,11 +168,29 @@ const main = async () => {
await openticket.events.get("onLanguageLoad").emit([openticket.languages])
await openticket.events.get("afterLanguagesLoaded").emit([openticket.languages])
//initiate language
await openticket.events.get("onLanguageInit").emit([openticket.languages])
if (openticket.defaults.getDefault("languageInitiating")){
await openticket.languages.init()
await openticket.events.get("afterLanguagesInitiated").emit([openticket.languages])
//add available languages to list for config checker
const languageList = openticket.defaults.getDefault("languageList")
const languageIds = openticket.languages.getIds().map((id) => {
if (id.value.startsWith("openticket:")){
//is open ticket language => return without prefix
return id.value.split("openticket:")[1]
}else return id.value
})
languageList.push(...languageIds)
openticket.defaults.setDefault("languageList",languageList)
}
//select language
await openticket.events.get("onLanguageSelect").emit([openticket.languages])
if (openticket.defaults.getDefault("languageSelection")){
//set current language
const languageId = (generalConfig && generalConfig.data.language) ? generalConfig.data.language : "english"
const languageId = (generalConfig?.data?.language) ? generalConfig.data.language : "english"
if (languageId.includes(":")){
openticket.languages.setCurrentLanguage(languageId)
}else{
+42
View File
@@ -0,0 +1,42 @@
[
{
"message":{
"title":"Open Ticket Beta",
"titleColor":"red",
"description":"Hi there! You're using a beta version of the bot! Please report all bugs and errors to our discord server!",
"descriptionColor":"normal"
},
"active":{
"versions":["4.0.0"],
"languages":[],
"allLanguages":true,
"usingPlugins":true,
"notUsingPlugins":true,
"usingSlashCommands":true,
"notUsingSlashCommands":true,
"notUsingTranscripts":true,
"usingTextTranscripts":true,
"usingHtmlTranscripts":true
}
},
{
"message":{
"title":"Documentation",
"titleColor":"blue",
"description":"Currently there is no documentation available yet for this version of the bot.",
"descriptionColor":"normal"
},
"active":{
"versions":["4.0.0"],
"languages":[],
"allLanguages":true,
"usingPlugins":true,
"notUsingPlugins":true,
"usingSlashCommands":true,
"notUsingSlashCommands":true,
"notUsingTranscripts":true,
"usingTextTranscripts":true,
"usingHtmlTranscripts":true
}
}
]