This repository was archived by the owner on Jul 10, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 11 files changed +147
-2
lines changed Expand file tree Collapse file tree 11 files changed +147
-2
lines changed Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff line change 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" : {
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
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">
Original file line number Diff line number Diff line change 11/**
2- * 定数をグローバルにセットする
2+ * @file 定数をグローバルにセットする
33 */
44// https://ja.nuxtjs.org/guide/plugins
55
Original file line number Diff line number Diff line change 1+ /**
2+ * @file 環境変数をグローバルにセットする
3+ */
4+
15import { Context } from '@nuxt/vue-app'
26
37export 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+ }
You can’t perform that action at this time.
0 commit comments