Skip to content

Commit

Permalink
refactor: Disable setLocale & setMessages mutations by default
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The mutations responsible for syncing nuxt-i18n's store module with vue-i18n's
locale and messages are now disabled by default, you'll need to manually re-enable them in the
module's configuration
  • Loading branch information
paulgv committed Jul 20, 2019
1 parent 2ceb8e4 commit 76c9978
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
10 changes: 5 additions & 5 deletions docs/options-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ Here are all the options available when configuring the module and their default

// Mutations config
mutations: {
// Mutation to commit to store current locale, set to false to disable
setLocale: 'I18N_SET_LOCALE',
// If enabled, current app's locale is synced with nuxt-i18n store module
setLocale: false,

// Mutation to commit to store current message, set to false to disable
setMessages: 'I18N_SET_MESSAGES',
// If enabled, current translation messages are synced with nuxt-i18n store module
setMessages: false,

// Mutation to commit to set route parameters translations
setRouteParams: 'I18N_SET_ROUTE_PARAMS'
setRouteParams: true
}
},

Expand Down
6 changes: 3 additions & 3 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ exports.DEFAULT_OPTIONS = {
vuex: {
moduleName: 'i18n',
mutations: {
setLocale: 'I18N_SET_LOCALE',
setMessages: 'I18N_SET_MESSAGES',
setRouteParams: 'I18N_SET_ROUTE_PARAMS'
setLocale: false,
setMessages: false,
setRouteParams: true
}
},
parsePages: true,
Expand Down
28 changes: 19 additions & 9 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,47 @@ export default async (context) => {
store.registerModule(vuex.moduleName, {
namespaced: true,
state: () => ({
locale: '',
messages: {},
routeParams: {}
<% if (options.vuex.mutations.setLocale) { %>locale: '',<% } %>
<% if (options.vuex.mutations.setMessages) { %>messages: {},<% } %>
<% if (options.vuex.mutations.setRouteParams) { %>routeParams: {}<% } %>
}),
actions: {
<% if (options.vuex.mutations.setLocale) { %>
setLocale ({ commit }, locale) {
commit(vuex.mutations.setLocale, locale)
commit('setLocale', locale)
},
<% } if (options.vuex.mutations.setMessages) { %>
setMessages ({ commit }, messages) {
commit(vuex.mutations.setMessages, messages)
commit('setMessages', messages)
},
<% } if (options.vuex.mutations.setRouteParams) { %>
setRouteParams ({ commit }, params) {
if (process.env.NODE_ENV === 'development') {
validateRouteParams(params)
}
commit(vuex.mutations.setRouteParams, params)
commit('setRouteParams', params)
}
<% } %>
},
mutations: {
[vuex.mutations.setLocale] (state, locale) {
<% if (options.vuex.mutations.setLocale) { %>
setLocale (state, locale) {
state.locale = locale
},
[vuex.mutations.setMessages] (state, messages) {
<% } if (options.vuex.mutations.setMessages) { %>
setMessages (state, messages) {
state.messages = messages
},
[vuex.mutations.setRouteParams] (state, params) {
<% } if (options.vuex.mutations.setRouteParams) { %>
setRouteParams (state, params) {
state.routeParams = params
}
<% } %>
},
getters: {
<% if (options.vuex.mutations.setRouteParams) { %>
localeRouteParams: ({ routeParams }) => locale => routeParams[locale] || {}
<% } %>
}
}, { preserveState: !!store.state[vuex.moduleName] })
}
Expand Down

0 comments on commit 76c9978

Please sign in to comment.