switchLanguage.vue 2.4 KB

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