1
0

locale.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import type { LocaleSetting, LocaleType } from '/#/config';
  2. import { defineStore } from 'pinia';
  3. import { store } from '/@/store';
  4. import { LOCALE_KEY } from '/@/enums/cacheEnum';
  5. import { createLocalStorage } from '/@/utils/cache';
  6. import { localeSetting } from '/@/settings/localeSetting';
  7. const ls = createLocalStorage();
  8. const lsLocaleSetting = (ls.get(LOCALE_KEY) || localeSetting) as LocaleSetting;
  9. interface LocaleState {
  10. localInfo: LocaleSetting;
  11. }
  12. export const useLocaleStore = defineStore({
  13. id: 'app-locale',
  14. state: (): LocaleState => ({
  15. localInfo: lsLocaleSetting,
  16. }),
  17. getters: {
  18. getShowPicker(): boolean {
  19. return !!this.localInfo?.showPicker;
  20. },
  21. getLocale(): LocaleType {
  22. return this.localInfo?.locale ?? 'zh_CN';
  23. },
  24. },
  25. actions: {
  26. /**
  27. * Set up multilingual information and cache
  28. * @param info multilingual info
  29. */
  30. setLocaleInfo(info: Partial<LocaleSetting>) {
  31. this.localInfo = { ...this.localInfo, ...info };
  32. ls.set(LOCALE_KEY, this.localInfo);
  33. },
  34. /**
  35. * Initialize multilingual information and load the existing configuration from the local cache
  36. */
  37. initLocale() {
  38. this.setLocaleInfo({
  39. ...localeSetting,
  40. ...this.localInfo,
  41. });
  42. },
  43. },
  44. });
  45. // Need to be used outside the setup
  46. export function useLocaleStoreWithOut() {
  47. return useLocaleStore(store);
  48. }