diff --git a/examples/i18n/layouts/default.vue b/examples/i18n/layouts/default.vue index 1f22aa7a30..09cdd6ddea 100644 --- a/examples/i18n/layouts/default.vue +++ b/examples/i18n/layouts/default.vue @@ -7,13 +7,13 @@ {{ $t('links.home') }} - + {{ $t('links.about') }} - + {{ $t('links.french') }} - + {{ $t('links.english') }} @@ -27,15 +27,19 @@ export default { methods: { path (url) { - return (this.$store.state.lang.lang === 'en' ? url : '/' + this.$store.state.lang.lang + url) + return (this.$i18n.locale === 'en' ? url : '/' + this.$i18n.locale + url) } } } diff --git a/examples/i18n/layouts/error.vue b/examples/i18n/layouts/error.vue deleted file mode 100644 index 32e5417d15..0000000000 --- a/examples/i18n/layouts/error.vue +++ /dev/null @@ -1,59 +0,0 @@ - - - - - diff --git a/examples/i18n/static/locales/en.json b/examples/i18n/locales/en.json similarity index 100% rename from examples/i18n/static/locales/en.json rename to examples/i18n/locales/en.json diff --git a/examples/i18n/static/locales/fr.json b/examples/i18n/locales/fr.json similarity index 100% rename from examples/i18n/static/locales/fr.json rename to examples/i18n/locales/fr.json diff --git a/examples/i18n/middleware/i18n.js b/examples/i18n/middleware/i18n.js index 8642cafa4b..10e437bcdb 100644 --- a/examples/i18n/middleware/i18n.js +++ b/examples/i18n/middleware/i18n.js @@ -1,8 +1,13 @@ -export default async function ({ store, params, error }) { - const lang = params.lang || 'en' - if (!store.state.lang.locales.includes(lang)) { - await store.dispatch('lang/setLang', 'en') - return error({ message: 'Page not found', statusCode: 404 }) +export default function ({ i18n, store, route, params, error, redirect }) { + const locale = params.lang || 'en' + if (store.state.locales.indexOf(locale) === -1) { + return error({ message: 'This page could not be found.', statusCode: 404 }) + } + // Set locale + store.commit('SET_LANG', locale) + i18n.locale = store.state.locale + // If route is /en/... -> redirect to /... + if (locale === 'en' && route.fullPath.indexOf('/en') === 0) { + redirect(route.fullPath.replace(/^\/en/, '/')) } - await store.dispatch('lang/setLang', lang) } diff --git a/examples/i18n/nuxt.config.js b/examples/i18n/nuxt.config.js index 1fd8a6ad40..9cf19567b3 100644 --- a/examples/i18n/nuxt.config.js +++ b/examples/i18n/nuxt.config.js @@ -1,12 +1,16 @@ module.exports = { - loading: { - color: 'cyan' + build: { + vendor: ['vue-i18n'] }, router: { middleware: 'i18n' }, - build: { - vendor: ['axios'] + plugins: [ + // Will inject the plugin in the $root app and also in the context as `i18n` + { src: '~plugins/i18n.js', injectAs: 'i18n' } + ], + generate: { + routes: ['/', '/about', '/fr', '/fr/about'] }, - plugins: ['~plugins/i18n'] + loading: { color: 'cyan' }, } diff --git a/examples/i18n/package.json b/examples/i18n/package.json index 04228a1d9e..4731621677 100644 --- a/examples/i18n/package.json +++ b/examples/i18n/package.json @@ -1,12 +1,12 @@ { "name": "nuxt-i18n", "dependencies": { - "axios": "^0.15.3", "nuxt": "latest" }, "scripts": { "dev": "nuxt", "build": "nuxt build", - "start": "nuxt start" + "start": "nuxt start", + "generate": "nuxt generate" } } diff --git a/examples/i18n/pages/_lang/about.vue b/examples/i18n/pages/_lang/about.vue index e01f333525..f536aac53f 100644 --- a/examples/i18n/pages/_lang/about.vue +++ b/examples/i18n/pages/_lang/about.vue @@ -1,13 +1,16 @@ diff --git a/examples/i18n/pages/_lang/index.vue b/examples/i18n/pages/_lang/index.vue index 9bd4e585d9..acb7cc5387 100644 --- a/examples/i18n/pages/_lang/index.vue +++ b/examples/i18n/pages/_lang/index.vue @@ -1,13 +1,16 @@ diff --git a/examples/i18n/pages/about.vue b/examples/i18n/pages/about.vue index e01f333525..17031a84c3 100644 --- a/examples/i18n/pages/about.vue +++ b/examples/i18n/pages/about.vue @@ -1,13 +1,4 @@ - - diff --git a/examples/i18n/pages/index.vue b/examples/i18n/pages/index.vue index 9bd4e585d9..fab3df9c9c 100644 --- a/examples/i18n/pages/index.vue +++ b/examples/i18n/pages/index.vue @@ -1,13 +1,4 @@ - - diff --git a/examples/i18n/partials/About.vue b/examples/i18n/partials/About.vue deleted file mode 100644 index 9b3e526674..0000000000 --- a/examples/i18n/partials/About.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - diff --git a/examples/i18n/partials/Home.vue b/examples/i18n/partials/Home.vue deleted file mode 100644 index 0ee45a5ca2..0000000000 --- a/examples/i18n/partials/Home.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - diff --git a/examples/i18n/plugins/axios.js b/examples/i18n/plugins/axios.js deleted file mode 100644 index e44041d5f0..0000000000 --- a/examples/i18n/plugins/axios.js +++ /dev/null @@ -1,9 +0,0 @@ -import axios from 'axios' - -let options = {} -// The server-side needs a full url to works -if (process.SERVER_BUILD) { - options.baseURL = `http://${process.env.HOST || 'localhost'}:${process.env.PORT || 3000}` -} - -export default axios.create(options) diff --git a/examples/i18n/plugins/i18n.js b/examples/i18n/plugins/i18n.js index db834b5309..0bf5d875b6 100644 --- a/examples/i18n/plugins/i18n.js +++ b/examples/i18n/plugins/i18n.js @@ -1,12 +1,16 @@ import Vue from 'vue' +import VueI18n from 'vue-i18n' import store from '~store' -Vue.prototype.$t = function (key) { - const state = store.state.lang - let keys = key.split('.') - let value = state._[state.lang] - keys.forEach((k) => { - value = value[k] - }) - return value -} +Vue.use(VueI18n) + +const i18n = new VueI18n({ + locale: store.state.locale, + fallbackLocale: 'en', + messages: { + 'en': require('~/locales/en.json'), + 'fr': require('~/locales/fr.json') + } +}) + +export default i18n diff --git a/examples/vue-i18n/store/index.js b/examples/i18n/store/index.js similarity index 58% rename from examples/vue-i18n/store/index.js rename to examples/i18n/store/index.js index a978956377..0582e7bdb9 100644 --- a/examples/vue-i18n/store/index.js +++ b/examples/i18n/store/index.js @@ -1,10 +1,11 @@ export const state = { - locale: 'en-US' + locales: ['en', 'fr'], + locale: 'en' } export const mutations = { SET_LANG (state, locale) { - if (['en-US', 'fr-FR'].indexOf(locale) !== -1) { + if (state.locales.indexOf(locale) !== -1) { state.locale = locale } } diff --git a/examples/i18n/store/lang.js b/examples/i18n/store/lang.js deleted file mode 100644 index 4c58d1f668..0000000000 --- a/examples/i18n/store/lang.js +++ /dev/null @@ -1,27 +0,0 @@ -import axios from '~plugins/axios' - -export const state = { - locales: ['en', 'fr'], // available langages - lang: null, // current lang - _: {} // store for translations -} - -export const mutations = { - SET_LANG (state, lang) { - state.lang = lang - }, - SET_TRANSLATION (state, translation) { - state._[state.lang] = translation - } -} - -export const actions = { - async setLang ({ state, commit }, lang) { - if (state._[lang]) { - return commit('SET_LANG', lang) - } - let res = await axios.get(`/locales/${lang}.json`) - commit('SET_LANG', lang) - commit('SET_TRANSLATION', res.data) - } -} diff --git a/examples/vue-i18n/README.md b/examples/vue-i18n/README.md deleted file mode 100644 index d55e9ec258..0000000000 --- a/examples/vue-i18n/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# i18n with Nuxt.js - -https://nuxtjs.org/examples/i18n diff --git a/examples/vue-i18n/layouts/default.vue b/examples/vue-i18n/layouts/default.vue deleted file mode 100644 index eaf4bddcc1..0000000000 --- a/examples/vue-i18n/layouts/default.vue +++ /dev/null @@ -1,100 +0,0 @@ - - - - - diff --git a/examples/vue-i18n/layouts/error.vue b/examples/vue-i18n/layouts/error.vue deleted file mode 100644 index 32e5417d15..0000000000 --- a/examples/vue-i18n/layouts/error.vue +++ /dev/null @@ -1,59 +0,0 @@ - - - - - diff --git a/examples/vue-i18n/locales/en-US.json b/examples/vue-i18n/locales/en-US.json deleted file mode 100644 index 898218c0a0..0000000000 --- a/examples/vue-i18n/locales/en-US.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "links": { - "home": "Home", - "about": "About", - "english": "English version", - "french": "French version" - }, - "home": { - "title": "Welcome", - "introduction": "This is an introduction in English." - }, - "about": { - "title": "About", - "introduction": "This page is made to give you more informations." - } -} diff --git a/examples/vue-i18n/locales/fr-FR.json b/examples/vue-i18n/locales/fr-FR.json deleted file mode 100644 index b4e96aeb96..0000000000 --- a/examples/vue-i18n/locales/fr-FR.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "links": { - "home": "Accueil", - "about": "À propos", - "english": "Version Anglaise", - "french": "Version Française" - }, - "home": { - "title": "Bienvenue", - "introduction": "Ceci est un texte d'introduction en Français." - }, - "about": { - "title": "À propos", - "introduction": "Cette page est faite pour vous donner plus d'informations." - } -} diff --git a/examples/vue-i18n/middleware/i18n.js b/examples/vue-i18n/middleware/i18n.js deleted file mode 100644 index ea4d486b46..0000000000 --- a/examples/vue-i18n/middleware/i18n.js +++ /dev/null @@ -1,5 +0,0 @@ -export default function ({ app, store, params, error }) { - const locale = params.lang || 'en-US' - store.commit('SET_LANG', locale) - app.$i18n.locale = store.state.locale -} diff --git a/examples/vue-i18n/nuxt.config.js b/examples/vue-i18n/nuxt.config.js deleted file mode 100644 index d9fd7ea922..0000000000 --- a/examples/vue-i18n/nuxt.config.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = { - loading: { - color: 'cyan' - }, - router: { - middleware: 'i18n' - }, - build: { - vendor: ['vue-i18n'] - }, - plugins: [ - { src: '~plugins/i18n.js', injectAs: 'i18n' } - ] -} diff --git a/examples/vue-i18n/package.json b/examples/vue-i18n/package.json deleted file mode 100644 index 04228a1d9e..0000000000 --- a/examples/vue-i18n/package.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "nuxt-i18n", - "dependencies": { - "axios": "^0.15.3", - "nuxt": "latest" - }, - "scripts": { - "dev": "nuxt", - "build": "nuxt build", - "start": "nuxt start" - } -} diff --git a/examples/vue-i18n/pages/_lang/about.vue b/examples/vue-i18n/pages/_lang/about.vue deleted file mode 100644 index e01f333525..0000000000 --- a/examples/vue-i18n/pages/_lang/about.vue +++ /dev/null @@ -1,13 +0,0 @@ - - - diff --git a/examples/vue-i18n/pages/_lang/index.vue b/examples/vue-i18n/pages/_lang/index.vue deleted file mode 100644 index 9bd4e585d9..0000000000 --- a/examples/vue-i18n/pages/_lang/index.vue +++ /dev/null @@ -1,13 +0,0 @@ - - - diff --git a/examples/vue-i18n/pages/about.vue b/examples/vue-i18n/pages/about.vue deleted file mode 100644 index e01f333525..0000000000 --- a/examples/vue-i18n/pages/about.vue +++ /dev/null @@ -1,13 +0,0 @@ - - - diff --git a/examples/vue-i18n/pages/index.vue b/examples/vue-i18n/pages/index.vue deleted file mode 100644 index 9bd4e585d9..0000000000 --- a/examples/vue-i18n/pages/index.vue +++ /dev/null @@ -1,13 +0,0 @@ - - - diff --git a/examples/vue-i18n/partials/About.vue b/examples/vue-i18n/partials/About.vue deleted file mode 100644 index 9b3e526674..0000000000 --- a/examples/vue-i18n/partials/About.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - diff --git a/examples/vue-i18n/partials/Home.vue b/examples/vue-i18n/partials/Home.vue deleted file mode 100644 index 0ee45a5ca2..0000000000 --- a/examples/vue-i18n/partials/Home.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - diff --git a/examples/vue-i18n/plugins/i18n.js b/examples/vue-i18n/plugins/i18n.js deleted file mode 100644 index 8587fa6588..0000000000 --- a/examples/vue-i18n/plugins/i18n.js +++ /dev/null @@ -1,18 +0,0 @@ -import Vue from 'vue' -import VueI18n from 'vue-i18n' -import store from '~store' - -Vue.use(VueI18n) - -console.log(store.state.locale) - -const i18n = new VueI18n({ - locale: store.state.locale, - fallbackLocale: 'en-US', - messages: { - 'en-US': require('~/locales/en-US.json'), - 'fr-FR': require('~/locales/fr-FR.json') - } -}) - -export default i18n