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