| 1 | import type { NDateLocale, NLocale } from "naive-ui" |
| 2 | import type { WritableComputedRef } from "vue" |
| 3 | import type { LocaleCodes } from "@/lang/config" |
| 4 | import { |
| 5 | dateDeDE, |
| 6 | dateEnUS, |
| 7 | dateEsAR, |
| 8 | dateFrFR, |
| 9 | dateItIT, |
| 10 | dateJaJP, |
| 11 | deDE, |
| 12 | enUS, |
| 13 | esAR, |
| 14 | frFR, |
| 15 | itIT, |
| 16 | jaJP |
| 17 | } from "naive-ui" |
| 18 | import { acceptHMRUpdate, defineStore } from "pinia" |
| 19 | import { nextTick } from "vue" |
| 20 | import { useI18n } from "vue-i18n" |
| 21 | import dayjs from "@/utils/dayjs" |
| 22 | |
| 23 | export const useLocalesStore = defineStore("i18n", { |
| 24 | state: () => { |
| 25 | nextTick(() => { |
| 26 | const dayjsLocale = useLocalesStore().locale === "jp" ? "ja" : useLocalesStore().locale |
| 27 | dayjs.locale(dayjsLocale) |
| 28 | }) |
| 29 | |
| 30 | return { |
| 31 | locale: useI18n().locale as WritableComputedRef<LocaleCodes, string>, |
| 32 | availableLocales: useI18n().availableLocales as LocaleCodes[] |
| 33 | } |
| 34 | }, |
| 35 | actions: { |
| 36 | setLocale(locale: LocaleCodes): LocaleCodes { |
| 37 | const dayjsLocale = locale === "jp" ? "ja" : locale |
| 38 | dayjs.locale(dayjsLocale) |
| 39 | this.locale = locale |
| 40 | return locale |
| 41 | } |
| 42 | }, |
| 43 | getters: { |
| 44 | naiveuiLocales(): { code: LocaleCodes; ui: NLocale; date: NDateLocale }[] { |
| 45 | return [ |
| 46 | { code: "de", ui: deDE, date: dateDeDE }, |
| 47 | { code: "en", ui: enUS, date: dateEnUS }, |
| 48 | { code: "es", ui: esAR, date: dateEsAR }, |
| 49 | { code: "fr", ui: frFR, date: dateFrFR }, |
| 50 | { code: "it", ui: itIT, date: dateItIT }, |
| 51 | { code: "jp", ui: jaJP, date: dateJaJP } |
| 52 | ] |
| 53 | }, |
| 54 | naiveuiLocale(state): NLocale | undefined { |
| 55 | return this.naiveuiLocales.find(locale => locale.code === state.locale)?.ui |
| 56 | }, |
| 57 | naiveuiDateLocale(state): NDateLocale | undefined { |
| 58 | return this.naiveuiLocales.find(locale => locale.code === state.locale)?.date |
| 59 | } |
| 60 | }, |
| 61 | persist: { |
| 62 | // @ts-expect-error "Type instantiation is excessively deep and possibly infinite" ts(2589) |
| 63 | pick: ["locale"] |
| 64 | } |
| 65 | }) |
| 66 | |
| 67 | if (import.meta.hot) { |
| 68 | import.meta.hot.accept(acceptHMRUpdate(useLocalesStore, import.meta.hot)) |
| 69 | } |