This repository was archived by the owner on Jul 10, 2019. It is now read-only.
File tree 11 files changed +147
-2
lines changed
11 files changed +147
-2
lines changed Original file line number Diff line number Diff line change @@ -56,7 +56,8 @@ module.exports = {
56
56
'@/plugins/axios.ts' ,
57
57
'@/plugins/constants-inject.ts' ,
58
58
'@/plugins/env-inject.ts' ,
59
- '@/plugins/vue-lazyload.ts'
59
+ '@/plugins/vue-lazyload.ts' ,
60
+ '@/plugins/i18n.ts'
60
61
] ,
61
62
62
63
/*
@@ -134,6 +135,8 @@ module.exports = {
134
135
// ログインの必要のない画面でも middleware が実行されるので注意が必要
135
136
// middleware: 'check-auth',
136
137
138
+ middleware : 'i18n' ,
139
+
137
140
extendRoutes ( routes : any , resolve : any ) : void {
138
141
// https://ja.nuxtjs.org/api/configuration-router/#extendroutes
139
142
if ( routers && routers . length > 0 ) {
Original file line number Diff line number Diff line change 66
66
"nuxt-env" : " ^0.1.0" ,
67
67
"nuxt-property-decorator" : " ^2.1.3" ,
68
68
"ts-node" : " ^8.0.3" ,
69
+ "vue-i18n" : " ^8.10.0" ,
69
70
"vue-lazyload" : " ^1.2.6"
70
71
},
71
72
"devDependencies" : {
Original file line number Diff line number Diff line change
1
+ {
2
+ "text" : {
3
+ "home" : {
4
+ "title" : " これはタイトルです" ,
5
+ "hello" : " こんにちは"
6
+ }
7
+ },
8
+ "error" : {
9
+ "home" : {
10
+ "errorMessage" : " これはエラーメッセージです"
11
+ }
12
+ }
13
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change @@ -62,6 +62,9 @@ section
62
62
p
63
63
nuxt-link( to ='/example/user-agent' )
64
64
| user-agent
65
+ p
66
+ nuxt-link( to ='/example/i18n' )
67
+ | i18n
65
68
</template >
66
69
67
70
<script lang="ts">
Original file line number Diff line number Diff line change 1
1
/**
2
- * 定数をグローバルにセットする
2
+ * @file 定数をグローバルにセットする
3
3
*/
4
4
// https://ja.nuxtjs.org/guide/plugins
5
5
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @file 環境変数をグローバルにセットする
3
+ */
4
+
1
5
import { Context } from '@nuxt/vue-app'
2
6
3
7
export default ( context : Context ) : void => {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -12246,6 +12246,11 @@ vue-hot-reload-api@^2.3.0:
12246
12246
resolved "https://registry.yarnpkg.com/vue-hot-reload-api/-/vue-hot-reload-api-2.3.3.tgz#2756f46cb3258054c5f4723de8ae7e87302a1ccf"
12247
12247
integrity sha512-KmvZVtmM26BQOMK1rwUZsrqxEGeKiYSZGA7SNWE6uExx8UX/cj9hq2MRV/wWC3Cq6AoeDGk57rL9YMFRel/q+g==
12248
12248
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
+
12249
12254
vue-jest@^3.0.4 :
12250
12255
version "3.0.4"
12251
12256
resolved "https://registry.yarnpkg.com/vue-jest/-/vue-jest-3.0.4.tgz#b6a2b0d874968f26fa775ac901903fece531e08b"
You can’t perform that action at this time.
0 commit comments