nuxt.js实战之用vue-i18n实现多语言
一、实现步骤
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实现多语言的更多相关文章
- 【Vue.js实战案例】- Vue.js递归组件实现组织架构树和选人功能
大家好!先上图看看本次案例的整体效果. 浪奔,浪流,万里涛涛江水永不休.如果在jq时代来实这个功能简直有些噩梦了,但是自从前端思想发展到现在的以MVVM为主流的大背景下,来实现一个这样繁杂的功能简直不 ...
- nuxt.js实战踩坑记录
读万卷书不如行万里路,必须实践出真理! 看官方文档安装项目vue init nuxt-community/starter-template <project-name>注意:这是新手项目不 ...
- nuxt.js实战之引入jquery
head: { title: 'nuxt', meta: [ { charset: 'utf-8' }, { name: 'viewport', content: 'width=device-widt ...
- nuxt.js实战之移动端rem
nuxt.js的项目由于是服务端渲染,通过js动态调整不同尺寸设备的根字体这种rem方案存在一种缺陷.由于设置字体的代码不能做服务端渲染,导致服务端返回的代码一开始没有相应的跟字体,直到与前端代码进行 ...
- nuxt.js实战之开发环境配置
一.创建项目 1.使用如下命令生成项目 vue init nuxt-community/starter-template testPro --testPro为项目名称 2.进入到项目根目录下,使用np ...
- Vue.js实战:初识Vue.js
一.Vue.js是什么 简单小巧的核心,渐进式技术栈,足以应付任何规模的应用. 简单小巧指的是Vue.js 压缩后大小仅有17KB 所谓渐进式(Progressive)就是你一步一步,有阶段性地来使用 ...
- nuxt.js实战之window和document对象的使用
在开发nuxt项目的时候,我们难免会使用到document来获取dom元素.如果直接在文件中使用就会报错.这是因为document是浏览器端的东西服务端并没有. 解决方法: 我们只需要在使用的地方通过 ...
- 🏃♀️点亮你的Vue技术栈,万字Nuxt.js实践笔记来了~
前言 作为一位 Vuer(vue开发者),如果还不会这个框架,那么你的 Vue 技术栈还没被点亮. Nuxt.js 是什么 Nuxt.js 官方介绍: Nuxt.js 是一个基于 Vue.js 的通用 ...
- Nuxt.js logoVue.js 后端渲染开源库 Nuxt.js
Nuxt.js 是一个通过 Vue 用于服务端渲染的简单框架,灵感来自 Next.js. 目前尚处于开发阶段,1.0 版本即将发布 1 分钟视频演示 Nuxt 基于 ES2015,这使得代码有着更愉快 ...
随机推荐
- text-decoration、text-decoration-color、text-decoration-line、text-decoration-style属性
text-decoration:over-line 定义上划线 text-decoration:line-through 定义删除线 text-decoration:underline 定义下划 ...
- [转帖]cmd批处理常用符号详解
cmd批处理常用符号详解 https://www.jb51.net/article/32866.htm 很多符号 还是不清楚的.. 批处理能够极大的提高 工作效率 需要加强深入学习. 1.@一般在 ...
- WSL Windows subsytem linux 的简单学习与使用
1. win10 1709 以上的版本应该都增加上了 ctrl +r 运行 winver 查看版本 2. 添加删除程序 增加 wsl 增加一个功能 3. 打开cmd 输入 bash 即可 4. 可以将 ...
- C#如何调用C++的dll
背景 一个项目,算法部分使用C++的openCV库编写图像处理程序,编译成dll,用户界面采用C#编写,去调用该dll暴露的接口. C#编写的是托管代码,编译生成微软中间语言,而普通C++代码则编译 ...
- Vue 获得所选中目标的状态(checked)以及对应目标的数据,并进行相应的操作
一.我们现在要拿取购物车中选中商品的状态和该商品的所有数据或者id <ul v-if="shopList.list.length>0"> <li class ...
- Selenium简单回顾
一.Selenium介绍 1.Selenium(浏览器自动化测试框架): Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的 ...
- python学习笔记(11)--数据组织的维度
数据的操作周期 存储 -- 表示 -- 操作 一维数据表示 如果数据有序,可以使用列表[]:如果数据没有顺序,可以使用集合{} 一维数组存储 存储方式一:空格分隔 ,使用一个或多个空格分隔进行分隔, ...
- docker学习笔记一
知识点: 1)docker简介 2)docker安装,仓库配置 3)docker仓库镜像拉取,导出,导入,删除 4)docker容器操作,容器的创建,删除,运行,停止,日志查看等. 5) docke ...
- 莫烦scikit-learn学习自修第六天【特征值矩阵标准化】
1.代码实战 #!/usr/bin/env python #!_*_coding:UTF-8 _*_ import numpy as np from sklearn import preprocess ...
- ADO.NET工具类(三)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...