mirror of
https://github.com/Powerful-517/yys-editor.git
synced 2026-03-05 15:05:27 +00:00
fix(embed): fallback i18n when host app has no vue-i18n plugin
This commit is contained in:
@@ -116,13 +116,13 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { reactive, onMounted } from 'vue';
|
import { reactive, onMounted } from 'vue';
|
||||||
import { useI18n } from 'vue-i18n';
|
|
||||||
import updateLogs from "../data/updateLog.json"
|
import updateLogs from "../data/updateLog.json"
|
||||||
import { useFilesStore } from "@/ts/useStore";
|
import { useFilesStore } from "@/ts/useStore";
|
||||||
import { ElMessageBox } from "element-plus";
|
import { ElMessageBox } from "element-plus";
|
||||||
import { useGlobalMessage } from "@/ts/useGlobalMessage";
|
import { useGlobalMessage } from "@/ts/useGlobalMessage";
|
||||||
import { getLogicFlowInstance } from "@/ts/useLogicFlow";
|
import { getLogicFlowInstance } from "@/ts/useLogicFlow";
|
||||||
import { useCanvasSettings } from '@/ts/useCanvasSettings';
|
import { useCanvasSettings } from '@/ts/useCanvasSettings';
|
||||||
|
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||||
import type { Pinia } from 'pinia';
|
import type { Pinia } from 'pinia';
|
||||||
|
|
||||||
const props = withDefaults(defineProps<{
|
const props = withDefaults(defineProps<{
|
||||||
@@ -136,8 +136,15 @@ const filesStore = props.piniaInstance ? useFilesStore(props.piniaInstance) : us
|
|||||||
const { showMessage } = useGlobalMessage();
|
const { showMessage } = useGlobalMessage();
|
||||||
const { selectionEnabled, snapGridEnabled, snaplineEnabled } = useCanvasSettings();
|
const { selectionEnabled, snapGridEnabled, snaplineEnabled } = useCanvasSettings();
|
||||||
|
|
||||||
// 获取当前的 i18n 实例
|
const { t } = useSafeI18n({
|
||||||
const {t} = useI18n();
|
import: '导入',
|
||||||
|
export: '导出',
|
||||||
|
prepareCapture: '准备截图',
|
||||||
|
setWatermark: '设置水印',
|
||||||
|
loadExample: '加载样例',
|
||||||
|
updateLog: '更新日志',
|
||||||
|
feedback: '问题反馈'
|
||||||
|
});
|
||||||
|
|
||||||
// 定义响应式数据
|
// 定义响应式数据
|
||||||
const state = reactive({
|
const state = reactive({
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ import draggable from 'vuedraggable';
|
|||||||
import ShikigamiSelect from './flow/nodes/yys/ShikigamiSelect.vue';
|
import ShikigamiSelect from './flow/nodes/yys/ShikigamiSelect.vue';
|
||||||
import ShikigamiProperty from './flow/nodes/yys/ShikigamiProperty.vue';
|
import ShikigamiProperty from './flow/nodes/yys/ShikigamiProperty.vue';
|
||||||
import html2canvas from 'html2canvas';
|
import html2canvas from 'html2canvas';
|
||||||
import {useI18n} from 'vue-i18n'
|
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||||
import { Quill, QuillEditor } from '@vueup/vue-quill'
|
import { Quill, QuillEditor } from '@vueup/vue-quill'
|
||||||
import '@vueup/vue-quill/dist/vue-quill.bubble.css'
|
import '@vueup/vue-quill/dist/vue-quill.bubble.css'
|
||||||
import '@vueup/vue-quill/dist/vue-quill.snow.css' // 引入样式文件
|
import '@vueup/vue-quill/dist/vue-quill.snow.css' // 引入样式文件
|
||||||
@@ -146,7 +146,7 @@ const clipboard = ref('');
|
|||||||
const { showMessage } = useGlobalMessage();
|
const { showMessage } = useGlobalMessage();
|
||||||
|
|
||||||
// 获取当前的 i18n 实例
|
// 获取当前的 i18n 实例
|
||||||
const {t} = useI18n()
|
const { t } = useSafeI18n()
|
||||||
|
|
||||||
const copy = (str) => {
|
const copy = (str) => {
|
||||||
clipboard.value = str
|
clipboard.value = str
|
||||||
|
|||||||
@@ -84,11 +84,11 @@ import shikigami from "../data/Shikigami.json"
|
|||||||
import ShikigamiSelect from "@/components/flow/nodes/yys/ShikigamiSelect.vue";
|
import ShikigamiSelect from "@/components/flow/nodes/yys/ShikigamiSelect.vue";
|
||||||
import ShikigamiProperty from "@/components/flow/nodes/yys/ShikigamiProperty.vue";
|
import ShikigamiProperty from "@/components/flow/nodes/yys/ShikigamiProperty.vue";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import {useI18n} from "vue-i18n";
|
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||||
import draggable from 'vuedraggable';
|
import draggable from 'vuedraggable';
|
||||||
|
|
||||||
|
|
||||||
const {t} = useI18n()
|
const { t } = useSafeI18n()
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
groups: any[];
|
groups: any[];
|
||||||
|
|||||||
@@ -238,12 +238,12 @@
|
|||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, watch, computed } from 'vue';
|
import { ref, watch, computed } from 'vue';
|
||||||
import { useI18n } from 'vue-i18n';
|
|
||||||
import { CirclePlus } from '@element-plus/icons-vue';
|
import { CirclePlus } from '@element-plus/icons-vue';
|
||||||
|
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||||
// import YuhunSelect from "@/components/flow/nodes/yys/YuhunSelect.vue";
|
// import YuhunSelect from "@/components/flow/nodes/yys/YuhunSelect.vue";
|
||||||
|
|
||||||
// 获取当前的 i18n 实例
|
// 获取当前的 i18n 实例
|
||||||
const { t } = useI18n();
|
const { t } = useSafeI18n();
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
currentProperty: {
|
currentProperty: {
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ import {ref, reactive, toRefs, nextTick} from 'vue';
|
|||||||
import ShikigamiSelect from './ShikigamiSelect.vue';
|
import ShikigamiSelect from './ShikigamiSelect.vue';
|
||||||
import ShikigamiProperty from './ShikigamiProperty.vue';
|
import ShikigamiProperty from './ShikigamiProperty.vue';
|
||||||
import html2canvas from 'html2canvas';
|
import html2canvas from 'html2canvas';
|
||||||
import {useI18n} from 'vue-i18n'
|
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||||
import { Quill, QuillEditor } from '@vueup/vue-quill'
|
import { Quill, QuillEditor } from '@vueup/vue-quill'
|
||||||
import '@vueup/vue-quill/dist/vue-quill.bubble.css'
|
import '@vueup/vue-quill/dist/vue-quill.bubble.css'
|
||||||
import '@vueup/vue-quill/dist/vue-quill.snow.css' // 引入样式文件
|
import '@vueup/vue-quill/dist/vue-quill.snow.css' // 引入样式文件
|
||||||
@@ -147,7 +147,7 @@ const dialogTableVisible = ref(false)
|
|||||||
const {showMessage} = useGlobalMessage();
|
const {showMessage} = useGlobalMessage();
|
||||||
|
|
||||||
// 获取当前的 i18n 实例
|
// 获取当前的 i18n 实例
|
||||||
const {t} = useI18n()
|
const { t } = useSafeI18n()
|
||||||
|
|
||||||
// 定义 QuillEditor 的 ref
|
// 定义 QuillEditor 的 ref
|
||||||
const shortDescriptionEditor = ref<InstanceType<typeof QuillEditor>>()
|
const shortDescriptionEditor = ref<InstanceType<typeof QuillEditor>>()
|
||||||
|
|||||||
@@ -127,12 +127,12 @@ import propertyData from "../../../../data/property.json";
|
|||||||
import {ref, watch, computed} from 'vue'
|
import {ref, watch, computed} from 'vue'
|
||||||
import ShikigamiSelect from "@/components/flow/nodes/yys/ShikigamiSelect.vue";
|
import ShikigamiSelect from "@/components/flow/nodes/yys/ShikigamiSelect.vue";
|
||||||
import YuhunSelect from "@/components/flow/nodes/yys/YuhunSelect.vue";
|
import YuhunSelect from "@/components/flow/nodes/yys/YuhunSelect.vue";
|
||||||
import {useI18n} from 'vue-i18n'
|
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||||
// import YuhunSelect from "./YuhunSelect.vue";
|
// import YuhunSelect from "./YuhunSelect.vue";
|
||||||
|
|
||||||
|
|
||||||
// 获取当前的 i18n 实例
|
// 获取当前的 i18n 实例
|
||||||
const {t} = useI18n()
|
const { t } = useSafeI18n()
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
currentShikigami: {
|
currentShikigami: {
|
||||||
|
|||||||
22
src/ts/useSafeI18n.ts
Normal file
22
src/ts/useSafeI18n.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { useI18n } from 'vue-i18n'
|
||||||
|
|
||||||
|
type TranslateFn = (key: string, ...args: any[]) => string
|
||||||
|
|
||||||
|
export function useSafeI18n(fallbackMap: Record<string, string> = {}) {
|
||||||
|
let safeT: TranslateFn = (key) => fallbackMap[key] ?? key
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { t } = useI18n()
|
||||||
|
safeT = (key: string, ...args: any[]) => {
|
||||||
|
const translated = t(key, ...args)
|
||||||
|
if (typeof translated === 'string' && translated.length > 0) {
|
||||||
|
return translated
|
||||||
|
}
|
||||||
|
return fallbackMap[key] ?? key
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// The host app may not install vue-i18n; fallback to key/default text.
|
||||||
|
}
|
||||||
|
|
||||||
|
return { t: safeT }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user