47 lines
974 B
JavaScript
47 lines
974 B
JavaScript
// const fs = require('fs')
|
|
// const path = require('path')
|
|
|
|
class MsgManager {
|
|
constructor () {
|
|
this.errs = {}
|
|
this.defLang = 'zh'
|
|
let err = require(`./language/zh`)
|
|
this.errs['zh'] = err
|
|
}
|
|
|
|
checkLang (lang = '') {
|
|
let lng = this.defLang
|
|
if (typeof lang === 'string' && lang.trim().length > 0) {
|
|
let l = lang.split(',')
|
|
if (l.length > 0) {
|
|
let tmp = l[0].substring(0, 2)
|
|
if (tmp.trim().length > 0) lng = tmp
|
|
}
|
|
}
|
|
|
|
if (!(lng in this.errs)) {
|
|
try {
|
|
let errs = require(`./language/${lng}`)
|
|
this.errs[lng] = errs
|
|
} catch (err) {
|
|
lng = this.defLang
|
|
}
|
|
}
|
|
return lng
|
|
}
|
|
|
|
getMsg (code, lang = '') {
|
|
let lng = this.checkLang(lang)
|
|
|
|
return this.errs[lng][code] || 'errorCode not found'
|
|
}
|
|
|
|
getMailTemplate (type, lang = '') {
|
|
let lng = this.checkLang(lang)
|
|
|
|
return this.errs[lng]['mail'][type] || {}
|
|
}
|
|
}
|
|
|
|
module.exports = new MsgManager()
|