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">
|
||||
import { reactive, onMounted } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import updateLogs from "../data/updateLog.json"
|
||||
import { useFilesStore } from "@/ts/useStore";
|
||||
import { ElMessageBox } from "element-plus";
|
||||
import { useGlobalMessage } from "@/ts/useGlobalMessage";
|
||||
import { getLogicFlowInstance } from "@/ts/useLogicFlow";
|
||||
import { useCanvasSettings } from '@/ts/useCanvasSettings';
|
||||
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||
import type { Pinia } from 'pinia';
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
@@ -136,8 +136,15 @@ const filesStore = props.piniaInstance ? useFilesStore(props.piniaInstance) : us
|
||||
const { showMessage } = useGlobalMessage();
|
||||
const { selectionEnabled, snapGridEnabled, snaplineEnabled } = useCanvasSettings();
|
||||
|
||||
// 获取当前的 i18n 实例
|
||||
const {t} = useI18n();
|
||||
const { t } = useSafeI18n({
|
||||
import: '导入',
|
||||
export: '导出',
|
||||
prepareCapture: '准备截图',
|
||||
setWatermark: '设置水印',
|
||||
loadExample: '加载样例',
|
||||
updateLog: '更新日志',
|
||||
feedback: '问题反馈'
|
||||
});
|
||||
|
||||
// 定义响应式数据
|
||||
const state = reactive({
|
||||
|
||||
@@ -116,7 +116,7 @@ import draggable from 'vuedraggable';
|
||||
import ShikigamiSelect from './flow/nodes/yys/ShikigamiSelect.vue';
|
||||
import ShikigamiProperty from './flow/nodes/yys/ShikigamiProperty.vue';
|
||||
import html2canvas from 'html2canvas';
|
||||
import {useI18n} from 'vue-i18n'
|
||||
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||
import { Quill, QuillEditor } from '@vueup/vue-quill'
|
||||
import '@vueup/vue-quill/dist/vue-quill.bubble.css'
|
||||
import '@vueup/vue-quill/dist/vue-quill.snow.css' // 引入样式文件
|
||||
@@ -146,7 +146,7 @@ const clipboard = ref('');
|
||||
const { showMessage } = useGlobalMessage();
|
||||
|
||||
// 获取当前的 i18n 实例
|
||||
const {t} = useI18n()
|
||||
const { t } = useSafeI18n()
|
||||
|
||||
const copy = (str) => {
|
||||
clipboard.value = str
|
||||
@@ -645,4 +645,4 @@ defineExpose({
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
@@ -84,11 +84,11 @@ import shikigami from "../data/Shikigami.json"
|
||||
import ShikigamiSelect from "@/components/flow/nodes/yys/ShikigamiSelect.vue";
|
||||
import ShikigamiProperty from "@/components/flow/nodes/yys/ShikigamiProperty.vue";
|
||||
import _ from "lodash";
|
||||
import {useI18n} from "vue-i18n";
|
||||
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||
import draggable from 'vuedraggable';
|
||||
|
||||
|
||||
const {t} = useI18n()
|
||||
const { t } = useSafeI18n()
|
||||
|
||||
const props = defineProps<{
|
||||
groups: any[];
|
||||
@@ -264,4 +264,4 @@ const getYuhunPropertyNames = (yuhun) => {
|
||||
display: block;
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
@@ -238,12 +238,12 @@
|
||||
|
||||
<script setup>
|
||||
import { ref, watch, computed } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { CirclePlus } from '@element-plus/icons-vue';
|
||||
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||
// import YuhunSelect from "@/components/flow/nodes/yys/YuhunSelect.vue";
|
||||
|
||||
// 获取当前的 i18n 实例
|
||||
const { t } = useI18n();
|
||||
const { t } = useSafeI18n();
|
||||
|
||||
const props = defineProps({
|
||||
currentProperty: {
|
||||
@@ -338,4 +338,4 @@ const confirm = () => {
|
||||
border-radius: 4px;
|
||||
border: 1px solid #dcdfe6;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
@@ -112,7 +112,7 @@ import {ref, reactive, toRefs, nextTick} from 'vue';
|
||||
import ShikigamiSelect from './ShikigamiSelect.vue';
|
||||
import ShikigamiProperty from './ShikigamiProperty.vue';
|
||||
import html2canvas from 'html2canvas';
|
||||
import {useI18n} from 'vue-i18n'
|
||||
import { useSafeI18n } from '@/ts/useSafeI18n';
|
||||
import { Quill, QuillEditor } from '@vueup/vue-quill'
|
||||
import '@vueup/vue-quill/dist/vue-quill.bubble.css'
|
||||
import '@vueup/vue-quill/dist/vue-quill.snow.css' // 引入样式文件
|
||||
@@ -147,7 +147,7 @@ const dialogTableVisible = ref(false)
|
||||
const {showMessage} = useGlobalMessage();
|
||||
|
||||
// 获取当前的 i18n 实例
|
||||
const {t} = useI18n()
|
||||
const { t } = useSafeI18n()
|
||||
|
||||
// 定义 QuillEditor 的 ref
|
||||
const shortDescriptionEditor = ref<InstanceType<typeof QuillEditor>>()
|
||||
@@ -624,4 +624,4 @@ defineExpose({
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
@@ -127,12 +127,12 @@ import propertyData from "../../../../data/property.json";
|
||||
import {ref, watch, computed} from 'vue'
|
||||
import ShikigamiSelect from "@/components/flow/nodes/yys/ShikigamiSelect.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";
|
||||
|
||||
|
||||
// 获取当前的 i18n 实例
|
||||
const {t} = useI18n()
|
||||
const { t } = useSafeI18n()
|
||||
|
||||
const props = defineProps({
|
||||
currentShikigami: {
|
||||
@@ -373,4 +373,4 @@ const resetData = () => {
|
||||
const updateSkillRequired = (index, value) => {
|
||||
shikigami.value.skillRequired[index] = value;
|
||||
}
|
||||
</script>
|
||||
</script>
|
||||
|
||||
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