index.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>配置国际化</title>
  8. </head>
  9. <style>
  10. .locales-setting {
  11. position: fixed;
  12. z-index: 20000;
  13. left: 0;
  14. top: 0;
  15. width: 100%;
  16. height: 100%;
  17. background-color: rgba(0, 0, 0, 0.5);
  18. pointer-events: all;
  19. }
  20. .locales-setting > div {
  21. padding: 40px 20px;
  22. display: flex;
  23. align-items: flex-start;
  24. position: absolute;
  25. left: 30px;
  26. top: 30px;
  27. bottom: 30px;
  28. right: 30px;
  29. background: #efefef;
  30. box-shadow: 0 0 8px #666;
  31. }
  32. .locales-setting > div aside {
  33. width: 200px;
  34. height: 100%;
  35. border-right: solid 1px #999;
  36. }
  37. .locales-setting > div aside li {
  38. margin-bottom: 10px;
  39. cursor: pointer;
  40. }
  41. .locales-setting > div aside li.active {
  42. color: #f60;
  43. }
  44. .locales-setting > div main {
  45. flex: 1;
  46. width: 100%;
  47. height: 100%;
  48. overflow: hidden;
  49. overflow-y: auto;
  50. }
  51. .locales-setting > div main li {
  52. display: flex;
  53. align-items: center;
  54. margin-bottom: 10px;
  55. }
  56. .locales-setting > div main li input {
  57. height: 24px;
  58. border: solid 1px #666;
  59. width: 100%;
  60. padding: 0 4px;
  61. }
  62. .locales-setting > div main li > div:first-child {
  63. width: 400px;
  64. /* text-align: right; */
  65. padding-right: 3px;
  66. }
  67. .locales-setting > div main li > div:last-child {
  68. width: 100%;
  69. }
  70. .locales-setting .save {
  71. position: absolute;
  72. right: 5px;
  73. top: 5px;
  74. }
  75. .locales-setting .save select {
  76. border: solid 1px #666;
  77. width: 70px;
  78. height: 24px;
  79. text-align: center;
  80. background: #fff;
  81. }
  82. .locales-setting .save button {
  83. border: solid 1px #666;
  84. width: 70px;
  85. height: 24px;
  86. text-align: center;
  87. background: #fff;
  88. margin-left: 10px;
  89. }
  90. </style>
  91. <body>
  92. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  93. <div id="app">
  94. <div class="locales-setting">
  95. <div>
  96. <div class="save">
  97. <select v-model="locale" @change="onLocaleChange">
  98. <option value="zh">中文</option>
  99. <option value="en">英文</option>
  100. <option value="kr">韩文</option>
  101. <option value="fr">法语</option>
  102. <option value="ja">日语</option>
  103. </select>
  104. <button @click="onSave">保存</button>
  105. </div>
  106. <aside>
  107. <ul>
  108. <li v-for="menu in menus" @click="onModuleChange(menu.name)" :class="{ active: menu.name == module }">{{ menu.text }}</li>
  109. </ul>
  110. </aside>
  111. <main>
  112. <ul>
  113. <li v-for="locale in locales">
  114. <div>{{ locale.key }}:</div>
  115. <div><input type="text" v-model="locale.value" /></div>
  116. </li>
  117. </ul>
  118. </main>
  119. </div>
  120. </div>
  121. </div>
  122. <script>
  123. const { createApp } = Vue
  124. createApp({
  125. data() {
  126. return {
  127. locale: 'zh',
  128. locales: [],
  129. info: {},
  130. menus: [],
  131. module: 'home',
  132. respone: null,
  133. }
  134. },
  135. methods: {
  136. async onLocaleChange() {
  137. this.locales = []
  138. this.respone = await this.fetchLocale()
  139. this.initData(this.respone)
  140. },
  141. onModuleChange(name) {
  142. this.locales = []
  143. this.module = name
  144. console.log(this.module)
  145. console.log(this.respone)
  146. this.initData(this.respone)
  147. },
  148. initData(data, init = false) {
  149. for (let key in data) {
  150. if (key.split('.').pop() == 'name') {
  151. if (init) {
  152. this.menus.push({ name: key.split('.')[0], text: data[key] })
  153. }
  154. } else if (typeof data[key] == 'object' && key == this.module) {
  155. console.log(this.module)
  156. this.initData(data[key])
  157. } else if (typeof data[key] == 'string') {
  158. this.locales.push({ key: key.split('.')[0], value: data[key] })
  159. // if (!this.info[key]) {
  160. // this.info[key] = []
  161. // }
  162. // this.info[key].push({ key: key.split('.')[0], value: data[key] })
  163. // this.locales.push({ key: key.split('.')[0], value: data[key] })
  164. }
  165. }
  166. },
  167. async fetchLocale(locale) {
  168. return await (await fetch(`./locales/${this.locale}.json?${Date.now()}`)).json()
  169. },
  170. },
  171. mounted() {
  172. this.fetchLocale('zh').then(res => {
  173. this.respone = res
  174. this.initData(res, true)
  175. })
  176. // this.onLocaleChange()
  177. },
  178. }).mount('#app')
  179. </script>
  180. </body>
  181. </html>