Skip to content

Instantly share code, notes, and snippets.

@hmsk
Last active March 1, 2018 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmsk/b6394c2170a0d5878292c3e91208c798 to your computer and use it in GitHub Desktop.
Save hmsk/b6394c2170a0d5878292c3e91208c798 to your computer and use it in GitHub Desktop.
Determine locale with env variable
<!-- pages/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.config.js
module.exports = {
env: {
baseUrl: process.env.BASE_URL || 'http://localhost:3000',
buildLocale: process.env.BUILD_LOCALE || 'en'
},
build: {
vendor: ['vue-i18n']
},
plugins: ['~/plugins/i18n.js'],
generate: {
routes: ['/', '/about']
}
}
// plugins/i18n.js
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import en from '~/locales/en.yaml'
import ja from '~/locales/ja.yaml'
Vue.use(VueI18n)
export default ({ app, store }) => {
app.i18n = new VueI18n({
locale: store.state.locale,
fallbackLocale: 'en',
messages: {
en, ja
}
})
}
export const state = (): State => ({
locale: process.env.buildLocale
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment