一、实现步骤

1、安装vue-i18n并且创建store.js(vuex状态管理)文件

2、创建一个(middleware)中间件,用来管理不同的语言

3、创建不同语言的json文件作为语言包(例如: ~locales/en.json)

4、在pages文件夹下创建文件,并进行翻译

二、详细步骤

1、安装vue-i18n

npm install vue-i18n --save

2、在nuxt应用程序中引入vue-i18n

新建文件  ~plugins/i18n.js,内容如下:

import Vue from 'vue';
import VueI18n from 'vue-i18n';
Vue.use(VueI18n);
export default ({ app, store }) => {
// Set i18n instance on app
// This way we can use it in middleware and pages asyncData/fetch
app.i18n = new VueI18n({
locale: store.state.locale,
fallbackLocale: 'en',
messages: {
'en': require('~/locales/en.json'),
'fr': require('~/locales/fr.json')
}
});
app.i18n.path = (link) => {
if (app.i18n.locale === app.i18n.fallbackLocale) {
return `/${link}`;
}
return `/${app.i18n.locale}/${link}`;
}
}

3、使用vuex保存当前语言状态

新建文件~store/index.js,内容如下:

export const state = () => ({
locales: [‘en’, ‘fr’],
locale: ‘en’
})
export const mutations = {
SET_LANG(state, locale) {
if (state.locales.indexOf(locale) !== -1) {
state.locale = locale
}
}
}

4、在middleware下新建i18n.js用来控制语言切换

/*
* 1. sets i18n.locale and state.locale if possible
* 2. redirects if not with locale
*/
export default function ({
isHMR, app, store, route, params, error, redirect
}) {
if (isHMR) { // ignore if called from hot module replacement
return;
} // if url does not have language, redirect to english
else if (!params.lang) {
return redirect('/en'+route.fullPath);
}
// based on directory structure _lang/xxxx, en/about has params.lang as "en"
const locale = params.lang || 'en';
store.commit('SET_LANG', locale); // set store
app.i18n.locale = store.state.locale;
}

5、修改nuxt.config.js文件配置如下:

module.exports = {
loading: { color: '#3B8070' },
build: { // customize webpack build
vendor: ['vue-i18n'] // webpack vue-i18n.bundle.js
},
router: { // customize nuxt.js router (vue-router).
middleware: 'i18n' // middleware all pages of the application
},
plugins: ['~/plugins/i18n.js'], // webpack plugin
generate: {
routes: ['/', '/about', '/fr', '/fr/about']
}
}

6、创建本地语言包

例如:(~locales/en.js) 

{
"links": {
"home": "Home",
...
},
"home": {
"title": "Welcome",
"introduction": "This is an introduction in English."
},
"about": {
"title": "About",
"introduction": "..."
}
}

(~locales/fr.js)

{
"links": {
"home": "Accueil",
...
},
"home": {
"title": "Bienvenue",
"introduction": "Ceci est un texte d'introduction en Français."
},
"about": {
"title": "À propos",
"introduction": "..."
}
}

7、创建页面,并添加翻译

~pages/_lang/index.vue

~pages/_lang/about.vue

<template>
<div class="Content">
<div class="container">
<h1 class="Content__Title">{{ $t('about.title') }}</h1>
<p>{{ $t('about.introduction') }}</p>
</div>
</div>
</template>
<script>
export default {
head() {
return { title: this.$t('about.title') }
}
}
</script>  

