LanguageDropdown.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <cwg-dropdown :menu-list="customMenuList" @menuClick="handleMenuClick">
  3. <view class="lang-select">
  4. <cwg-icon name="cwg-lang" color="#141d22" />
  5. </view>
  6. </cwg-dropdown>
  7. </template>
  8. <script setup lang="ts">
  9. import { computed } from 'vue'
  10. import { useI18n } from 'vue-i18n'
  11. import { localesList, LANG_MAP } from '@/locale/index'
  12. import { lang } from '@/composables/config'
  13. const { t, locale } = useI18n()
  14. const currentLang = computed(() => lang.value || locale.value)
  15. const currentLabel = computed(() => t(`language.${currentLang.value}`))
  16. const currentFlag = computed(() => getFlagSrc(currentLang.value))
  17. const customMenuList = computed(() =>
  18. localesList.map((code) => ({
  19. label: t(`language.${code}`),
  20. type: code
  21. })),
  22. )
  23. function handleMenuClick({ value }: { value: any }) {
  24. const type = value?.type ?? value
  25. if (!type) return
  26. locale.value = type
  27. lang.value = type
  28. const localeValue = LANG_MAP[type] || 'zh-Hans'
  29. uni.setLocale(localeValue)
  30. }
  31. function getFlagSrc(code: string) {
  32. switch (code) {
  33. case 'en':
  34. return '/static/flag-en.png'
  35. case 'cn':
  36. return '/static/flag-cn.png'
  37. case 'zhHant':
  38. return '/static/flag-zhHant.png'
  39. case 'es':
  40. return '/static/flag-es.png'
  41. default:
  42. return '/static/flag-en.png'
  43. }
  44. }
  45. </script>
  46. <style scoped lang="scss">
  47. @import "@/uni.scss";
  48. .lang-select {
  49. width: px2rpx(40);
  50. height: px2rpx(40);
  51. border-radius: px2rpx(4);
  52. border: 1px solid rgba(108, 133, 149, 0);
  53. display: flex;
  54. align-items: center;
  55. font-size: px2rpx(14);
  56. cursor: pointer;
  57. display: flex;
  58. justify-content: center;
  59. align-items: center;
  60. &:hover {
  61. background: rgba(108, 133, 149, 0.12);
  62. border-color: rgb(145, 163, 176);
  63. }
  64. }
  65. .flag {
  66. width: 22px;
  67. height: 16px;
  68. margin-right: 6px;
  69. }
  70. .arrow {
  71. margin-left: 4px;
  72. font-size: 12px;
  73. }
  74. :deep(.cwg-dropdown-menu-container .menu .menu-item) {
  75. min-height: px2rpx(36);
  76. }
  77. :deep(.cwg-dropdown) {
  78. overflow: visible !important;
  79. }
  80. </style>