select-lang.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <script setup lang="ts">
  2. import { computed, ref } from 'vue';
  3. import { loadLocaleMessages } from '@vben/locales';
  4. import { preferences, updatePreferences } from '@vben/preferences';
  5. import { Dropdown } from 'antdv-next';
  6. const items = [
  7. {
  8. key: 'en-US',
  9. label: 'English',
  10. },
  11. {
  12. key: 'zh-CN',
  13. label: '中文',
  14. },
  15. ];
  16. const show = ref(false);
  17. const currentLabel = computed(() => {
  18. const selected = items.find((item) => item.key === preferences.app.locale);
  19. return selected?.label ?? 'Language';
  20. });
  21. const onOpenChange = (open: boolean) => {
  22. show.value = open;
  23. };
  24. const onMenuClick = (info: any) => {
  25. if (preferences.app.locale === info.key) {
  26. return;
  27. }
  28. updatePreferences({
  29. app: {
  30. locale: info.key,
  31. },
  32. });
  33. loadLocaleMessages(info.key);
  34. };
  35. </script>
  36. <template>
  37. <Dropdown
  38. :menu="{ items }"
  39. placement="bottom"
  40. @menu-click="onMenuClick"
  41. @open-change="onOpenChange"
  42. >
  43. <div class="flex cursor-pointer items-center gap-2">
  44. <img
  45. alt="earth"
  46. height="20px"
  47. src="@/assets/image/earth_18301626@2x.png"
  48. width="20px"
  49. />
  50. <span>{{ currentLabel }}</span>
  51. <img
  52. :class="show ? 'rotate-180' : ''"
  53. alt="polygon"
  54. height="6px"
  55. src="@/assets/image/Polygon 2.png"
  56. width="7px"
  57. />
  58. </div>
  59. </Dropdown>
  60. </template>
  61. <style scoped></style>