4

Vue中使用i18n进行配置语言全球化

 3 years ago
source link: https://surest.cn/archives/171/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Vue中使用i18n进行配置语言全球化

npm install vue-i18n

我这里创建了utils文件夹

> mkdir -p utils
> vi i18n.js

import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)

export default new VueI18n({
    locale: 'en', 
    messages: {
        'en': require('./lang/en.json')
    }
})

> mkdir lang
> vi en.json

{
    "document.account": "Account/Email",
    "document.content_empty": "Empty",
    "document.password": "Password",
    "document.tips.account": "Please input Account/Email",
    "document.tips.login_fail": "Wrong account or password",
    "document.tips.login_success": "Login successful",
    "document.tips.password": "Please enter the password",
    "document.tips.tip": "Tip",
    "document.directory": "Directory"
}

进入main.js

import VueI18n from 'vue-i18n';
import i18n from './utils/i18n';
Vue.use(VueI18n);

new Vue({
    el: '#app',
    i18n,
    router,
    store
});


# 方法中使用
this.$t('document.tips.login_success')

# template中使用
<el-form-item :label="$t('document.password')" prop="password" :rules="{ required: true, message: $t('document.tips.password'), trigger: 'blur' }">
    <el-input type="password" v-model="form.password"></el-input>
</el-form-item>

本文由 邓尘锋 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: May 8, 2021 at 11:19 am


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK