mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 11:41:17 +02:00
test: language switcher and i18n refactor
This commit is contained in:
parent
19d7ba4905
commit
4651828dd1
10 changed files with 127 additions and 133 deletions
|
@ -1,73 +1,17 @@
|
|||
---
|
||||
// import themeConfig from '@/config'
|
||||
// import { getPagePath } from '@/utils/path'
|
||||
// import { getCollection } from 'astro:content'
|
||||
import { getNextLangUrl } from '@/utils/i18n/lang'
|
||||
|
||||
// // 获取当前页面路径信息
|
||||
// const { isPost } = getPagePath(Astro.url.pathname)
|
||||
// 获取当前路径
|
||||
const currentPath = Astro.url.pathname
|
||||
|
||||
// // 获取默认语言
|
||||
// const defaultLocale = themeConfig.global.locale
|
||||
|
||||
// // 定义固定的语言顺序(按照要求的顺序)
|
||||
// const fixedLangOrder = [defaultLocale, ...themeConfig.global.moreLocale]
|
||||
|
||||
// // 获取当前文章的可用语言
|
||||
// let availableLangs: string[] = []
|
||||
// let currentPostSlug = ''
|
||||
|
||||
// // 如果是文章页,获取当前文章对象
|
||||
// if (isPost) {
|
||||
// // 从URL中提取文章slug
|
||||
// const pathParts = Astro.url.pathname.split('/')
|
||||
// const slugIndex = pathParts.findIndex(part => part === 'posts') + 1
|
||||
// if (slugIndex > 0 && pathParts.length > slugIndex) {
|
||||
// currentPostSlug = pathParts[slugIndex]
|
||||
|
||||
// // 获取所有文章
|
||||
// const posts = await getCollection('posts')
|
||||
|
||||
// // 找到所有具有相同abbrlink或slug的文章
|
||||
// const relatedPosts = posts.filter(post =>
|
||||
// post.data.abbrlink === currentPostSlug || post.slug === currentPostSlug,
|
||||
// )
|
||||
|
||||
// if (relatedPosts.length > 0) {
|
||||
// // 收集所有相关文章支持的语言
|
||||
// const supportedLangs = new Set()
|
||||
|
||||
// relatedPosts.forEach((post) => {
|
||||
// // 处理lang属性
|
||||
// if (typeof post.data.lang === 'string' && post.data.lang.trim() !== '') {
|
||||
// supportedLangs.add(post.data.lang)
|
||||
// }
|
||||
// // 如果没有指定语言,则假定支持默认语言
|
||||
// else {
|
||||
// supportedLangs.add(defaultLocale)
|
||||
// }
|
||||
// })
|
||||
|
||||
// // 按照固定顺序筛选出可用的语言
|
||||
// availableLangs = fixedLangOrder.filter(lang => supportedLangs.has(lang))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// // 非文章页面使用所有语言
|
||||
// availableLangs = fixedLangOrder
|
||||
// }
|
||||
|
||||
// // 当前语言
|
||||
// function getLanguageDisplayName(code: string) {
|
||||
// return new Intl.DisplayNames(['en'], { type: 'language' }).of(code) || code
|
||||
// }
|
||||
// 直接获取下一个语言的URL
|
||||
const nextUrl = getNextLangUrl(currentPath)
|
||||
---
|
||||
|
||||
<button
|
||||
type="button"
|
||||
id="language-switcher"
|
||||
<a
|
||||
href={nextUrl}
|
||||
class="uno-button"
|
||||
aria-label="Click to switch to next language"
|
||||
aria-label="Switch website language"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
|
@ -77,64 +21,4 @@
|
|||
>
|
||||
<path d="M19 21 12.3 2h-1L4.7 21l-2.5.2v.8h6.3v-.8L5.7 21l2-5.9h7.5l2 5.9-3.3.2v.8h7.9v-.8zM8 14.3l3.4-10.1 3.5 10.1z" fill="currentColor" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<!-- <script is:inline define:vars={{ availableLangs, currentPostSlug, isPost, defaultLocale }}>
|
||||
document.addEventListener('astro:page-load', () => {
|
||||
const langSwitch = document.getElementById('language-switcher')
|
||||
|
||||
langSwitch?.addEventListener('click', () => {
|
||||
const { pathname, search, hash } = window.location
|
||||
const segments = pathname.split('/').filter(Boolean)
|
||||
const firstSegment = segments[0] || ''
|
||||
|
||||
// 获取当前语言
|
||||
let currentLang = defaultLocale
|
||||
if (availableLangs.includes(firstSegment) && firstSegment !== defaultLocale) {
|
||||
currentLang = firstSegment
|
||||
}
|
||||
|
||||
// 获取下一个语言
|
||||
const currentIndex = availableLangs.indexOf(currentLang)
|
||||
const nextLang = availableLangs[(currentIndex + 1) % availableLangs.length]
|
||||
|
||||
const newPath = buildNewPath(currentLang, nextLang, segments) || '/'
|
||||
window.location.href = `${newPath}${search}${hash}`
|
||||
})
|
||||
})
|
||||
|
||||
function buildNewPath(currentLang, nextLang, segments) {
|
||||
// 创建一个新的segments数组副本,避免修改原始数组
|
||||
const newSegments = [...segments]
|
||||
const firstSegment = newSegments[0] || ''
|
||||
|
||||
// 检查第一段是否是语言代码(不包括默认语言)
|
||||
const isFirstSegmentLang = availableLangs.includes(firstSegment) && firstSegment !== defaultLocale
|
||||
|
||||
// 处理不同的情况
|
||||
if (nextLang === defaultLocale) {
|
||||
// 如果下一个语言是默认语言,则移除语言段
|
||||
if (isFirstSegmentLang) {
|
||||
newSegments.shift()
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 如果下一个语言不是默认语言
|
||||
if (isFirstSegmentLang) {
|
||||
// 如果第一段是语言代码,则替换它
|
||||
newSegments[0] = nextLang
|
||||
}
|
||||
else {
|
||||
// 如果第一段不是语言代码,则添加新的语言代码
|
||||
newSegments.unshift(nextLang)
|
||||
}
|
||||
}
|
||||
|
||||
// 返回新路径
|
||||
// 处理空路径的特殊情况,避免生成 // 这样的路径
|
||||
if (newSegments.length === 0) {
|
||||
return '/'
|
||||
}
|
||||
return `/${newSegments.join('/')}/`
|
||||
}
|
||||
</script> -->
|
||||
</a>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue