eslint
This commit is contained in:
113
src/lang.ts
113
src/lang.ts
@@ -26,7 +26,7 @@ const LANG: LangObjWhole = {
|
||||
discord: {
|
||||
noQueue: 'There is not an active queue in this channel, type `/open` to create one',
|
||||
noChannel: 'Unable to find channel {channelId} for teams of {teamsize}',
|
||||
noCreate: 'There is already an active queue in this channel for teams of ${teamsize}',
|
||||
noCreate: 'There is already an active queue in this channel for teams of {teamsize}',
|
||||
inQueue: 'You are already in the queue',
|
||||
notInQueue: 'You aren\'t in the queue',
|
||||
notMod: 'Member is not a moderator'
|
||||
@@ -45,64 +45,61 @@ const LANG: LangObjWhole = {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export namespace Lang {
|
||||
|
||||
var LANGID = 'en';
|
||||
if (!(LANGID in LANG))
|
||||
let 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';
|
||||
|
||||
export function setLang(langid: string) {
|
||||
if (langid in LANG)
|
||||
LANGID = langid;
|
||||
else
|
||||
throw 'language id does not exist';
|
||||
}
|
||||
|
||||
function template(str: string, args: {[keys: string]: string}): string {
|
||||
|
||||
return str.replace(/{\w+}/g, str => {
|
||||
|
||||
let key = str.substring(1, str.length-1);
|
||||
|
||||
if (key in args)
|
||||
return args[key];
|
||||
|
||||
return key;
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* reads language json
|
||||
* @param id ex: discord.error.noActiveQueue
|
||||
* @returns language value, defaults to `id` parameter
|
||||
*/
|
||||
export function get(id: string, args: {[keys: string]: 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 template(found, args);
|
||||
|
||||
finding = found;
|
||||
|
||||
} else
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function template(str: string, args: {[keys: string]: string}): string {
|
||||
|
||||
return str.replace(/{\w+}/g, str => {
|
||||
|
||||
const key = str.substring(1, str.length-1);
|
||||
|
||||
if (key in args)
|
||||
return args[key];
|
||||
|
||||
return key;
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* reads language json
|
||||
* @param id ex: discord.error.noActiveQueue
|
||||
* @returns language value, defaults to `id` parameter
|
||||
*/
|
||||
export function get(id: string, args: {[keys: string]: string} = {}): string {//discord.error.noActiveQueue
|
||||
|
||||
const keySpl = id.split('.').map(k => k.trim()).filter(k => k);
|
||||
|
||||
let finding = LANG[LANGID];
|
||||
|
||||
for (const key of keySpl) {
|
||||
|
||||
if (key in finding) {
|
||||
|
||||
const found = finding[key];
|
||||
|
||||
if (typeof found === 'string')
|
||||
return template(found, args);
|
||||
|
||||
finding = found;
|
||||
|
||||
} else
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
Reference in New Issue
Block a user