feat(i18n): page tab's title

This commit is contained in:
kerms 2025-01-18 16:42:08 +01:00
parent b7590d8997
commit 2f4790a57c
2 changed files with 52 additions and 13 deletions

View File

@ -45,4 +45,8 @@ export function setLang(lang: string): void {
} }
} }
export function getLang() {
return i18n.global.locale;
}
export default i18n; export default i18n;

View File

@ -1,5 +1,4 @@
import {createRouter, createWebHistory} from 'vue-router' import {createRouter, createWebHistory, type RouteLocationNormalizedLoaded} from 'vue-router'
import Home from '@/views/Home.vue'
import Wifi from '@/views/Wifi.vue' import Wifi from '@/views/Wifi.vue'
import Feedback from '@/views/Feedback.vue' import Feedback from '@/views/Feedback.vue'
import About from '@/views/About.vue' import About from '@/views/About.vue'
@ -8,7 +7,40 @@ import Page404 from '@/views/404.vue'
import Update from '@/views/Update.vue' import Update from '@/views/Update.vue'
import {translate} from "@/locales"; import {translate} from "@/locales";
import {isOTAEnabled} from "@/composables/buildMode"; import {isOTAEnabled} from "@/composables/buildMode";
import {reactive, watch} from "vue";
import {getLang} from "@/i18n";
const languageState = reactive({
currentLanguage: getLang(), // Get the current language from your i18n setup
});
interface AppRouteMeta {
title?: string;
titleKey?: string;
}
const updateMetaTitles = () => {
router.getRoutes().forEach(route => {
const meta = route.meta as AppRouteMeta;
if (meta.titleKey) {
meta.title = translate(meta.titleKey);
}
});
};
function updateDocumentTitle(route: RouteLocationNormalizedLoaded) {
const meta = route.meta as AppRouteMeta;
document.title = typeof route.meta.title === 'string'
? `${translate(meta.titleKey || "")} | ${translate('studioYunSi')}`
: '允斯调试器';
}
// Watch for language changes to update the titles dynamically
watch(() => languageState.currentLanguage, () => {
// Recompute all route meta titles
updateMetaTitles();
updateDocumentTitle(router.currentRoute.value);
}, {deep: true});
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
@ -16,46 +48,49 @@ const router = createRouter({
{ {
path: '/', path: '/',
name: 'home', name: 'home',
meta: {title: translate("page.home")}, meta: { titleKey: 'page.home' },
// component: Wifi
redirect: () => '/uart', redirect: () => '/uart',
}, { }, {
path: '/home:ext(.*)', path: '/home:ext(.*)',
meta: {title: translate("page.home")}, meta: { titleKey: 'page.home' },
redirect: () => '/', redirect: () => '/',
}, { }, {
path: '/wifi:ext(.*)', path: '/wifi:ext(.*)',
meta: {title: translate('page.wifi')}, meta: { titleKey: 'page.wifi' },
component: Wifi, component: Wifi,
}, { }, {
path: '/about:ext(.*)', path: '/about:ext(.*)',
meta: {title: translate('page.about')}, meta: { titleKey: 'page.about' },
component: About, component: About,
}, { }, {
path: '/uart:ext(.*)', path: '/uart:ext(.*)',
meta: {title: translate('page.uart')}, meta: { titleKey: 'page.uart' },
component: Uart, component: Uart,
}, { }, {
path: '/feedback:ext(.*)', path: '/feedback:ext(.*)',
meta: {title: translate('page.feedback')}, meta: { titleKey: 'page.feedback' },
name: 'feedback', name: 'feedback',
component: Feedback, component: Feedback,
}, { }, {
path: '/update:ext(.*)', path: '/update:ext(.*)',
meta: {title: translate('page.update')}, meta: { titleKey: 'page.update' },
name: 'update', name: 'update',
component: isOTAEnabled() ? Update : Page404, component: isOTAEnabled() ? Update : Page404,
}, { }, {
path: '/:catchAll(.*)', // This will match all paths that aren't matched by above routes path: '/:catchAll(.*)', // Catch-all route for 404
name: 'NotFound', name: 'NotFound',
component: Page404, component: Page404,
}, },
] ]
}) })
// Update document title dynamically
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
document.title = typeof to.meta.title === 'string' ? to.meta.title + " | 允斯工作室" : '允斯调试器'; updateDocumentTitle(to);
next(); next();
}); });
export default router // Initialize titles on load
updateMetaTitles();
export default router;