简体   繁体   English

如何在Vue文件中导入i18n

[英]How to import i18n in Vue file

I am using i18n library in Vue.js for language translation.我在 Vue.js 中使用 i18n 库进行语言翻译。 I want to to import in it my script and store a value in data, but I have trouble with importing it.我想在其中导入我的脚本并在数据中存储一个值,但是我在导入它时遇到了麻烦。 How should I import it?我应该如何导入它? I tried with import $t from "./i18n";我尝试使用import $t from "./i18n"; but that just returns this error: Module not found: Error: Can't resolve './i18n'但这只会返回此错误: Module not found: Error: Can't resolve './i18n'

here is my code:这是我的代码:

script>
import Header from "../components/Header";
import $ from "jquery";
import $t from "./i18n";

export default {
  name: "GroupPermissions",
  components: {
    Header
  },
  data() {
    return {
      showAddGroupDialog: false,
      updatePermissionDialog: false,
      itemsToBeDeleted: [],
      permissions: $t("groupPermissions.table.permissions")
    };

and my main.js:和我的 main.js:

import Vue from "vue";
import App from "./App.vue";
import router from "./router";

import "bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";

import VueMaterial from "vue-material";
import "vue-material/dist/vue-material.min.css";
import "vue-material/dist/theme/default.css";
import "vue-multiselect/dist/vue-multiselect.min.css";
import $ from "jquery";
import Multiselect from "vue-multiselect";
import i18n from "./i18n";
import "@/plugins/echarts";
import Sticky from 'vue-sticky-directive'

Vue.component("multiselect", Multiselect);
Vue.use(VueMaterial);
Vue.use(Sticky);

Vue.config.productionTip = false;

new Vue({
  router,
  $,
  i18n,
  render: h => h(App)
}).$mount("#app");

Imre showed how to install the vue-i18n globally. Imre 展示了如何全局安装vue-i18n I'll just add that you can also import an instance of i18n and use it in any javascript file, not only within Vue components.我只是补充一点,您还可以导入i18n的实例并在任何 javascript 文件中使用它,而不仅仅是在 Vue 组件中。

If you export i18n like this如果你像这样导出i18n

import VueI18n from 'vue-i18n';

export const i18n = new VueI18n({
    locale: 'en',
    messages: { 'en': { title: 'Title' }},
});

you can then use it in Vue components, though using this.$t() is preferable.然后你可以在 Vue 组件中使用它,尽管using this.$t()更好。

<script>
import Header from "../components/Header";
import $ from "jquery";
import { i18n } from "./i18n";

export default {
  name: "GroupPermissions",
  components: {
    Header
  },
  data() {
    return {
      showAddGroupDialog: false,
      updatePermissionDialog: false,
      itemsToBeDeleted: [],
      permissions: i18n.t("groupPermissions.table.permissions")
    };
</script>

But more relevantly, you can use this anywhere else.但更相关的是,您可以在其他任何地方使用它。

//some-function.js
import { i18n } from "./i18n";

function someFunction() {
  console.log(i18n.t(...));
}

If you'd like use vue-i18n , in your main.js you should import vue-i18n library first and create an i18n variable like this:如果你想使用vue-i18n ,在你的 main.js 中你应该首先导入vue-i18n库并创建一个 i18n 变量,如下所示:

import VueI18n from 'vue-i18n';

const i18n = new VueI18n({
    locale: 'en',
    messages: { 'en': { title: 'Title' }},
});

and pass the i18n var to your Vue in main.js as you did it:并在 main.js 中将i18n var 传递给您的 Vue:

new Vue({
  router,
  $,
  i18n,
  render: h => h(App)
}).$mount("#app");

After this, in your component you will have a this.$t('title') method that will return with 'Title' value.在此之后,在您的组件中,您将拥有一个this.$t('title')方法,该方法将返回 'Title' 值。

This link may help for you: http://kazupon.github.io/vue-i18n/started.html#javascript此链接可能对您有所帮助: http://kazupon.github.io/vue-i18n/started.html#javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM