首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下一步:我做错什么了?

下一步:我做错什么了?
EN

Stack Overflow用户
提问于 2020-12-22 22:58:17
回答 3查看 12.2K关注 0票数 6

我已经开始了一个Vue 3项目(目前不超过TypeScript的样板),并尝试将i18n添加到其中。

据我所知,vue-i18n在Vue 3中不能正常工作;但是vue-i18n-next应该能正常工作。

这是我的main.ts

代码语言:javascript
复制
import { createApp } from "vue";
import "./registerServiceWorker";
import router from "./router";
import store from "./store";
import { createI18n } from 'vue-i18n'
import App from "./App.vue";

//import en from "./locale/en.json"
//import ru from "./locale/ru.json"

const messages = {
    en: {
        message: {
            hello: 'hello world'
        }
    },
    ru: {
        message: {
            hello: 'Таки здравствуйте'
        }
    }
}

const i18n = createI18n({
    locale: 'ru',
/*    messages: {
        en,
        ru
        },*/
    messages,
    fallbackLocale: 'en'
})

const app = createApp(App)
    .use(store)
    .use(router)
    .use(i18n);
    .mount("#app");

这是我的App.vue

代码语言:javascript
复制
<template>
  <div id="nav">
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link>
  </div>
  <div>{{ $t("message.hello") }}</div>
  <router-view />
</template>

不过,我收到一个警告

代码语言:javascript
复制
[intlify] The message format compilation is not supported in this build. Because message compiler isn't included. You need to pre-compilation all message format. So translate function return 'message.hello'.

事实上,我已经找到并安装了@intlify/message编译器--但是对使用它没有任何想法。

我的webpack.config.js是从例子中获得的

代码语言:javascript
复制
const path = require("path");
module.exports = {
  rules: [
    {
      test: /\.(json5?|ya?ml)$/, // target json, json5, yaml and yml files
      type: "javascript/auto",
      loader: "@intlify/vue-i18n-loader",
      include: [
        // Use `Rule.include` to specify the files of locale messages to be pre-compiled
        path.resolve(__dirname, "./src/locale"),
      ],
    },
  ],
};

我的vue.config.js看起来很简单

代码语言:javascript
复制
module.exports = {
  chainWebpack: (config) => {
    config.plugin("html").tap((args) => {
      args[0].template = "./resources/index.html";
      return args;
    });
  },
  configureWebpack: {
    devServer: {
      watchOptions: {
        ignored: ["/node_modules/", "/public/", "**/.#*"],
      },
    },
  },
  parallel: true,
  devServer: {
    disableHostCheck: true,
    public: process.env.DEV_PUBLIC ?? "mlb.ru",
    port: process.env.DEV_PORT ?? 8080,
  },
};

我甚至发现我的消息已经被编译成了包。

也许有人在vue-18n-next上有任何成功,或者Vue 3的其他i18n解决方案?

EN

回答 3

Stack Overflow用户

发布于 2020-12-29 09:59:54

回购和文件系统已经移动:

我尝试过一种非常类似的代码,只要您在vue-i18n@9.0.0-beta.16中,从'vue-i18n'导入{ createI18n }的就可以工作了。

代码语言:javascript
复制
... [code]

import { createI18n } from 'vue-i18n'

const messages = {
  es: {
    message: {
      value: 'Hola Español.',
    },
  },
  en: {
    message: {
      value: 'Hello English.',
    },
  },
}

const i18n = createI18n({
  locale: 'es',
  messages,
})

app
  .use(i18n)
  .mount('#app')

[code] ...
票数 7
EN

Stack Overflow用户

发布于 2020-12-22 23:45:08

与Vue本身一样,i18n包提供了各种版本。和Vue一样,它们有一个有和没有运行时编译器的版本。来自医生们

vue-i18n.esm-bundler.js:包括运行时编译器。如果您使用的是绑定程序,但仍然希望编译区域设置消息(例如,通过内联JavaScript字符串的模板),请使用此方法。

您收到的警告显然是告诉您需要此编译器版本。文档对此稍不清楚,但您需要将导入指向包的运行时编译器版本,如下所示:

代码语言:javascript
复制
import { createI18n } from "vue-i18n/dist/vue-i18n.esm-bundler.js";
票数 5
EN

Stack Overflow用户

发布于 2021-07-19 22:00:24

我在外部文件中使用i18n (i18n.js),希望它能帮助您。

i18n.js

代码语言:javascript
复制
import { createI18n } from 'vue-i18n'
const messages = {
    en: {
        message: {
            hello: 'hello world'
        }
    },
    ru: {
        message: {
            hello: 'Таки здравствуйте'
        }
    }
}
const i18n = createI18n({
    locale: 'en',
    messages
})
export default i18n

main.js

代码语言:javascript
复制
import { createApp } from 'vue'
import App from './App.vue'
import i18n from "@/i18n"
const app = createApp(App)
app.use(i18n)
app.mount('#app')

App.vue

代码语言:javascript
复制
<template>
    <span><div>{{ $t("message.hello") }}</div></span>
</template>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65416926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档