import { boot } from "quasar/wrappers";
import { createI18n } from "vue-i18n";
import { Cookies } from "quasar";

import messages from "src/i18n";

let language = Cookies.get("language");

// 如果 cookie 中没有语言信息,则使用服务器给的语言
if (!language) {
  language = (<any>window).media_control_client_language;
}

// 如果服务器也没有给语言, 默认中文
if (!language) {
  language = "zh-CN";
}

if (language != "zh-CN" && language != "en-US") {
  language = "zh-CN";
}

Cookies.set("language", language, {
  expires: 365,
});

(<any>window).currnet_use_language = language;

const i18n = createI18n({
  legacy: false,
  locale: language,
  messages,
});

export default boot(({ app }) => {
  // Set i18n instance on app
  app.use(i18n);
});

export { i18n };
