WXL
2025-12-27 05e6b08007a86b5b10c680babc9c3bcc3a1a201b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<template>
  <view class="language-switch">
    <view class="current-lang" @tap="showLanguagePopup">
      <text class="iconfont icon-language"></text>
      <text>{{ currentLanguageLabel }}</text>
    </view>
    
    <!-- 语言选择弹窗 -->
    <uni-popup ref="popupRef" type="bottom">
      <view class="lang-popup">
        <view class="popup-header">
          <text class="title">{{ $t('common.language.select') }}</text>
          <text class="close" @tap="closePopup">×</text>
        </view>
        <view class="lang-list">
          <view 
            class="lang-item" 
            v-for="lang in languages" 
            :key="lang.value"
            :class="{ active: currentLang === lang.value }"
            @tap="selectLanguage(lang.value)"
          >
            <text>{{ lang.label }}</text>
            <text class="iconfont icon-check" v-if="currentLang === lang.value"></text>
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>
 
<script setup>
import { ref, computed } from 'vue'
import { getStoredLanguage, setLanguage } from '@/locale'
 
const popupRef = ref(null)
const currentLang = ref(getStoredLanguage())
const languages = [
  { label: '简体中文', value: 'zh-Hans' },
  { label: '繁體中文', value: 'zh-Hant' },
  { label: 'Português', value: 'pt' }
]
 
const currentLanguageLabel = computed(() => {
  const lang = languages.find(l => l.value === currentLang.value)
  return lang ? lang.label : ''
})
 
const showLanguagePopup = () => {
  popupRef.value?.open()
}
 
const closePopup = () => {
  popupRef.value?.close()
}
 
const selectLanguage = (lang) => {
  if (currentLang.value !== lang) {
    currentLang.value = lang
    setLanguage(lang)
    // 刷新页面应用新语言
    uni.reLaunch({
      url: '/pages/index/index'
    })
  }
  closePopup()
}
</script>
 
<style lang="scss">
.language-switch {
  .current-lang {
    display: flex;
    align-items: center;
    height: 72rpx;
    padding: 10rpx 20rpx;
    background: #F5F6FA;
    border-radius: 36rpx;
    
    .icon-language {
      width: 36rpx;
      height: 36rpx;
      margin-right: 10rpx;
    }
    
    text {
      font-size: 28rpx;
      color: #333;
    }
  }
}
 
.lang-popup {
  background: #fff;
  border-radius: 24rpx 24rpx 0 0;
  
  .popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    border-bottom: 1rpx solid #eee;
    
    .title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }
    
    .close {
      font-size: 40rpx;
      color: #999;
      padding: 0 20rpx;
    }
  }
  
  .lang-list {
    .lang-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 30rpx;
      border-bottom: 1rpx solid #eee;
      
      text {
        font-size: 30rpx;
        color: #333;
      }
      
      &.active {
        color: #0066CC;
        
        text {
          color: #0066CC;
        }
      }
      
      .icon-check {
        width: 32rpx;
        height: 32rpx;
      }
    }
  }
}
</style>