一、实现步骤

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. WPF中定时器Timer与DispatcherTimer的用法

    最近的工作项目中需要定时更新UI控件中的数据,这时候第一反应肯定会想到去使用System.Timers.Timer定时更新UI控件,但是程序运行后,会发现程序崩溃了.报的异常为“调用线程无法访问此对象 ...

  2. VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

    正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of ...

  3. Spring Boot(1)——开发你的第一款Spring Boot应用(Edition1)

    Spring Boot(1)——开发你的第一款Spring Boot应用(Edition1) 准备工作: java:java 8 或者 java 9: Spring框架:5.0.8.RELEASE或以 ...

  4. Junit概述

    Junit ->  java unit.也就是说Junit是xunit家族中的一员. unit   <- unit test case,即单元测试用例. Junit  = java uni ...

  5. 使用Elasticsearch 出现的拒绝连接

    pom 文件 spring: elasticsearch: jest: uris: http://192.168.124.142:9201 # data: # elasticsearch: # clu ...

  6. Python 版百度站长平台链接主动推送脚本

    如果自己的网站需要被百度收录,可以在搜索结果中找到,就需要将网站的链接提交给百度.依靠百度的爬虫可能无法检索到网站所有的内容,因此可以主动将链接提交给百度. 在百度的站长平台上介绍了链接提交方法,目前 ...

  7. JMeter 连接 sql server

    1.安装驱动 http://www.microsoft.com/zh-CN/download/details.aspx?id=11774 下载后解压后复制sqljdbc.jar到 “jmeter的安装 ...

  8. python time模块介绍(日期格式化 时间戳)

    import time # 1.time.time() 用于获取当前时间的时间戳, ticks = time.time() print(ticks) # 1545617668.8195682 浮点数 ...

  9. dreamweavercs 和dreamweaver cc的區別

    https://zhidao.baidu.com/question/1541178469432885667.html

  10. Supervisord管理进程实践

    今天凑空研究了下Supervisord,这是一款linux进程管理工具,使用python开发,主要用于在后台维护进程(类似master守护进程),可以实现监控进程的状态.自动重启进程等操作,便于一些服 ...