locale.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. interface appList {
  13. name: string;
  14. url: string;
  15. }
  16. interface appItem {
  17. name: string;
  18. icon: string;
  19. appList: appList[];
  20. }
  21. export const useLocaleStore = defineStore({
  22. id: 'app-locale',
  23. state: (): LocaleState => ({
  24. localInfo: lsLocaleSetting,
  25. }),
  26. getters: {
  27. getShowPicker(): boolean {
  28. return !!this.localInfo?.showPicker;
  29. },
  30. getLocale(): LocaleType {
  31. return this.localInfo?.locale ?? 'zh_CN';
  32. },
  33. getAppList(): appItem[] {
  34. return [
  35. {
  36. name: 'iOS',
  37. icon: 'uil:apple',
  38. appList: [
  39. {
  40. name: 'Pro',
  41. url: 'https://apps.apple.com/cn/app/id6451010749',
  42. },
  43. {
  44. name: 'Minion/Mega',
  45. url: 'https://apps.apple.com/cn/app/id6451013242',
  46. },
  47. ],
  48. },
  49. {
  50. name: 'Android',
  51. icon: 'uil:android',
  52. appList: [
  53. {
  54. name: 'Pro',
  55. url: 'https://play.google.com/store/apps/details?id=com.fdage.eight.jp',
  56. },
  57. {
  58. name: 'Minion/Mega',
  59. url: 'https://play.google.com/store/apps/details?id=com.fdage.turn.dual.jp',
  60. },
  61. ],
  62. },
  63. ];
  64. },
  65. },
  66. actions: {
  67. /**
  68. * Set up multilingual information and cache
  69. * @param info multilingual info
  70. */
  71. setLocaleInfo(info: Partial<LocaleSetting>) {
  72. this.localInfo = { ...this.localInfo, ...info };
  73. ls.set(LOCALE_KEY, this.localInfo);
  74. },
  75. /**
  76. * Initialize multilingual information and load the existing configuration from the local cache
  77. */
  78. initLocale() {
  79. this.setLocaleInfo({
  80. ...localeSetting,
  81. ...this.localInfo,
  82. });
  83. },
  84. },
  85. });
  86. // Need to be used outside the setup
  87. export function useLocaleStoreWithOut() {
  88. return useLocaleStore(store);
  89. }