import { createI18n } from "vue-i18n";
import { navigatorLanguage } from "../common/js/utils";
import { Locale } from "vant";
// 引入英文语言包
import enUS from "vant/es/locale/lang/en-US";
// 引入简体中文语言包
import zhCN from "vant/es/locale/lang/zh-CN";
import jaJP from "vant/es/locale/lang/ja-JP";
// 引入俄语语言包
import ruRU from "vant/es/locale/lang/ru-RU";
// 引入法语语言包
import frFR from "vant/es/locale/lang/fr-FR";
// 引入西班牙语语言包
import esES from "vant/es/locale/lang/es-ES";
// 引入葡萄牙语语言包
import ptBR from "vant/es/locale/lang/pt-BR";
// 引入乌克兰语语言包
import ukUA from "vant/es/locale/lang/uk-UA";
const curLang = localStorage.getItem('curLang');
let language = 'en';
// 储存语言，方便中英切换

if (curLang) {
    language = curLang;
    localStorage.setItem('curLang', curLang);
} else {
    language = navigatorLanguage();
    localStorage.setItem('curLang', navigatorLanguage());
}

const i18n = createI18n({
    //多语言实例
    legacy: false,
    globalInjection: true,
    // 默认语言
    locale: localStorage.getItem("curLang")
      ? localStorage.getItem("curLang")
      : "en",
    // 关闭控制台警告
    silentFallbackWarn: true,
    messages: {
      en: require("../assets/language/en.json"), //英文包
      zh: require("../assets/language/zh.json"), //中文包
      ja: require("../assets/language/ja.json"), //日文包
      ru: require("../assets/language/ru.json"), //俄语包
      fr: require("../assets/language/fr.json"), //法语包
      es: require("../assets/language/es.json"), //西班牙语包
      pt: require("../assets/language/pt.json"), //葡萄牙语包
      uk: require("../assets/language/uk.json"), //乌克兰语包
    },
  });

// 判断中英文，切换vant语言包
if (language === 'en') {
    Locale.use('en-US', enUS);
} else if (language === 'ja') {
    Locale.use('ja-JP', jaJP);
} else if (language === 'ru') {
    Locale.use('ru-RU', ruRU);
} else if (language === 'fr') {
    Locale.use('fr-FR', frFR);
} else if (language === 'es') {
    Locale.use('es-ES', esES);
} else if (language === 'pt') {
    Locale.use('pt-BR', ptBR);
} else if (language === 'uk') {
    Locale.use('uk-UA', ukUA);
} else {
    Locale.use('zh-CN', zhCN);
}

// 监听语言切换
i18n.global.onBeforeLanguageSwitch = (lang) => {
  // 同步更新 Vant 语言
  switch (lang) {
    case "en-US":
      Locale.use("en-US", enUS);
      break;
    case "zh-CN":
      Locale.use("zh-CN", zhCN);
      break;
    case "ja-JP":
      Locale.use("ja-JP", jaJP);
      break;
    case "fr-FR":
      Locale.use("fr-FR", frFR);
      break;
    case "ru-RU":
      Locale.use("ru-RU", ruRU);
      break;
    case "es-ES":
      Locale.use("es-ES", esES);
      break;
    case "pt-BR":
      Locale.use("pt-BR", ptBR);
      break;
    case "uk-UA":
      Locale.use("uk-UA", ukUA);
      break;
  }
};

export default i18n;

