如何在 Vue Web 组件中使用 vue-i18n?
How can I use vue-i18n in a Vue web component?
我正在使用 vue-cli 3 和 --target wc
选项创建一个 Vue 网络组件。我还需要组件使用 vue-i18n 插件,这需要像这样将一些选项传递给主 Vue 实例:
new Vue({
i18n: new VueI18n({ ... ])
...
})
在常规 Vue 应用程序中,这很好,因为我控制主 Vue 实例的构造。但是,当作为 Web 组件构建时,主要 Vue 对象的构建是由 vue-web-component-wrapper 生成的,我没有看到任何明显的影响它的方法。
如何在我的网络组件中使用 vue-18n 插件?
i18n 插件需要在 "entry" 组件(即构成 Web 组件基础的组件)而不是根组件上初始化。
但即便如此,所有消息都在入口组件 上定义也很重要。在子组件上定义消息,无论是直接在组件中还是使用 <i18n>
元素,都会有效地覆盖入口组件中的 vue-i18n 配置,并导致该子组件使用默认语言 en-US。
这是一个简单的工作示例:
入门组件
<template>
<div>
<p>{{ $t('hello') }}</p>
<child-component />
</div>
</template>
<script>
import ChildComponent from "./child.vue"
import Vue from "vue"
import VueI18n from "vue-i18n"
Vue.use(VueI18n)
export default {
name: "App",
i18n: new VueI18n({
locale: 'ja',
messages: {
en: {
hello: "hello in en",
test: "test in en"
},
ja: {
hello: "hello in ja",
test: "test in ja"
},
}
}),
components: { ChildComponent }
};
</script>
子组件
<template>
<div>{{ $t("test") }}</div>
</template>
<script>
export default {
name: "child-component"
};
</script>
@tctimmeh 解决方案的问题是 i18n plugin
需要实际传递到 new Vue
构造函数中,但是在使用 web components wrapper
时我们无法访问 new Vue
因为它在包装器库中
因此,幸运的是,感谢这个 PR https://github.com/kazupon/vue-i18n/pull/420/commits/13ed0488caf70ab454ffd5cb2affa36ec31c4c50
我们可以在 VueI18n
实例化之前扩展 Vue 对象
import Vue from 'vue'
import wrap from '@vue/web-component-wrapper'
import VueI18n from 'vue-i18n';
const Component = {
// any component options
}
const i18n = new VueI18n({
locale: 'en',
});
const VueExtended = Vue.extend({ i18n });
const CustomElement = wrap(VueExtended, Component)
window.customElements.define('my-element', CustomElement)
我正在使用 vue-cli 3 和 --target wc
选项创建一个 Vue 网络组件。我还需要组件使用 vue-i18n 插件,这需要像这样将一些选项传递给主 Vue 实例:
new Vue({
i18n: new VueI18n({ ... ])
...
})
在常规 Vue 应用程序中,这很好,因为我控制主 Vue 实例的构造。但是,当作为 Web 组件构建时,主要 Vue 对象的构建是由 vue-web-component-wrapper 生成的,我没有看到任何明显的影响它的方法。
如何在我的网络组件中使用 vue-18n 插件?
i18n 插件需要在 "entry" 组件(即构成 Web 组件基础的组件)而不是根组件上初始化。
但即便如此,所有消息都在入口组件 上定义也很重要。在子组件上定义消息,无论是直接在组件中还是使用 <i18n>
元素,都会有效地覆盖入口组件中的 vue-i18n 配置,并导致该子组件使用默认语言 en-US。
这是一个简单的工作示例:
入门组件
<template>
<div>
<p>{{ $t('hello') }}</p>
<child-component />
</div>
</template>
<script>
import ChildComponent from "./child.vue"
import Vue from "vue"
import VueI18n from "vue-i18n"
Vue.use(VueI18n)
export default {
name: "App",
i18n: new VueI18n({
locale: 'ja',
messages: {
en: {
hello: "hello in en",
test: "test in en"
},
ja: {
hello: "hello in ja",
test: "test in ja"
},
}
}),
components: { ChildComponent }
};
</script>
子组件
<template>
<div>{{ $t("test") }}</div>
</template>
<script>
export default {
name: "child-component"
};
</script>
@tctimmeh 解决方案的问题是 i18n plugin
需要实际传递到 new Vue
构造函数中,但是在使用 web components wrapper
时我们无法访问 new Vue
因为它在包装器库中
因此,幸运的是,感谢这个 PR https://github.com/kazupon/vue-i18n/pull/420/commits/13ed0488caf70ab454ffd5cb2affa36ec31c4c50
我们可以在 VueI18n
import Vue from 'vue'
import wrap from '@vue/web-component-wrapper'
import VueI18n from 'vue-i18n';
const Component = {
// any component options
}
const i18n = new VueI18n({
locale: 'en',
});
const VueExtended = Vue.extend({ i18n });
const CustomElement = wrap(VueExtended, Component)
window.customElements.define('my-element', CustomElement)