SToggleLanguage2.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const n = `<template>
  2. <div
  3. class="s-toggle-language"
  4. :class="blockType === 'button' ? 'flex-right' : ''"
  5. v-if="$utils.env.VITE_APP_LANG_ENABLE"
  6. >
  7. <a-dropdown :trigger="['click']" @openChange="openChange" v-if="blockType === 'dropdown'">
  8. <div class="locale-select">
  9. <div class="label">{{ lang === 'zh' ? '中文' : 'English' }}</div>
  10. <img src="@p/static/svg/login/map-icon.svg" class="icon" v-if="!isOpen" />
  11. <img src="@p/static/svg/login/map-icon-s.svg" class="icon" v-else />
  12. <s-icon
  13. name="CaretDownOutlined"
  14. style="color: var(--s-text-color); font-size: 12px"
  15. v-if="!isOpen"
  16. />
  17. <s-icon name="CaretUpOutlined" class="s-theme-color" style="font-size: 12px" v-else />
  18. </div>
  19. <template #overlay>
  20. <a-menu>
  21. <a-menu-item v-for="item in locales" :key="item.key" @click="toggleLang(item)">
  22. {{ item.name }}
  23. </a-menu-item>
  24. </a-menu>
  25. </template>
  26. </a-dropdown>
  27. <a-button v-else class="custom-btn" @click="toggleChange">{{
  28. lang === 'zh' ? '中' : 'EN'
  29. }}</a-button>
  30. </div>
  31. </template>
  32. <script setup>
  33. import { onMounted, ref } from 'vue'
  34. defineOptions({ name: 'SToggleLanguage' })
  35. defineProps({
  36. // 展示形态:button 按钮 / dropdown 下拉 / 其它为块状
  37. blockType: {
  38. type: String,
  39. default: 'button'
  40. }
  41. })
  42. const lang = ref('')
  43. const isOpen = ref(false)
  44. const locales = [
  45. {
  46. key: 'zh',
  47. name: window.$l('中文')
  48. },
  49. {
  50. key: 'en',
  51. name: 'English'
  52. }
  53. ]
  54. function openChange(e) {
  55. isOpen.value = e
  56. }
  57. function toggleLang(item) {
  58. lang.value = item.key
  59. window.$utils.ls.set('lang', item.key)
  60. window.location.reload()
  61. }
  62. function toggleChange() {
  63. if (lang.value === 'zh') {
  64. lang.value = 'en'
  65. window.$utils.ls.set('lang', 'en')
  66. } else {
  67. lang.value = 'zh'
  68. window.$utils.ls.set('lang', 'zh')
  69. }
  70. window.location.reload()
  71. }
  72. function init() {
  73. // 默认中文
  74. const saved = window.$utils.ls.get('lang')
  75. if (!saved || saved === 'undefined') {
  76. window.$utils.ls.set('lang', window.$utils.env.VITE_APP_LANG)
  77. }
  78. lang.value = window.$utils.env.VITE_APP_LANG_ENABLE ? window.$utils.ls.get('lang') : 'zh'
  79. }
  80. onMounted(() => {
  81. init()
  82. })
  83. <\/script>
  84. <style lang="less" scoped>
  85. .s-toggle-language {
  86. width: 100px;
  87. height: 42px;
  88. display: flex;
  89. justify-content: center;
  90. align-items: center;
  91. // user-select: none;
  92. }
  93. .locale-select {
  94. width: 100px;
  95. height: 42px;
  96. display: flex;
  97. justify-content: center;
  98. align-items: center;
  99. border-radius: 4px;
  100. cursor: pointer;
  101. }
  102. .label {
  103. width: 58px;
  104. text-align: right;
  105. }
  106. .icon {
  107. width: 16px;
  108. height: 16px;
  109. margin: 0 5px;
  110. }
  111. .custom-btn {
  112. padding: 0;
  113. width: 24px;
  114. height: 24px;
  115. color: var(--s-text-color-inverse);
  116. outline: none;
  117. box-shadow: none;
  118. border: none;
  119. background-color: var(--s-text-color);
  120. border-radius: 5px;
  121. display: flex;
  122. justify-content: center;
  123. align-items: center;
  124. }
  125. .custom-btn:hover {
  126. color: var(--s-text-color-inverse);
  127. outline: none;
  128. box-shadow: none;
  129. border: none;
  130. }
  131. .flex-right {
  132. justify-content: flex-end;
  133. }
  134. </style>
  135. `;
  136. export {
  137. n as default
  138. };