Bladeren bron

feat: 把i18n的东西放到 i18n.vue

菲鸽 2 jaren geleden
bovenliggende
commit
960580fd62
3 gewijzigde bestanden met toevoegingen van 14 en 12 verwijderingen
  1. 4 0
      src/locales/index.ts
  2. 8 2
      src/pages/index/i18n.vue
  3. 2 10
      src/pages/index/index.vue

+ 4 - 0
src/locales/index.ts

@@ -29,6 +29,10 @@ const i18n = createI18n({
  * @param { string } localeKey 多语言的key,eg: "app.name"
  */
 export const translate = (localeKey: string) => {
+  if (!localeKey) {
+    console.error(`[i18n] Function translate(), localeKey param is required`)
+    return ''
+  }
   const locale = getLocale()
   const message = messages[locale]
   if (Object.keys(message).includes(localeKey)) {

+ 8 - 2
src/pages/index/i18n.vue

@@ -1,5 +1,3 @@
-<template>{{ $t('app.name') }}</template>
-
 <route lang="json">
 {
   "style": {
@@ -7,3 +5,11 @@
   }
 }
 </route>
+
+<template>{{ $t('app.name') }}</template>
+
+<script lang="ts" setup>
+import { testI18n } from '@/utils/index'
+
+testI18n()
+</script>

+ 2 - 10
src/pages/index/index.vue

@@ -2,8 +2,8 @@
 <route lang="json5" type="home">
 {
   style: {
-    // "navigationStyle": "custom",
-    navigationBarTitleText: '%app.name%',
+    navigationStyle: 'custom',
+    navigationBarTitleText: '首页',
   },
 }
 </route>
@@ -12,7 +12,6 @@
     class="bg-white h-full overflow-hidden pt-2 px-4"
     :style="{ marginTop: safeAreaInsets?.top + 'px' }"
   >
-    <view>{{ $t('app.name') }}</view>
     <view class="mt-12">
       <image src="/static/logo.svg" alt="" class="w-40 h-40 block mx-auto" />
     </view>
@@ -38,15 +37,8 @@
 </template>
 
 <script lang="ts" setup>
-import { testI18n } from '@/utils/index'
-
-testI18n()
-
 // 获取屏幕边界到安全区域距离
 const { safeAreaInsets } = uni.getSystemInfoSync()
-
-const author = ref('菲鸽')
-console.log(author)
 </script>
 
 <style>