import cn from './cn';
import en from './en';
import { createI18n } from 'vue-i18n';

// 判断语言--第一次打开根据浏览器语言判断
const getLanguage = () => {
  try {
    if (process.client) {
      let lan = localStorage.getItem('lang');
      if (navigator.language === 'zh-CN' || navigator.language === 'zh-TW') {
        return 'cn';
      } else if (navigator.language == 'en') {
        return 'en';
      } else if (lan) {
        return lan;
      } else {
        return 'en';
      }
    } else {
      // 服务端环境下
      return 'cn';
    }
  } catch (e) {
    return 'cn';
  }
};
const i18n = createI18n({
  legacy: false,
  locale: getLanguage(),
  messages: {
    cn,
    en,
  },
});

export default i18n;
