Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgv committed Nov 26, 2017
2 parents f884416 + 51d88a9 commit 93a2b32
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 35 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 |
17 changes: 9 additions & 8 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand All @@ -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')
}
7 changes: 2 additions & 5 deletions lib/templates/i18n.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/i18n.routing.middleware.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/i18n.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default {
namespaced: true,

state: () => ({
locales: <%= options.locales %>,
locales: <%= JSON.stringify(options.locales) %>,
currentLocale: '<%= options.defaultLocale %>'
}),

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 93a2b32

Please sign in to comment.