created lang file

This commit is contained in:
2022-02-13 18:56:12 -06:00
parent a1a387880c
commit 9da9650f92
2 changed files with 104 additions and 0 deletions

57
src/lang.ts Normal file
View File

@@ -0,0 +1,57 @@
type LangObj = { [keys:string]: LangObj | string }
type LangObjWhold = { [langid:string]: LangObj }
const LANG: LangObjWhold = {
en: {
discord: {
error: {
noActiveQueue: 'There is not an active queue in this channel, type `/open` to create one'
}
}
}
}
export namespace Lang {
var LANGID = 'en';
if (!(LANGID in LANG))
throw 'language id does not exist';
export function setLang(langid: string) {
if (langid in LANG)
LANGID = langid;
else
throw 'language id does not exist';
}
/**
* reads language json
* @param id ex: discord.error.noActiveQueue
* @returns language value, defaults to `id` parameter
*/
export function get(id: string): string {//discord.error.noActiveQueue
let keySpl = id.split('.').map(k => k.trim()).filter(k => k);
let finding = LANG[LANGID];
for (let key of keySpl) {
if (key in finding) {
let found = finding[key];
if (typeof found === 'string')
return found;
finding = found;
} else
break;
}
return id;
}
}