From 28014102f96ed88db94fb50f7ddc4d34dcb9e7a4 Mon Sep 17 00:00:00 2001 From: Paul Gascou-Vaillancourt Date: Sun, 26 Nov 2017 13:01:36 -0500 Subject: [PATCH 1/2] Support vue-i18n options --- lib/module.js | 17 +++++++++-------- lib/templates/i18n.plugin.js | 7 ++----- lib/templates/i18n.routing.middleware.js | 2 +- lib/templates/i18n.store.js | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/module.js b/lib/module.js index 871d09ea4..5a14155fb 100644 --- a/lib/module.js +++ b/lib/module.js @@ -6,11 +6,12 @@ module.exports = function (moduleOptions) { const defaults = {} const options = merge(defaults, moduleOptions, this.options.i18n) - const templatesOptions = { - defaultLocale: options.defaultLocale, - locales: JSON.stringify(options.locales), - fallbackLocale: options.fallbackLocale, - messages: JSON.stringify(options.messages) + // Backward compatibility with 0.0.1 + if (options.fallbackLocale && !options.vueI18n.fallbackLocale) { + options.vueI18n.fallbackLocale = options.fallbackLocale + } + if (options.messages && !options.vueI18n.messages) { + options.vueI18n.messages = options.messages } this.extendRoutes((routes) => { @@ -28,7 +29,7 @@ module.exports = function (moduleOptions) { this.addPlugin({ src: resolve(__dirname, './templates/i18n.plugin.js'), fileName: 'i18n.plugin.js', - options: templatesOptions + options }) // Routing plugin @@ -41,14 +42,14 @@ module.exports = function (moduleOptions) { this.addTemplate({ src: resolve(__dirname, './templates/i18n.store.js'), fileName: 'i18n.store.js', - options: templatesOptions + options }) // Middleware this.addTemplate({ src: resolve(__dirname, './templates/i18n.routing.middleware.js'), fileName: 'i18n.routing.middleware.js', - options: templatesOptions + options }) this.options.router.middleware.push('i18n') } diff --git a/lib/templates/i18n.plugin.js b/lib/templates/i18n.plugin.js index c17b49426..a812d4a6e 100644 --- a/lib/templates/i18n.plugin.js +++ b/lib/templates/i18n.plugin.js @@ -7,15 +7,12 @@ Vue.use(VueI18n) export default ({ app, store, route, isClient, hotReload }) => { store.registerModule('i18n', i18nStore) - const i18n = new VueI18n({ - <% if (options.fallbackLocale) { %>fallbackLocale: '<%= options.fallbackLocale %>',<% } %> - <% if (options.messages) { %>messages: <%= options.messages %><% } %> - }) + const i18n = new VueI18n(<%= JSON.stringify(options.vueI18n) %>) i18n.locale = store.state.i18n.currentLocale app.i18n = i18n // Check locale in URL (same as middleware but exclusive to client) if (isClient) { - const locales = <%= options.locales %> + const locales = <%= JSON.stringify(options.locales) %> const defaultLocale = '<%= options.defaultLocale %>' // Check if middleware called from hot-reloading, ignore if (hotReload) return diff --git a/lib/templates/i18n.routing.middleware.js b/lib/templates/i18n.routing.middleware.js index 36b65ce24..f507b94b0 100644 --- a/lib/templates/i18n.routing.middleware.js +++ b/lib/templates/i18n.routing.middleware.js @@ -1,7 +1,7 @@ import middleware from './middleware' middleware['i18n'] = function authMiddleware ({ app, store, route, error, hotReload, isServer }) { - const locales = <%= options.locales %> + const locales = <%= JSON.stringify(options.locales) %> const defaultLocale = '<%= options.defaultLocale %>' // Check if middleware called from hot-reloading, ignore if (hotReload) return diff --git a/lib/templates/i18n.store.js b/lib/templates/i18n.store.js index 2be61d26e..c247dc91d 100644 --- a/lib/templates/i18n.store.js +++ b/lib/templates/i18n.store.js @@ -2,7 +2,7 @@ export default { namespaced: true, state: () => ({ - locales: <%= options.locales %>, + locales: <%= JSON.stringify(options.locales) %>, currentLocale: '<%= options.defaultLocale %>' }), From 51d88a960cbb148141bacdcdd0076f3b4678970d Mon Sep 17 00:00:00 2001 From: Paul Gascou-Vaillancourt Date: Sun, 26 Nov 2017 13:10:07 -0500 Subject: [PATCH 2/2] Prepare release v0.1.0 --- README.md | 39 ++++++++++++++++++++------------------- package.json | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 555da071c..69eb63c57 100644 --- a/README.md +++ b/README.md @@ -81,19 +81,21 @@ module.exports = { } ], defaultLocale: 'en', - fallbackLocale: 'en', - messages: { - fr: { - home: 'Accueil', - about: 'À propos', - category: 'Catégorie' + vueI18n: { + messages: { + fr: { + home: 'Accueil', + about: 'À propos', + category: 'Catégorie' + }, + en: { + home: 'Homepage', + about: 'About us', + category: 'Category' + } }, - en: { - home: 'Homepage', - about: 'About us', - category: 'Category' - } - }, + fallbackLocale: 'en', + } routes: { about: { fr: '/a-propos', @@ -184,10 +186,9 @@ In the app, you'll need to preserve the language option when showing links. To d ## Options -| Option | Type | Description | -|------------------|--------|----------------------------------------------------------------------------------------------------------------------| -| `locales` | Array | A list of objects that describes the locales available in your app, each object should contain at least a `code` key | -| `defaultLocale` | String | The app's default locale, URLs for this language won't be prefixed with the locale code | -| `fallbackLocale` | String | Fallback locale used by vue-i18n when no message is available in the current language | -| `messages` | Object | Translated message to use with vue-i18n | -| `routes` | Object | Custom routing configuration, if routes are omitted, Nuxt's default routes are used | +| Option | Type | Description | +|-----------------|--------|-------------------------------------------------------------------------------------------------------------------------------------------------| +| `locales` | Array | A list of objects that describes the locales available in your app, each object should contain at least a `code` key | +| `defaultLocale` | String | The app's default locale, URLs for this language won't be prefixed with the locale code | +| `vueI18n` | Object | Configuration options for vue-i18n, refer to [the doc](http://kazupon.github.io/vue-i18n/en/api.html#constructor-options) for supported options | +| `routes` | Object | Custom routing configuration, if routes are omitted, Nuxt's default routes are used | diff --git a/package.json b/package.json index 7a9e3a61e..bf4d9cb9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuxt-i18n", - "version": "0.0.1", + "version": "0.1.0", "description": "i18n for Nuxt", "main": "lib/module.js", "license": "MIT",