I18nPop.vue
5.4 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<template>
<uv-popup ref="i18nPopRef" round="24rpx" @maskClick="handleShowTabbar">
<view class="pop_header">
<view style="width: 22px;visibility: hidden;"></view>
<!-- <view style="width: 22px;"></view> -->
<text class="pop_header_text">{{ t('table.language') }}</text>
<uv-icon name="close" size="22" class="pop_header_icon" @tap="handleCloseI18nPop"></uv-icon>
</view>
<view class="national">
<view class="n_item" v-for="(item,index) in i18nList" :key="item.value"
:class="{'n_item_active':index === checkIndex}" @tap="handleChangeI18n(index)">
<image :src="item.nationalFlag" style="width: 52rpx;" mode="widthFix"></image>
<view class="n_label">
<view class="n_item_text" :class="{'n_item_active_text':index === checkIndex}">{{ item.label }}</view>
<view class="n_item_textDesc">{{ item.labelDesc }}</view>
</view>
<image v-if="checkIndex === index" src="@/static/national-flag/check.png" style="width: 36rpx;height:36rpx"
mode="aspectFill"></image>
</view>
</view>
<view class="confirm-button" @tap="handleConfirmSelectType">
<text class="btn">{{ t('message.confirm') }}</text>
</view>
</uv-popup>
</template>
<script setup>
import {
ref,
computed
} from 'vue'
import {
onLoad
} from '@dcloudio/uni-app'
import {
t,
changeLanguage
} from '@/plugins/index.js'
import {
bindClientIdApi
} from '@/api';
import usa from '@/static/national-flag/usa.png'
import japan from '@/static/national-flag/japan.png'
import cn from '@/static/national-flag/cn.png'
const props = defineProps({
reloadPage: {
type: String,
default: ''
},
bindCid: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['changeLang'])
const i18nPopRef = ref(null)
const checkIndex = ref(0)
const handleOpenI18n = () => {
uni.hideTabBar()
const index = i18nList.findIndex(item => item.value === currentLang.value)
checkIndex.value = index === -1 ? 0 : index
i18nPopRef.value?.open('bottom')
}
const handleShowTabbar = ()=>{
uni.showTabBar()
}
const handleCloseI18nPop = () => {
i18nPopRef.value?.close()
handleShowTabbar()
}
const handleChangeI18n = (index) => {
checkIndex.value = index
}
const handleConfirmSelectType = async () => {
try {
const index = i18nList.findIndex(item => item.value === currentLang.value)
currentLang.value = i18nList[checkIndex.value].value
await changeLanguage(currentLang.value, true)
await handleCloseI18nPop()
// 重新载入
if (props.reloadPage) {
uni.setStorageSync('lang', currentLang.value)
// if(props.tab){
// uni.switchTab({
// url: props.reloadPage
// })
// }else{
// // uni.reLaunch({
// // url: props.reloadPage
// // })
// }
}
const item = i18nList.find(item => item.value === currentLang.value)
if(props.bindCid){
await handleRebindCid()
}
await emit('changeLang', item)
} catch (e) {
console.log(e)
}
}
const handleRebindCid = async ()=>{
try {
const clientInfo = uni.getDeviceInfo()
await bindClientIdApi({
clientId: uni.getStorageSync('clientId'),
clientType: clientInfo.osName, // ios or android
deviceInfo: JSON.stringify(clientInfo), // 备用传值
language: uni.getStorageSync('lang'),
status: 1
})
} catch (error) {
console.log('error:', error);
}
}
const currentLang = ref(uni.getStorageSync('lang') || 'en_US')
const currentLangText = computed(() => {
const item = i18nList.find(item => item.value === currentLang.value)
return item?.labelDesc || 'English,US'
})
const currentLangImg = computed(() => {
const item = i18nList.find(item => item.value === currentLang.value)
return item?.nationalFlag || usa
})
const i18nList = [{
label: 'English,US',
labelDesc: 'English,US',
value: 'en_US',
nationalFlag: usa
}, {
label: 'Japan',
labelDesc: 'Japanese',
value: 'ja_JP',
nationalFlag: japan
},
// {
// label: 'China',
// labelDesc: 'Chinese',
// value: 'zh_CN',
// nationalFlag: cn
// }, {
// label: 'China',
// labelDesc: 'Traditional Chinese',
// value: 'zh_TW',
// nationalFlag: cn
// },
]
onLoad(()=>{
const item = i18nList.find(item => item.value === currentLang.value)
emit('changeLang', item)
})
defineExpose({
currentLangImg,
currentLangText,
handleOpenI18n
})
</script>
<style lang="scss" scoped>
.national {
overflow: hidden;
padding: 32rpx 0 32rpx 32rpx;
}
.n_item {
padding: 16rpx 32rpx 16rpx 0;
display: flex;
align-items: center;
border-bottom: 1px solid #EEF0F3;
}
.n_label {
flex: 1;
margin-left: 30rpx;
.n_item_text {
font-family: Inter, Inter;
font-weight: 500;
font-size: 28rpx;
color: #262626;
}
.n_item_textDesc {
font-family: Inter, Inter;
font-weight: 400;
font-size: 24rpx;
color: #90949D;
}
}
.confirm-button {
width: 686rpx;
height: 88rpx;
background-color: #387CF5;
border-radius: 12rpx;
margin: 14rpx 32rpx 50rpx;
text-align: center;
line-height: 88rpx;
.btn {
color: #fff;
text-align: center;
}
}
</style>