Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit 713fa8c

Browse files
authored
Merge pull request #9 from hisasann/feature/add-i18n
Feature/add i18n
2 parents 3c49c73 + e88891b commit 713fa8c

File tree

11 files changed

+147
-2
lines changed

11 files changed

+147
-2
lines changed

nuxt.config.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ module.exports = {
5656
'@/plugins/axios.ts',
5757
'@/plugins/constants-inject.ts',
5858
'@/plugins/env-inject.ts',
59-
'@/plugins/vue-lazyload.ts'
59+
'@/plugins/vue-lazyload.ts',
60+
'@/plugins/i18n.ts'
6061
],
6162

6263
/*
@@ -134,6 +135,8 @@ module.exports = {
134135
// ログインの必要のない画面でも middleware が実行されるので注意が必要
135136
// middleware: 'check-auth',
136137

138+
middleware: 'i18n',
139+
137140
extendRoutes(routes: any, resolve: any): void {
138141
// https://ja.nuxtjs.org/api/configuration-router/#extendroutes
139142
if (routers && routers.length > 0) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"nuxt-env": "^0.1.0",
6767
"nuxt-property-decorator": "^2.1.3",
6868
"ts-node": "^8.0.3",
69+
"vue-i18n": "^8.10.0",
6970
"vue-lazyload": "^1.2.6"
7071
},
7172
"devDependencies": {

src/locales/ja.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"text": {
3+
"home": {
4+
"title": "これはタイトルです",
5+
"hello": "こんにちは"
6+
}
7+
},
8+
"error": {
9+
"home": {
10+
"errorMessage": "これはエラーメッセージです"
11+
}
12+
}
13+
}

src/middleware/i18n.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* SSR でロケールを決定するためのミドルウェア
3+
* @param isHMR
4+
* @param app
5+
* @param store
6+
* @param route
7+
* @param params
8+
* @param error
9+
* @param redirect
10+
*/
11+
export default function({
12+
isHMR,
13+
app,
14+
store,
15+
route,
16+
params,
17+
error,
18+
redirect
19+
}): void {
20+
const defaultLocale = app.i18n.fallbackLocale
21+
// If middleware is called from hot module replacement, ignore it
22+
if (isHMR) {
23+
return
24+
}
25+
26+
// Get locale from params
27+
const locale = params.lang || defaultLocale
28+
if (store.state.i18n.locales.indexOf(locale) === -1) {
29+
return error({ message: 'This page could not be found.', statusCode: 404 })
30+
}
31+
// Set locale
32+
store.commit('i18n/SET_LANG', locale)
33+
app.i18n.locale = store.state.locale
34+
// If route is /<defaultLocale>/... -> redirect to /...
35+
// if (locale === defaultLocale && route.fullPath.indexOf('/' + defaultLocale) === 0) {
36+
// const toReplace = '^/' + defaultLocale + (route.fullPath.indexOf('/' + defaultLocale + '/') === 0 ? '/' : '')
37+
// const re = new RegExp(toReplace)
38+
// return redirect(
39+
// route.fullPath.replace(re, '/')
40+
// )
41+
// }
42+
}

src/pages/example/i18n.vue

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template lang="pug">
2+
section
3+
h1.title
4+
| i18n
5+
p {{ $t('text.home.hello') }}
6+
p {{ $t('error.home.errorMessage') }}
7+
</template>
8+
9+
<script lang="ts">
10+
import { Component, Vue } from 'nuxt-property-decorator'
11+
12+
@Component
13+
export default class extends Vue {
14+
public head() {
15+
return {
16+
title: this.$t('text.home.title')
17+
}
18+
}
19+
}
20+
</script>

src/pages/example/index.vue

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ section
6262
p
6363
nuxt-link(to='/example/user-agent')
6464
| user-agent
65+
p
66+
nuxt-link(to='/example/i18n')
67+
| i18n
6568
</template>
6669

6770
<script lang="ts">

src/plugins/constants-inject.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* 定数をグローバルにセットする
2+
* @file 定数をグローバルにセットする
33
*/
44
// https://ja.nuxtjs.org/guide/plugins
55

src/plugins/env-inject.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @file 環境変数をグローバルにセットする
3+
*/
4+
15
import { Context } from '@nuxt/vue-app'
26

37
export default (context: Context): void => {

src/plugins/i18n.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @file i18n をグローバルにセットする
3+
*/
4+
5+
import Vue from 'vue'
6+
import VueI18n from 'vue-i18n'
7+
8+
Vue.use(VueI18n)
9+
10+
export default ({ app, store }): void => {
11+
// Set i18n instance on app
12+
// This way we can use it in middleware and pages asyncData/fetch
13+
app.i18n = new VueI18n({
14+
locale: store.state.locale,
15+
fallbackLocale: 'ja',
16+
messages: {
17+
ja: require('~/locales/ja.json')
18+
}
19+
})
20+
21+
// app.i18n.path = (link) => {
22+
// if (app.i18n.locale === app.i18n.fallbackLocale) {
23+
// return `/${link}`
24+
// }
25+
//
26+
// return `/${app.i18n.locale}/${link}`
27+
// }
28+
}

src/store/i18n.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* store 用インターフェイス
3+
*/
4+
export interface StateInterface {
5+
locales: string[]
6+
locale: string
7+
}
8+
9+
/**
10+
* state
11+
*/
12+
export const state = (): StateInterface => ({
13+
locales: ['ja'],
14+
locale: 'ja'
15+
})
16+
17+
/**
18+
* mutations
19+
*/
20+
export const mutations = {
21+
SET_LANG(state, locale): void {
22+
if (state.locales.indexOf(locale) !== -1) {
23+
state.locale = locale
24+
}
25+
}
26+
}

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -12246,6 +12246,11 @@ vue-hot-reload-api@^2.3.0:
1224612246
resolved "https://registry.yarnpkg.com/vue-hot-reload-api/-/vue-hot-reload-api-2.3.3.tgz#2756f46cb3258054c5f4723de8ae7e87302a1ccf"
1224712247
integrity sha512-KmvZVtmM26BQOMK1rwUZsrqxEGeKiYSZGA7SNWE6uExx8UX/cj9hq2MRV/wWC3Cq6AoeDGk57rL9YMFRel/q+g==
1224812248

12249+
vue-i18n@^8.10.0:
12250+
version "8.10.0"
12251+
resolved "https://registry.yarnpkg.com/vue-i18n/-/vue-i18n-8.10.0.tgz#e95f215b5548bedf5610c14506acb3438717b6bd"
12252+
integrity sha512-n2A9Q5dbwk3q4r6cOdT5jJaMb/mV4JtkNmgSiUNtoDp+N00bQHzpALM2XRyNzu7WZSHyi10/wBrNKl0unNKpVg==
12253+
1224912254
vue-jest@^3.0.4:
1225012255
version "3.0.4"
1225112256
resolved "https://registry.yarnpkg.com/vue-jest/-/vue-jest-3.0.4.tgz#b6a2b0d874968f26fa775ac901903fece531e08b"

0 commit comments

Comments
 (0)