From 44d67569206d7d33111e9de4b6c0d0db8e3f64ac Mon Sep 17 00:00:00 2001 From: Zhenye Dong Date: Tue, 5 Nov 2024 00:09:46 +0800 Subject: [PATCH 1/5] fix: language switching now strictly follows URL path - Remove localStorage dependency for language persistence - Add route change listener for language updates - Simplify language switching logic - Fix language reset issue when refreshing /en pages --- lib/global.js | 28 +++++++++++++++++++++++++++- lib/lang.js | 31 +++++++++---------------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/lib/global.js b/lib/global.js index f526e165476..8f8850f30d4 100644 --- a/lib/global.js +++ b/lib/global.js @@ -87,9 +87,35 @@ export function GlobalContextProvider(props) { } } + // 添加路由变化时的语言处理 + useEffect(() => { + const handleRouteChange = (url) => { + // 从路径中提取语言前缀 + const pathSegments = url.split('/') + const pathLang = pathSegments[1] + + // 检查是否是有效的语言路径 + if (pathLang === 'en' || pathLang === 'zh') { + const targetLang = pathLang === 'en' ? 'en-US' : 'zh-CN' + + // 直接更新语言,不使用 localStorage + updateLang(targetLang) + updateLocale(generateLocaleDict(targetLang)) + } + } + + // 初始化时处理当前路径 + handleRouteChange(router.asPath) + + // 监听路由变化 + router.events.on('routeChangeComplete', handleRouteChange) + return () => { + router.events.off('routeChangeComplete', handleRouteChange) + } + }, [router]) + useEffect(() => { initDarkMode(updateDarkMode, defaultDarkMode) - initLocale(lang, locale, updateLang, updateLocale) if ( NOTION_CONFIG?.REDIRECT_LANG && JSON.parse(NOTION_CONFIG?.REDIRECT_LANG) diff --git a/lib/lang.js b/lib/lang.js index 0436b80ff2c..ac2f06821a8 100644 --- a/lib/lang.js +++ b/lib/lang.js @@ -70,34 +70,21 @@ export function generateLocaleDict(langString) { */ export function initLocale(lang, locale, changeLang, changeLocale) { if (isBrowser) { - // 用户请求的语言 - let queryLang = - getQueryVariable('locale') || - getQueryVariable('lang') || - loadLangFromLocalStorage() + // 只使用 URL 参数,忽略 localStorage + const queryLang = getQueryVariable('locale') || getQueryVariable('lang') if (queryLang) { - // 用正则表达式匹配有效的语言标识符例如zh-CN(可选的 -CN 部分) - queryLang = queryLang.match(/[a-zA-Z]{2}(?:-[a-zA-Z]{2})?/) - if (queryLang) { - queryLang = queryLang[0] + const match = queryLang.match(/[a-zA-Z]{2}(?:-[a-zA-Z]{2})?/) + if (match) { + const targetLang = match[0] + changeLang(targetLang) + const targetLocale = generateLocaleDict(targetLang) + changeLocale(targetLocale) } } - - let currentLang = lang - if (queryLang && queryLang !== lang) { - currentLang = queryLang - } - - changeLang(currentLang) - saveLangToLocalStorage(currentLang) - - const targetLocale = generateLocaleDict(currentLang) - if (JSON.stringify(locale) !== JSON.stringify(currentLang)) { - changeLocale(targetLocale) - } } } + /** * 读取语言 * @returns {*} From 1db272ecef5de6472a56fcee0a1dc4c75019e14b Mon Sep 17 00:00:00 2001 From: Biliko Date: Tue, 5 Nov 2024 14:07:54 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=9B=BF=E6=8D=A2Twikoo=E9=BB=98=E8=AE=A4C?= =?UTF-8?q?DN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit staticfile.org等CDN服务因恶意攻击代码被uBlock屏蔽,替换默认CDN为Twikoo官方文档中的地址 --- blog.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blog.config.js b/blog.config.js index a6e8d16ef0e..772cae9b671 100644 --- a/blog.config.js +++ b/blog.config.js @@ -353,7 +353,7 @@ const BLOG = { process.env.NEXT_PUBLIC_COMMENT_TWIKOO_COUNT_ENABLE || false, // 博客列表是否显示评论数 COMMENT_TWIKOO_CDN_URL: process.env.NEXT_PUBLIC_COMMENT_TWIKOO_CDN_URL || - 'https://cdn.staticfile.net/twikoo/1.6.17/twikoo.min.js', // twikoo客户端cdn + 'https://cdn.jsdelivr.net/npm/twikoo@1.6.17/dist/twikoo.all.min.js', // twikoo客户端cdn // utterance COMMENT_UTTERRANCES_REPO: From 6010bd22189b1047f67ae1efb33d244ce43c8718 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 8 Nov 2024 16:21:43 +0800 Subject: [PATCH 3/5] =?UTF-8?q?Starter=20=E9=A1=B5=E8=84=9A=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E6=96=87=E7=AB=A0=E6=95=B0=E9=87=8F=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/starter/components/Footer.js | 7 +++++-- themes/starter/components/Team.js | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/themes/starter/components/Footer.js b/themes/starter/components/Footer.js index 9e212ddf398..ab145c0bb16 100644 --- a/themes/starter/components/Footer.js +++ b/themes/starter/components/Footer.js @@ -5,8 +5,11 @@ import { SVGFooterCircleBG } from './svg/SVGFooterCircleBG' /* eslint-disable @next/next/no-img-element */ export const Footer = props => { - const latestPosts = props?.latestPosts ? props?.latestPosts.slice(0, 2) : [] - const STARTER_FOOTER_LINK_GROUP = siteConfig('STARTER_FOOTER_LINK_GROUP') + const footerPostCount = siteConfig('STARTER_FOOTER_POST_COUNT', 2) + const latestPosts = props?.latestPosts + ? props?.latestPosts.slice(0, footerPostCount) + : [] + const STARTER_FOOTER_LINK_GROUP = siteConfig('STARTER_FOOTER_LINK_GROUP', []) return ( <> {/* */} diff --git a/themes/starter/components/Team.js b/themes/starter/components/Team.js index 69eda8a6b09..facc53fd41b 100644 --- a/themes/starter/components/Team.js +++ b/themes/starter/components/Team.js @@ -3,7 +3,7 @@ import { siteConfig } from '@/lib/config' import { SVGAvatarBG } from './svg/SVGAvatarBG' export const Team = () => { - const STARTER_TEAM_ITEMS = siteConfig('STARTER_TEAM_ITEMS') + const STARTER_TEAM_ITEMS = siteConfig('STARTER_TEAM_ITEMS', []) return ( <> {/* */} From 4d7b0f550da54f9047b56abae5267e7f6011eaa0 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 8 Nov 2024 17:03:23 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=A0=B9=E6=8D=AEurl=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E5=89=8D=E7=BC=80=EF=BC=8C=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=AB=99=E7=82=B9=E6=98=BE=E7=A4=BA=E7=9A=84=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/global.js | 24 +----------------------- lib/lang.js | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/lib/global.js b/lib/global.js index 8f8850f30d4..a42971878bd 100644 --- a/lib/global.js +++ b/lib/global.js @@ -89,29 +89,7 @@ export function GlobalContextProvider(props) { // 添加路由变化时的语言处理 useEffect(() => { - const handleRouteChange = (url) => { - // 从路径中提取语言前缀 - const pathSegments = url.split('/') - const pathLang = pathSegments[1] - - // 检查是否是有效的语言路径 - if (pathLang === 'en' || pathLang === 'zh') { - const targetLang = pathLang === 'en' ? 'en-US' : 'zh-CN' - - // 直接更新语言,不使用 localStorage - updateLang(targetLang) - updateLocale(generateLocaleDict(targetLang)) - } - } - - // 初始化时处理当前路径 - handleRouteChange(router.asPath) - - // 监听路由变化 - router.events.on('routeChangeComplete', handleRouteChange) - return () => { - router.events.off('routeChangeComplete', handleRouteChange) - } + initLocale(router.locale, changeLang, updateLocale) }, [router]) useEffect(() => { diff --git a/lib/lang.js b/lib/lang.js index ac2f06821a8..9eda4da9cff 100644 --- a/lib/lang.js +++ b/lib/lang.js @@ -65,13 +65,19 @@ export function generateLocaleDict(langString) { } /** - * 初始化站点翻译 - * 根据用户当前浏览器语言进行切换 + * 站点翻译 + * 借助router中的locale机制,根据locale自动切换对应的语言 */ -export function initLocale(lang, locale, changeLang, changeLocale) { +export function initLocale(locale, changeLang, updateLocale) { if (isBrowser) { - // 只使用 URL 参数,忽略 localStorage - const queryLang = getQueryVariable('locale') || getQueryVariable('lang') + // 根据router中的locale对象判断当前语言:表现为前缀中包含 zh、en 等。 + let pathLocaleLang = null + if (locale === 'en' || locale === 'zh') { + pathLocaleLang = locale === 'en' ? 'en-US' : 'zh-CN' + } + // 如果有query参数切换语言则优先 + const queryLang = + getQueryVariable('locale') || getQueryVariable('lang') || pathLocaleLang if (queryLang) { const match = queryLang.match(/[a-zA-Z]{2}(?:-[a-zA-Z]{2})?/) @@ -79,7 +85,7 @@ export function initLocale(lang, locale, changeLang, changeLocale) { const targetLang = match[0] changeLang(targetLang) const targetLocale = generateLocaleDict(targetLang) - changeLocale(targetLocale) + updateLocale(targetLocale) } } } From c2d932402ca6c6cc257f4ff79fd83adae705a35b Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 8 Nov 2024 18:38:57 +0800 Subject: [PATCH 5/5] =?UTF-8?q?starter=E4=B8=BB=E9=A2=98logo=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/starter/components/Header.js | 4 ++-- themes/starter/components/Logo.js | 14 +++++++++----- themes/starter/index.js | 3 ++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/themes/starter/components/Header.js b/themes/starter/components/Header.js index e8f0c7f24b6..ef69c077a69 100644 --- a/themes/starter/components/Header.js +++ b/themes/starter/components/Header.js @@ -33,7 +33,7 @@ export const Header = props => { return () => { window.removeEventListener('scroll', navBarScollListener) } - }, [[isDarkMode]]) + }, [isDarkMode]) // 滚动监听 const throttleMs = 200 @@ -58,7 +58,7 @@ export const Header = props => {
{/* Logo */} - +
{/* 中间菜单 */} diff --git a/themes/starter/components/Logo.js b/themes/starter/components/Logo.js index e290d4d9948..c73cd4ceef6 100644 --- a/themes/starter/components/Logo.js +++ b/themes/starter/components/Logo.js @@ -10,10 +10,12 @@ import { useEffect, useState } from 'react' * 站点图标 * @returns */ -export const Logo = ({ white }) => { +export const Logo = props => { + const { white, NOTION_CONFIG } = props const router = useRouter() - const { isDarkMode } = useGlobal() const logoWhite = siteConfig('STARTER_LOGO_WHITE') + const logoNormal = siteConfig('STARTER_LOGO') + const { isDarkMode } = useGlobal() const [logo, setLogo] = useState(logoWhite) const [logoTextColor, setLogoTextColor] = useState('text-white') @@ -24,11 +26,12 @@ export const Logo = ({ white }) => { const scrollY = window.scrollY // 何时显示浅色或白底的logo const homePageNavBar = router.route === '/' && scrollY < 10 // 在首页并且视窗在页面顶部 + if (white || isDarkMode || homePageNavBar) { - setLogo(siteConfig('STARTER_LOGO_WHITE')) + setLogo(logoWhite) setLogoTextColor('text-white') } else { - setLogo(siteConfig('STARTER_LOGO')) + setLogo(logoNormal) setLogoTextColor('text-black') } }, throttleMs) @@ -50,8 +53,9 @@ export const Logo = ({ white }) => { router.push('/') }} src={logo} + height={14} alt='logo' - className='header-logo w-full' + className='header-logo w-full mr-1' /> )} {/* logo文字 */} diff --git a/themes/starter/index.js b/themes/starter/index.js index da034ac5f19..35d837bd559 100644 --- a/themes/starter/index.js +++ b/themes/starter/index.js @@ -81,6 +81,7 @@ const LayoutBase = props => { */ const LayoutIndex = props => { const count = siteConfig('STARTER_BLOG_COUNT', 3, CONFIG) + const { locale } = useGlobal() const posts = props?.allNavPages ? props.allNavPages.slice(0, count) : [] return ( <> @@ -108,7 +109,7 @@ const LayoutIndex = props => {
- 查看全部 + {locale.COMMON.MORE}