Fix: 修正国际化的显示、跳转问题

This commit is contained in:
2025-08-16 15:40:08 +08:00
parent daa91ac56f
commit eaf7d3be38
8 changed files with 94 additions and 37 deletions

View File

@ -10,19 +10,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { ElConfigProvider } from 'element-plus'; import { ElConfigProvider } from 'element-plus';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import en from 'element-plus/es/locale/lang/en';
const { login } = useStrapiAuth(); const { login } = useStrapiAuth();
const { locale } = useI18n(); const { getElementPlusLocale } = useLocalizations();
const elementPlusLocales = { const elementPlusLocale = getElementPlusLocale();
'zh': zhCn,
'en': en,
}
const elementPlusLocale = computed(() => elementPlusLocales[locale.value] || zhCn);
onMounted(() => { onMounted(() => {
// 检查用户是否已登录 // 检查用户是否已登录

View File

@ -17,19 +17,19 @@
<h4>{{ $t('quick-links') }}</h4> <h4>{{ $t('quick-links') }}</h4>
<ul class="footer-links"> <ul class="footer-links">
<li> <li>
<NuxtLink to="/">{{ $t('navigation.home') }}</NuxtLink> <NuxtLinkLocale to="/">{{ $t('navigation.home') }}</NuxtLinkLocale>
</li> </li>
<li> <li>
<NuxtLink to="/productions">{{ $t('productions') }}</NuxtLink> <NuxtLink :to="$localePath('/productions')">{{ $t('productions') }}</NuxtLink>
</li> </li>
<li> <li>
<NuxtLink to="/solutions">{{ $t('solutions') }}</NuxtLink> <NuxtLink :to="$localePath('/solutions')">{{ $t('solutions') }}</NuxtLink>
</li> </li>
<li> <li>
<NuxtLink to="/support">{{ $t('support') }}</NuxtLink> <NuxtLink :to="$localePath('/support')">{{ $t('support') }}</NuxtLink>
</li> </li>
<li> <li>
<NuxtLink to="/about">{{ $t('about-us') }}</NuxtLink> <NuxtLink :to="$localePath('/about')">{{ $t('about-us') }}</NuxtLink>
</li> </li>
</ul> </ul>
</div> </div>
@ -79,11 +79,11 @@
<p>备案号: 浙ICP备12003709号-5</p> <p>备案号: 浙ICP备12003709号-5</p>
</div> </div>
<div class="footer-links-bottom"> <div class="footer-links-bottom">
<NuxtLink to="/privacy">{{ $t('privacy-policy') }}</NuxtLink> <NuxtLink :to="$localePath('/privacy')">{{ $t('privacy-policy') }}</NuxtLink>
<span class="separator">|</span> <span class="separator">|</span>
<NuxtLink to="/terms">{{ $t('terms-of-service') }}</NuxtLink> <NuxtLink :to="$localePath('/terms')">{{ $t('terms-of-service') }}</NuxtLink>
<span class="separator">|</span> <span class="separator">|</span>
<NuxtLink to="/sitemap">{{ $t('sitemap') }}</NuxtLink> <NuxtLink :to="$localePath('/sitemap')">{{ $t('sitemap') }}</NuxtLink>
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="header-container"> <div class="header-container">
<div class="logo-section"> <div class="logo-section">
<NuxtLink to="/" class="logo-link"> <NuxtLink :to="$localePath('/')" class="logo-link">
<el-image class="website-logo" src="/jinshen-logo.png" alt="Jinshen Logo" fit="contain" /> <el-image class="website-logo" src="/jinshen-logo.png" alt="Jinshen Logo" fit="contain" />
</NuxtLink> </NuxtLink>
</div> </div>
@ -9,16 +9,16 @@
<!-- 导航菜单 --> <!-- 导航菜单 -->
<el-menu default-active="productions" class="header-menu" mode="horizontal" :ellipsis="false" router> <el-menu default-active="productions" class="header-menu" mode="horizontal" :ellipsis="false" router>
<el-menu-item index="/productions"> <el-menu-item :index="$localePath('/productions')">
<span class="title">{{ $t('productions') }}</span> <span class="title">{{ $t('productions') }}</span>
</el-menu-item> </el-menu-item>
<el-menu-item index="/solutions"> <el-menu-item :index="$localePath('/solutions')">
<span class="title">{{ $t('solutions') }}</span> <span class="title">{{ $t('solutions') }}</span>
</el-menu-item> </el-menu-item>
<el-menu-item index="/support"> <el-menu-item :index="$localePath('/support')">
<span class="title">{{ $t('support') }}</span> <span class="title">{{ $t('support') }}</span>
</el-menu-item> </el-menu-item>
<el-menu-item index="/about"> <el-menu-item :index="$localePath('/about')">
<span class="title">{{ $t('about-us') }}</span> <span class="title">{{ $t('about-us') }}</span>
</el-menu-item> </el-menu-item>
</el-menu> </el-menu>

View File

@ -14,6 +14,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
interface Props { interface Props {
name: string name: string
description: string description: string
@ -23,13 +24,13 @@ interface Props {
} }
const props = defineProps<Props>() const props = defineProps<Props>()
const router = useRouter() const localePath = useLocalePath()
const handleClick = () => { const handleClick = () => {
// 优先使用 slug如果没有则使用 id // 优先使用 slug如果没有则使用 id
const routeParam = props.slug || props.id const routeParam = props.slug || props.id
if (routeParam) { if (routeParam) {
router.push(`/productions/${routeParam}`) navigateTo(localePath(`/productions/${routeParam}`))
} }
} }
</script> </script>

View File

@ -0,0 +1,39 @@
import type { StrapiLocale } from '@nuxtjs/strapi';
import type { Language as ElementLanguage } from 'element-plus/es/locale';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import en from 'element-plus/es/locale/lang/en';
// Strapi本地化映射
export const strapiLocales: Record<string, StrapiLocale> = {
'zh': 'zh-Hans',
'en': 'en'
}
// Element Plus本地化映射
export const elementPlusLocales: Record<string, ElementLanguage> = {
'zh': zhCn,
'en': en
}
export const useLocalizations = () => {
const { locale } = useI18n();
// 获取Strapi本地化代码
const getStrapiLocale = (nuxtLocale?: string): StrapiLocale => {
const currentLocale = nuxtLocale || locale.value;
return strapiLocales[currentLocale] || 'zh-Hans';
}
// 获取Element Plus本地化
const getElementPlusLocale = (nuxtLocale?: string) => {
const currentLocale = nuxtLocale || locale.value;
const elementPlusLocale = elementPlusLocales[currentLocale] || elementPlusLocales['zh'];
return elementPlusLocale;
}
return {
locale: readonly(locale),
getStrapiLocale,
getElementPlusLocale,
}
}

View File

@ -28,7 +28,7 @@
<markdown-renderer :content="production.production_details || ''" /> <markdown-renderer :content="production.production_details || ''" />
</el-tab-pane> </el-tab-pane>
<el-tab-pane label="技术规格" name="specs"> <el-tab-pane label="技术规格" name="specs">
<spec-table :data="production.production_specs" /> <spec-table :data="parsedSpecs" />
</el-tab-pane> </el-tab-pane>
<el-tab-pane label="相关文档" name="documents" /> <el-tab-pane label="相关文档" name="documents" />
</el-tabs> </el-tabs>
@ -37,7 +37,12 @@
<!-- 加载状态 --> <!-- 加载状态 -->
<div v-else-if="pending" class="loading"> <div v-else-if="pending" class="loading">
{{ $t('loading') }} <el-skeleton style="--el-skeleton-circle-size: 400px">
<template #template>
<el-skeleton-item variant="circle" />
</template>
</el-skeleton>
<el-skeleton :rows="5" animated />
</div> </div>
<!-- 未找到产品 --> <!-- 未找到产品 -->
@ -69,6 +74,9 @@ interface ProductionDetails {
const route = useRoute() const route = useRoute()
const { findOne } = useStrapi() const { findOne } = useStrapi()
const { getStrapiLocale } = useLocalizations()
const strapiLocale = getStrapiLocale()
const production = ref<ProductionDetails | null>(null) const production = ref<ProductionDetails | null>(null)
const pending = ref(true) const pending = ref(true)
@ -78,11 +86,29 @@ const activeName = ref('details') // 默认选中概览标签
// 获取路由参数slug 或 id // 获取路由参数slug 或 id
const productionParam = computed(() => route.params.slug as string) const productionParam = computed(() => route.params.slug as string)
const parsedSpecs = computed(() => {
if (!production.value?.production_specs) return JSON.parse('{}')
const specs = production.value.production_specs
if (typeof specs === 'string') {
try {
return JSON.parse(specs)
} catch (error) {
console.error('Failed to parse production_specs:', error)
return specs
}
}
return specs
})
onMounted(async () => { onMounted(async () => {
try { try {
const response = await findOne<ProductionDetails>('productions', productionParam.value, { const response = await findOne<ProductionDetails>('productions', productionParam.value, {
populate: '*', populate: '*',
locale: strapiLocale,
}) })
if (response.data) { if (response.data) {
@ -176,7 +202,7 @@ useHead({
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
height: 300px; margin-top: 1rem;
} }
.not-found { .not-found {

View File

@ -15,10 +15,10 @@
<script setup lang="ts"> <script setup lang="ts">
import type { StrapiLocale } from '@nuxtjs/strapi'
const { find } = useStrapi() const { find } = useStrapi()
const { locale: i18nLocale } = useI18n() const { getStrapiLocale } = useLocalizations()
const strapiLocale = getStrapiLocale()
const baseUrl = 'http://192.168.86.5:1337'; const baseUrl = 'http://192.168.86.5:1337';
@ -48,18 +48,13 @@ class ProductionInfo {
} }
} }
const strapiLocales = {
'zh': 'zh-Hans' as StrapiLocale, // 简体中文
'en': 'en' as StrapiLocale // 英文
};
const productions = ref<ProductionInfo[]>(); const productions = ref<ProductionInfo[]>();
onMounted(async () => { onMounted(async () => {
try { try {
const response = await find<Production>('productions', { const response = await find<Production>('productions', {
populate: '*', populate: '*',
locale: strapiLocales[i18nLocale.value], // 使用简体中文 locale: strapiLocale, // 使用简体中文
}) })
productions.value = response.data.map((item: Production) => { productions.value = response.data.map((item: Production) => {

View File

@ -59,12 +59,14 @@ export default defineNuxtConfig({
useCookie: true, useCookie: true,
cookieKey: "i18n_redirected", cookieKey: "i18n_redirected",
redirectOn: "root", redirectOn: "root",
alwaysRedirect: true
}, },
locales: [ locales: [
{ code: "en", language: "en-US", name: "English", file: "en.json" }, { code: "en", language: "en-US", name: "English", file: "en.json" },
{ code: "zh", language: "zh-CN", name: "简体中文", file: "zh.json" }, { code: "zh", language: "zh-CN", name: "简体中文", file: "zh.json" },
], ],
defaultLocale: "zh", defaultLocale: "zh",
strategy: "prefix_except_default",
langDir: "locales", langDir: "locales",
}, },