switchLanguage.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view class="page">
  3. <view class="btn-list">
  4. <view class="language" data-lang="zh-CN" @tap="langChange"> <text>简体中文</text>
  5. <image v-if="languageSelect=='zh-CN'" class="selected" src="../../static/mine/326.png" mode=""></image>
  6. </view>
  7. <view class="language" data-lang="en-US" @tap="langChange"> <text>English</text>
  8. <image v-if="languageSelect=='en-US'" class="selected" src="../../static/mine/326.png" mode=""></image>
  9. </view>
  10. <view class="language" data-lang="es-ES" @tap="langChange"> <text>Español</text>
  11. <image v-if="languageSelect=='es-ES'" class="selected" src="../../static/mine/326.png" mode=""></image>
  12. </view>
  13. <view class="language" data-lang="it-IT" @tap="langChange"> <text>Italiano</text>
  14. <image v-if="languageSelect=='it-IT'" class="selected" src="../../static/mine/326.png" mode=""></image>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. languageSelect: '',
  24. language: 'cn'
  25. }
  26. },
  27. computed: {
  28. i18n() {
  29. return this.$t('index')
  30. }
  31. },
  32. methods: {
  33. update() {
  34. uni.$u.http.post('/api/member/update', {
  35. lang: this.language,
  36. }).then((res) => {
  37. this.$u.toast('切换成功')
  38. }).catch(() => {
  39. })
  40. },
  41. langChange(e) {
  42. const {
  43. lang
  44. } = e.currentTarget.dataset
  45. uni.setStorageSync("language", lang)
  46. this._i18n.locale = lang
  47. this.languageSelect = lang
  48. console.log('languageSelect',this.languageSelect);
  49. if (this.languageSelect == 'zh-CN') {
  50. this.language = 'cn'
  51. } else if (this.languageSelect == 'en-US') {
  52. this.language = 'en'
  53. } else if (this.languageSelect =='es-ES') {
  54. this.language = 'es'
  55. } else if (this.languageSelect == 'it-IT') {
  56. this.language = 'ita'
  57. }
  58. this.update()
  59. uni.setNavigationBarTitle({
  60. title: this.i18n.switchLanguage
  61. })
  62. }
  63. },
  64. created() {
  65. this.languageSelect = this._i18n.locale
  66. uni.setNavigationBarTitle({
  67. title: this.i18n.switchLanguage
  68. })
  69. }
  70. }
  71. </script>
  72. <style scoped lang="scss">
  73. .page {
  74. padding: 20rpx 24rpx;
  75. .btn-list {
  76. background-color: #fff;
  77. border-radius: 16rpx;
  78. padding: 0 20rpx;
  79. .language {
  80. display: flex;
  81. align-items: center;
  82. justify-content: space-between;
  83. height: 110rpx;
  84. border-bottom: 2rpx solid rgba(151, 151, 151, 0.2);
  85. .selected {
  86. width: 36rpx;
  87. height: 36rpx;
  88. }
  89. }
  90. }
  91. }
  92. </style>