cwg-language.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <view class="cwg-language">
  3. <cwg-dropdown :menu-list="customMenuList" @menuClick="handleMenuClick" showActive :activeKey="curLange">
  4. <view class="pc-header-btn">
  5. <cwg-icon name="cwg-lang" :color="iconColor " :size="20" />
  6. </view>
  7. </cwg-dropdown>
  8. </view>
  9. </template>
  10. <script setup lang="ts">
  11. import { computed,ref } from 'vue'
  12. import { useI18n } from 'vue-i18n'
  13. import { localesList, LANG_MAP } from '@/locale/index'
  14. import { lang } from '@/composables/config'
  15. const props = defineProps({
  16. iconColor: {
  17. type: String,
  18. default: '#97A1C0'
  19. },
  20. })
  21. const curLange = ref(lang)
  22. const { t, locale } = useI18n()
  23. const customMenuList = computed(() =>
  24. localesList.map((code) => ({
  25. label: t(`language.${code}`),
  26. type: code
  27. })),
  28. )
  29. function handleMenuClick({ value }: { value: any }) {
  30. const type = value?.type ?? value
  31. if (!type) return
  32. locale.value = type
  33. lang.value = type
  34. curLange.value = type
  35. const localeValue = LANG_MAP[type] || 'zh-Hans'
  36. uni.setLocale(localeValue)
  37. }
  38. function getFlagSrc(code: string) {
  39. switch (code) {
  40. case 'en':
  41. return '/static/flag-en.png'
  42. case 'cn':
  43. return '/static/flag-cn.png'
  44. case 'zhHant':
  45. return '/static/flag-zhHant.png'
  46. case 'es':
  47. return '/static/flag-es.png'
  48. default:
  49. return '/static/flag-en.png'
  50. }
  51. }
  52. </script>
  53. <style scoped lang="scss">
  54. @import "@/uni.scss";
  55. .pc-header-btn {
  56. display: flex;
  57. align-items: center;
  58. cursor: pointer;
  59. gap: px2rpx(6);
  60. }
  61. .cwg-language {
  62. @media screen and (max-width: 991px) {
  63. :deep(.cwg-dropdown-menu-container) {
  64. right: px2rpx(-20) !important;
  65. }
  66. }
  67. }
  68. :deep(.cwg-dropdown-menu-container .menu .menu-item) {
  69. min-height: px2rpx(36);
  70. }
  71. :deep(.cwg-dropdown) {
  72. overflow: visible !important;
  73. }
  74. </style>