import Vue from 'vue'
import VueI18n from './vue-i18n/vue-i18n.common'
import {getLocale} from '@/utils/db'
Vue.use(VueI18n)

const i18n = new VueI18n({
    locale: getLocale() || 'zh_CN',
    formatFallbackMessages: true,
    messages: {
        'en_US': require('./languages/en_US.json'),
        'zh_CN': require('./languages/zh_CN.json')
    }
})
window.i18n = i18n
console.log({i18n})
/*
用法
$l('title')         => titleZh
$l(item, 'title')   => item.titleZh
$l(null, 'title')   => titleZh
$l()                => Zh
*/
Vue.prototype.$l = i18n.l = (object, field = '') => {
    let prefix = i18n.locale.split('_')[0]
    let append = prefix.charAt(0).toUpperCase() + prefix.toLowerCase().substr(1)
    if(typeof object == 'string'){
        return object + append
    }
    // 如果object是null或者字符串则返回字段名
    if(!object) return field + append

    return object[field + append] || object[field]
}
/* Vue.filter('$t', Vue.$i18n)
 */
// 重新console.warn来捕获未翻译的内容
/* console.wareOrig = console.warn
console.warn = (...data) => {
    console.log('warn', typeof data)
    console.wareOrig(data)
} */
export default i18n