created lang file
This commit is contained in:
57
src/lang.ts
Normal file
57
src/lang.ts
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user