vue-i18n-js-loader
With vue-i18n-js-loader loader, you can write any javascript code in the i18n custom block!
This project is based on @intlify/vue-i18n-loader.
And this project is for Vue2
Before using this loader, you have to install Vue2, vue-loader and vue-i18n first.
Installation
$ npm install --save-dev vue-i18n-js-loader
Webpack config
// in webpack.config.jsconst vueI18nJSPlugin = ;moduleexports =plugins:;module:rules:resourceQuery: /blockType=i18n/type: 'javascript/auto'loader: 'vue-i18n-js-loader'
Vue-loader config
// in vue.config.js{configmoduletype'javascript/auto';config;}
Usage
Basic usage
You can use javascript in the i18n custom block now! You have to export a javascript plain object representing the i18n messages by default
.
You can still use languages supported by @intlify/vue-i18n-loader
{{ $t('hello') }}export default {en: {hello: 'Hello',},zhHans: {hello: '你好',},}
Import from other SFCs
You can also import an i18n object from another SFC file.
But you have to take care of the circular dependency problem.
<!-- a.vue -->export default {fromA: 'From a.vue',}{"fromA": "来自 a.vue"}
<!-- b.vue -->{{ $t('combined') }}import { i18n as messagesFromA } from './a.vue';export default {en: {combined: 'Hello, ' + messagesFromA.en.fromA,},zhHans: {combined: '你好, ' + messagesFromA.zhHans.fromA,}}
What can be accessed in the i18n block
You can write any js code you want, but you CANNOT access the vue component! Because the code in the i18n block is executed before the vue component creation.
import { i18n } from './another.vue';import { upperCase } from 'lodash';export default {en: {baz: upperCase(i18n.en.foo),// bar: this.something, // illegal},// ...}
Use source file
like this.