index.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. <link rel="stylesheet" href="./css/index.css" />
  10. <body>
  11. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  12. <div id="app">
  13. <div class="locales-setting">
  14. <div>
  15. <div class="save">
  16. <select v-model="locale" @change="onLocaleChange">
  17. <option value="zh">中文</option>
  18. <option value="en">英文</option>
  19. <option value="kr">韩文</option>
  20. <option value="fr">法语</option>
  21. <option value="ja">日语</option>
  22. </select>
  23. <button @click="onSave">保存</button>
  24. </div>
  25. <aside>
  26. <ul>
  27. <li v-for="menu in menus" @click="onModuleChange(menu.name)" :class="{ active: menu.name == module }">{{ menu.text }}</li>
  28. </ul>
  29. </aside>
  30. <main>
  31. <ul>
  32. <li v-for="locale in locales">
  33. <div>{{ locale.key }}:</div>
  34. <div><input type="text" @input="onChangeVal(locale)" v-model="locale.value" /></div>
  35. </li>
  36. </ul>
  37. </main>
  38. </div>
  39. </div>
  40. <div class="showbox" v-if="loading">
  41. <div class="loader">
  42. <svg class="circular" viewBox="25 25 50 50">
  43. <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" />
  44. </svg>
  45. </div>
  46. </div>
  47. </div>
  48. <script>
  49. const { createApp } = Vue
  50. createApp({
  51. data() {
  52. return {
  53. locale: 'zh',
  54. locales: [],
  55. info: {},
  56. menus: [],
  57. module: 'home',
  58. respone: null,
  59. loading: false,
  60. }
  61. },
  62. methods: {
  63. async onSave() {
  64. this.loading = true
  65. let params = this.respone
  66. fetch('http://192.168.0.130:9091/save', {
  67. method: 'post',
  68. headers: {
  69. 'Content-Type': 'application/x-www-form-urlencoded',
  70. },
  71. body: `q=${JSON.stringify(params)}&locale=${this.locale}`,
  72. })
  73. .then(async res => {
  74. this.loading = false
  75. let json = await res.json()
  76. alert('保存成功')
  77. })
  78. .catch(err => {
  79. this.loading = false
  80. alert('保存失败,请联系管理员')
  81. })
  82. },
  83. onChangeVal(item) {
  84. this.respone[this.module][item.key] = item.value
  85. },
  86. async onLocaleChange() {
  87. this.locales = []
  88. this.respone = await this.fetchLocale()
  89. this.initData(this.respone)
  90. },
  91. onModuleChange(name) {
  92. this.locales = []
  93. this.module = name
  94. this.initData(this.respone)
  95. },
  96. initData(data, init = false) {
  97. for (let key in data) {
  98. if (key.split('.').pop() == 'name') {
  99. if (init) {
  100. this.menus.push({ name: key.split('.')[0], text: data[key] })
  101. }
  102. } else if (typeof data[key] == 'object' && key == this.module) {
  103. this.initData(data[key])
  104. } else if (typeof data[key] == 'string') {
  105. this.locales.push({ key: key.split('.')[0], value: data[key] })
  106. }
  107. }
  108. },
  109. async fetchLocale(locale) {
  110. return await (await fetch(`./locales/${this.locale}.json?${Date.now()}`)).json()
  111. },
  112. },
  113. mounted() {
  114. this.fetchLocale('zh').then(res => {
  115. this.respone = res
  116. this.initData(res, true)
  117. })
  118. },
  119. }).mount('#app')
  120. </script>
  121. </body>
  122. </html>