Skip to content

Commit

Permalink
Support vue-i18n options
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgv committed Nov 26, 2017
1 parent f884416 commit 2801410
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
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

0 comments on commit 2801410

Please sign in to comment.