useLocaleUpdate.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { storeToRefs } from 'pinia'
  3. import { computed, ref } from 'vue'
  4. import {
  5. NotificationTypes,
  6. useNotifications,
  7. } from '#shared/components/CommonNotifications/index.ts'
  8. import { useUserCurrentLocaleMutation } from '#shared/entities/user/current/graphql/mutations/userCurrentLocale.api.ts'
  9. import { useLocaleStore } from '#shared/stores/locale.ts'
  10. import MutationHandler from '../server/apollo/handler/MutationHandler.ts'
  11. const ZAMMAD_TRANSLATION_LINK = 'https://translations.zammad.org/'
  12. export const useLocaleUpdate = () => {
  13. const isSavingLocale = ref(false)
  14. const localeMutation = new MutationHandler(useUserCurrentLocaleMutation({}), {
  15. errorNotificationMessage: __('The language could not be updated.'),
  16. })
  17. const { notify } = useNotifications()
  18. const localeStore = useLocaleStore()
  19. const { localeData, locales } = storeToRefs(localeStore)
  20. const { setLocale } = localeStore
  21. const modelCurrentLocale = computed({
  22. get: () => localeData.value?.locale ?? 'en',
  23. set: (locale) => {
  24. if (!locale || localeData.value?.locale === locale) return
  25. isSavingLocale.value = true
  26. Promise.all([setLocale(locale), localeMutation.send({ locale })])
  27. .then(() => {
  28. notify({
  29. id: 'locale-update',
  30. message: __('Profile language updated successfully.'),
  31. type: NotificationTypes.Success,
  32. })
  33. })
  34. .finally(() => {
  35. isSavingLocale.value = false
  36. })
  37. },
  38. })
  39. const localeOptions = computed(() => {
  40. return (
  41. locales?.value?.map((locale) => {
  42. return { label: locale.name, value: locale.locale }
  43. }) || []
  44. )
  45. })
  46. return {
  47. translation: {
  48. link: ZAMMAD_TRANSLATION_LINK,
  49. },
  50. isSavingLocale,
  51. modelCurrentLocale,
  52. localeOptions,
  53. }
  54. }