Save language for the user permanently when changing the language

This commit is contained in:
ogerly 2019-10-22 11:34:04 +02:00
parent 9aa904e3d3
commit 9dccb5fc0a
3 changed files with 43 additions and 1 deletions

View File

@ -390,7 +390,7 @@ describe('SignupVerification', () => {
nonce: $nonce
about: $about
termsAndConditionsAgreedVersion: $termsAndConditionsAgreedVersion
locale: $language
locale: $locale
) {
id
termsAndConditionsAgreedVersion

View File

@ -37,6 +37,8 @@ import Dropdown from '~/components/Dropdown'
import find from 'lodash/find'
import orderBy from 'lodash/orderBy'
import locales from '~/locales'
import { mapGetters, mapMutations } from 'vuex'
import { localeMutation } from '~/graphql/User.js'
export default {
components: {
@ -64,15 +66,44 @@ export default {
})
return routes
},
...mapGetters({
currentUser: 'auth/user',
}),
},
methods: {
changeLanguage(locale, toggleMenu) {
this.$i18n.set(locale)
this.change()
toggleMenu()
},
matcher(locale) {
return locale === this.$i18n.locale()
},
...mapMutations({
setCurrentUser: 'auth/SET_USER',
}),
async change() {
try {
await this.$apollo.mutate({
mutation: localeMutation(),
variables: {
id: this.currentUser.id,
locale: this.$i18n.locale(),
},
update: (store, { data: { UpdateUser } }) => {
const { locale } = UpdateUser
this.setCurrentUser({
...this.currentUser,
locale,
})
},
})
this.$toast.success(this.$t('contribution.success'))
} catch (err) {
this.$toast.error(err.message)
}
},
},
}
</script>

View File

@ -153,3 +153,14 @@ export const checkSlugAvailableQuery = gql`
}
}
`
export const localeMutation = () => {
return gql`
mutation($id: ID!, $locale: String) {
UpdateUser(id: $id, locale: $locale) {
id
locale
}
}
`
}