From df597c0328c895174dbc74b80545c50320014ade Mon Sep 17 00:00:00 2001 From: xingyy <64720302+Concur-max@users.noreply.github.com> Date: Thu, 27 Feb 2025 09:23:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(floatingBubble):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=9B=BD=E9=99=85=E5=8C=96=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 floating.js 中集成 vue-i18n - 从 nuxt/app 导入 useNuxtApp 替代 useI18n - 更新 index.vue 中的国际化使用方式 --- app/components/floatingBubble/floating.js | 16 ++++++++++++++++ app/components/floatingBubble/index.vue | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/components/floatingBubble/floating.js b/app/components/floatingBubble/floating.js index 111ffa8..c68d333 100644 --- a/app/components/floatingBubble/floating.js +++ b/app/components/floatingBubble/floating.js @@ -4,6 +4,7 @@ */ import { createApp } from 'vue' import MinWindow from '@/components/floatingBubble/index.vue' +import { createI18n } from 'vue-i18n' // 全局单例状态管理 let minWindowInstance = null // 组件实例引用 @@ -39,6 +40,21 @@ export const showMinWindow1 = (props = {}) => { // 创建Vue应用实例 const app = createApp(MinWindow, props) + + // 获取当前 Nuxt 应用的 i18n 配置 + const nuxtApp = window?.__nuxt + const i18nConfig = nuxtApp?.$i18n?.options || { + legacy: false, + locale: 'en', + messages: {} + } + + // 为独立组件创建 i18n 实例 + const i18n = createI18n(i18nConfig) + + // 安装 i18n + app.use(i18n) + minWindowApp = app minWindowInstance = app.mount(container) diff --git a/app/components/floatingBubble/index.vue b/app/components/floatingBubble/index.vue index c9087e0..ad029ed 100644 --- a/app/components/floatingBubble/index.vue +++ b/app/components/floatingBubble/index.vue @@ -6,9 +6,12 @@ import { watch, onUnmounted } from 'vue' import { hideMinWindow1 } from './floating' -const { t } = useI18n() +// 从 nuxt/app 导入 useNuxtApp +const { $i18n } = useNuxtApp() + +// 替换原来的 useI18n +const t = (key) => $i18n.t(key) -// 组件属性定义 const props = defineProps({ /** 点击气泡时的回调函数 */ onClick: {