switchLocales.vue
2.44 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
<template>
<view style="padding:24rpx;background: #f8f9fa;height: 100vh;">
<view class="center">
<view v-for="(item, index) in languageData" :key="item.value" class="flex justify-between"
:class="index == 0 ? 'v-label' : ''" @click="handleChange(item.value)">
<view>{{ item.label }}</view>
<image v-if="language === item.value" class="image" src="/static/languageSelect.png"></image>
</view>
</view>
</view>
</template>
<script>
export default {
onLoad() {
this.getLocale()
},
data() {
return {
language: 'zh-CN',
languageData: [
{ label: '中文简体', value: 'zh-CN' },
{ label: 'English', value: 'en' }
]
}
},
onShow() {
this.$nextTick(() => {
uni.setNavigationBarTitle({
title: this.$t('menu.languageSettings')
})
})
},
methods: {
getLocale() {
const locale = uni.getLocale()
if (locale) {
this.language = locale.includes('zh')?'zh-CN' : locale
}
},
handleChange(name) {
//App会重启应用
// #ifdef APP-PLUS || APP
uni.showModal({
title:this.$t('userCenter.switchRestLanguage'),
success:(res)=>{
if(res.confirm){
this.$i18n.locale = name
this.language = name
uni.setNavigationBarTitle({
title: this.$t('menu.languageSettings')
})
// setLocale设置的语言需要对照到uniapp上的默认配置语言来设置
uni.setLocale(name.includes('zh')?'zh-Hans':'en')
}
}
})
// #endif
// #ifdef MP-WEIXIN
uni.showLoading({
title:this.$t('userCenter.switchLanguages')
})
const timeOut = setTimeout(()=>{
this.$i18n.locale = name
this.language = name
uni.setNavigationBarTitle({
title: this.$t('menu.languageSettings')
})
uni.setLocale(name.includes('zh')?'zh-Hans':'en')
uni.hideLoading()
clearTimeout(timeOut)
},500)
// #endif
}
}
}
</script>
<style scoped lang="scss">
.center {
background: #fff;
padding: 24rpx;
border-radius: 24rpx;
}
.flex {
display: flex;
}
.justify-between {
justify-content: space-between;
}
.v-label {
border-bottom: 1rpx solid #ecedee;
padding-bottom: 6rpx;
margin-bottom: 8rpx
}
.image {
width: 44rpx;
height: 44rpx;
// margin-top: 20rpx;
// border-radius: 32rpx;
}
</style>