nuxt.js实战之用vue-i18n实现多语言的更多相关文章

  1. 【Vue.js实战案例】- Vue.js递归组件实现组织架构树和选人功能

    大家好!先上图看看本次案例的整体效果. 浪奔,浪流,万里涛涛江水永不休.如果在jq时代来实这个功能简直有些噩梦了,但是自从前端思想发展到现在的以MVVM为主流的大背景下,来实现一个这样繁杂的功能简直不 ...

  2. nuxt.js实战踩坑记录

    读万卷书不如行万里路,必须实践出真理! 看官方文档安装项目vue init nuxt-community/starter-template <project-name>注意:这是新手项目不 ...

  3. nuxt.js实战之引入jquery

    head: { title: 'nuxt', meta: [ { charset: 'utf-8' }, { name: 'viewport', content: 'width=device-widt ...

  4. nuxt.js实战之移动端rem

    nuxt.js的项目由于是服务端渲染,通过js动态调整不同尺寸设备的根字体这种rem方案存在一种缺陷.由于设置字体的代码不能做服务端渲染,导致服务端返回的代码一开始没有相应的跟字体,直到与前端代码进行 ...

  5. nuxt.js实战之开发环境配置

    一.创建项目 1.使用如下命令生成项目 vue init nuxt-community/starter-template testPro --testPro为项目名称 2.进入到项目根目录下,使用np ...

  6. Vue.js实战:初识Vue.js

    一.Vue.js是什么 简单小巧的核心,渐进式技术栈,足以应付任何规模的应用. 简单小巧指的是Vue.js 压缩后大小仅有17KB 所谓渐进式(Progressive)就是你一步一步,有阶段性地来使用 ...

  7. nuxt.js实战之window和document对象的使用

    在开发nuxt项目的时候,我们难免会使用到document来获取dom元素.如果直接在文件中使用就会报错.这是因为document是浏览器端的东西服务端并没有. 解决方法: 我们只需要在使用的地方通过 ...

  8. 🏃‍♀️点亮你的Vue技术栈,万字Nuxt.js实践笔记来了~

    前言 作为一位 Vuer(vue开发者),如果还不会这个框架,那么你的 Vue 技术栈还没被点亮. Nuxt.js 是什么 Nuxt.js 官方介绍: Nuxt.js 是一个基于 Vue.js 的通用 ...

  9. Nuxt.js logoVue.js 后端渲染开源库 Nuxt.js

    Nuxt.js 是一个通过 Vue 用于服务端渲染的简单框架,灵感来自 Next.js. 目前尚处于开发阶段,1.0 版本即将发布 1 分钟视频演示 Nuxt 基于 ES2015,这使得代码有着更愉快 ...

随机推荐

  1. 如何让pl/sql developer记住密码,实现快速登录

    前两天,有同事使用plsql的时候,切换数据库的时候需要不断的重复输入密码,这样太麻烦了. 下面,我这里说下如何的实现plsql不需要输入密码就能快速登录的方法: 1.一开始登录,首先像往常那样输入密 ...

  2. macOS & USB stick

    macOS & USB stick why macOS can only read USB stick, can not write files to USB stick macos 无法写文 ...

  3. TensorFlow总结

    第一 基础 1. 定义变量 #定义维度为[2,3], 平均值为·1, 标准差为1,类型为float32,名称为w1的服从正态分布的变量 w1 = tf.Variable(tf.random_norma ...

  4. webpack安裝和卸載

    webpack安裝和卸載 安裝: 先裝好node和npm: 安裝package.json:進入到根目錄,運行npm init 新建全局webpack:cd退到全局目錄,運行npm install -g ...

  5. html5 表單輸入類型

    輸入類型有:email,url,number,range,Date pickers(工作機制是什麼),search, 有相關類型的輸入域,會對域進行驗證. 不同的瀏覽器並不一定都支持所有的輸入類型.

  6. LOADING Redis is loading the dataset in memory Redis javaAPI实例

    今天在实现Redis客户端API操作Jedis的八种调用方式详解中,遇到了LOADING Redis is loading the dataset in memory错误,经过多番查找资料,找到了解决 ...

  7. b2b

    sku(Stock Keeping Unit)(件)最小库存量单位. spu(一款) 一种详细的规格参数有时候跟spu,规格参数加上颜色+尺寸什么的唯一确定了一个手机,对应的就是sku spu:一款产 ...

  8. Multi-Targeting and Porting a .NET Library to .NET Core 2.0

    Creating a new .NET Standard Project The first step for moving this library is to create a new .NET ...

  9. Nintex Forms Drop-Down "z-index"

    Now we’ve got the issue, that if we are working with a “Person-Column”, the drop-down where you can ...

  10. codeforces263B

    Squares CodeForces - 263B Vasya has found a piece of paper with a coordinate system written on it. T ...