From 8df49a195daaafb9df0a855fed1d8c4b844ba49d Mon Sep 17 00:00:00 2001 From: noeFly Date: Wed, 12 Feb 2025 21:29:18 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20=E4=BF=AE=E5=BE=A9=20?= =?UTF-8?q?i18n=20=E7=84=A1=E6=B3=95=E5=A5=97=E7=94=A8=E8=AA=9E=E8=A8=80?= =?UTF-8?q?=E8=AE=8A=E9=AB=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修復 i18n 無法正確使用語言變體,轉而使用最靠近的語系翻譯 --- lib/global.js | 8 +------- lib/lang.js | 35 ++++++++++++----------------------- lib/notion/convertInnerUrl.js | 4 ++-- next.config.js | 12 ++++++++---- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/lib/global.js b/lib/global.js index b9e84660..0bfbcfbd 100644 --- a/lib/global.js +++ b/lib/global.js @@ -8,12 +8,7 @@ import { import { useUser } from '@clerk/nextjs' import { useRouter } from 'next/router' import { createContext, useContext, useEffect, useState } from 'react' -import { - generateLocaleDict, - initLocale, - redirectUserLang, - saveLangToLocalStorage -} from './lang' +import { generateLocaleDict, initLocale, redirectUserLang } from './lang' /** * 全局上下文 @@ -81,7 +76,6 @@ export function GlobalContextProvider(props) { function changeLang(lang) { if (lang) { - saveLangToLocalStorage(lang) updateLang(lang) updateLocale(generateLocaleDict(lang)) } diff --git a/lib/lang.js b/lib/lang.js index 9eda4da9..17a4189e 100644 --- a/lib/lang.js +++ b/lib/lang.js @@ -8,6 +8,7 @@ import zhCN from './lang/zh-CN' import zhHK from './lang/zh-HK' import zhTW from './lang/zh-TW' import { extractLangPrefix } from './utils/pageId' +import { useRouter } from 'next/router' /** * 在这里配置所有支持的语言 @@ -70,15 +71,9 @@ export function generateLocaleDict(langString) { */ export function initLocale(locale, changeLang, updateLocale) { if (isBrowser) { - // 根据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 - + getQueryVariable('locale') || getQueryVariable('lang') || locale if (queryLang) { const match = queryLang.match(/[a-zA-Z]{2}(?:-[a-zA-Z]{2})?/) if (match) { @@ -91,22 +86,6 @@ export function initLocale(locale, changeLang, updateLocale) { } } -/** - * 读取语言 - * @returns {*} - */ -export const loadLangFromLocalStorage = () => { - return localStorage.getItem('lang') -} - -/** - * 保存语言 - * @param newTheme - */ -export const saveLangToLocalStorage = lang => { - localStorage.setItem('lang', lang) -} - /** * 检测用户的预研偏好,跳转至对应的多语言网站 * @param {*} lang @@ -143,3 +122,13 @@ export const redirectUserLang = (lang, pageId) => { } } } + +/** + * 获取当前 Next.js 路由下的 locale + * @returns 当前路由下的 locale + */ +export function getCurrentLang() { + const router = useRouter() + let currentLang = router.locale + return currentLang +} diff --git a/lib/notion/convertInnerUrl.js b/lib/notion/convertInnerUrl.js index 017bebd1..c6a39dcf 100644 --- a/lib/notion/convertInnerUrl.js +++ b/lib/notion/convertInnerUrl.js @@ -1,6 +1,6 @@ import { idToUuid } from 'notion-utils' import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils' -import { loadLangFromLocalStorage } from '@/lib/lang' +import { getCurrentLang } from '@/lib/lang' /** * 处理页面内连接跳转: @@ -21,7 +21,7 @@ export const convertInnerUrl = allPages => { const { origin, pathname } = window.location; const currentURL = origin + pathname const currentPathLang = pathname.split('/').filter(Boolean)[0] - const lang = loadLangFromLocalStorage().split(/[-_]/)[0] + const lang = getCurrentLang const langPrefix = lang === currentPathLang ? '/' + lang : '' for (const anchorTag of allAnchorTags) { // url替换成slug diff --git a/next.config.js b/next.config.js index 658001ed..3b8d7135 100644 --- a/next.config.js +++ b/next.config.js @@ -15,7 +15,7 @@ const themes = scanSubdirectories(path.resolve(__dirname, 'themes')) const locales = (function () { // 根据BLOG_NOTION_PAGE_ID 检查支持多少种语言数据. // 支持如下格式配置多个语言的页面id xxx,zh:xxx,en:xxx - const langs = [BLOG.LANG.slice(0, 2)] + const langs = [BLOG.LANG] if (BLOG.NOTION_PAGE_ID.indexOf(',') > 0) { const siteIds = BLOG.NOTION_PAGE_ID.split(',') for (let index = 0; index < siteIds.length; index++) { @@ -84,15 +84,19 @@ const nextConfig = { eslint: { ignoreDuringBuilds: true }, - output: process.env.EXPORT ? 'export' : process.env.NEXT_BUILD_STANDALONE === 'true' ? 'standalone' : undefined, + output: process.env.EXPORT + ? 'export' + : process.env.NEXT_BUILD_STANDALONE === 'true' + ? 'standalone' + : undefined, staticPageGenerationTimeout: 120, // 多语言, 在export时禁用 i18n: process.env.EXPORT ? undefined : { - defaultLocale: BLOG.LANG.slice(0, 2), + defaultLocale: BLOG.LANG, // 支持的所有多语言,按需填写即可 - locales + locales: locales }, images: { // 图片压缩 From 861b8a92e09b452010ac8801edb292b83ff8624c Mon Sep 17 00:00:00 2001 From: noeFly Date: Wed, 12 Feb 2025 21:30:47 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=F0=9F=9A=A8=20Prettier=20?= =?UTF-8?q?=E8=87=AA=E5=8B=95=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/convertInnerUrl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/notion/convertInnerUrl.js b/lib/notion/convertInnerUrl.js index c6a39dcf..913fba9e 100644 --- a/lib/notion/convertInnerUrl.js +++ b/lib/notion/convertInnerUrl.js @@ -18,7 +18,7 @@ export const convertInnerUrl = allPages => { if (!allAnchorTags) { return } - const { origin, pathname } = window.location; + const { origin, pathname } = window.location const currentURL = origin + pathname const currentPathLang = pathname.split('/').filter(Boolean)[0] const lang = getCurrentLang @@ -50,4 +50,4 @@ export const convertInnerUrl = allPages => { } } } -} \ No newline at end of file +} From 99ba183f5603d1e5b7bb9b0dc24aed970371b772 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Fri, 11 Apr 2025 18:14:49 +0800 Subject: [PATCH 3/4] fix lang --- components/ExternalPlugins.js | 4 +++- lib/lang.js | 12 +----------- lib/notion/convertInnerUrl.js | 4 ++-- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/components/ExternalPlugins.js b/components/ExternalPlugins.js index c2bf54d5..65932a39 100644 --- a/components/ExternalPlugins.js +++ b/components/ExternalPlugins.js @@ -10,6 +10,7 @@ import { initGoogleAdsense } from './GoogleAdsense' import Head from 'next/head' import ExternalScript from './ExternalScript' import WebWhiz from './Webwhiz' +import { useGlobal } from '@/lib/global' /** * 各种插件脚本 @@ -19,6 +20,7 @@ import WebWhiz from './Webwhiz' const ExternalPlugin = props => { // 读取自Notion的配置 const { NOTION_CONFIG } = props + const {lang} = useGlobal() const DISABLE_PLUGIN = siteConfig('DISABLE_PLUGIN', null, NOTION_CONFIG) const THEME_SWITCH = siteConfig('THEME_SWITCH', null, NOTION_CONFIG) const DEBUG = siteConfig('DEBUG', null, NOTION_CONFIG) @@ -166,7 +168,7 @@ const ExternalPlugin = props => { setTimeout(() => { // 映射url - convertInnerUrl(props?.allNavPages) + convertInnerUrl({ allPages:props?.allNavPages, lang:lang }) }, 500) }, [router]) diff --git a/lib/lang.js b/lib/lang.js index 17a4189e..47bb7093 100644 --- a/lib/lang.js +++ b/lib/lang.js @@ -121,14 +121,4 @@ export const redirectUserLang = (lang, pageId) => { } } } -} - -/** - * 获取当前 Next.js 路由下的 locale - * @returns 当前路由下的 locale - */ -export function getCurrentLang() { - const router = useRouter() - let currentLang = router.locale - return currentLang -} +} \ No newline at end of file diff --git a/lib/notion/convertInnerUrl.js b/lib/notion/convertInnerUrl.js index 913fba9e..16ef17c0 100644 --- a/lib/notion/convertInnerUrl.js +++ b/lib/notion/convertInnerUrl.js @@ -1,13 +1,14 @@ import { idToUuid } from 'notion-utils' import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils' import { getCurrentLang } from '@/lib/lang' +import BLOG from '@/blog.config' /** * 处理页面内连接跳转: * 1.若是本站域名,则在当前窗口打开、不开新窗口 * 2.url是notion-id,转成站内文章链接 */ -export const convertInnerUrl = allPages => { +export const convertInnerUrl = ({allPages,lang}) => { if (!isBrowser) { return } @@ -21,7 +22,6 @@ export const convertInnerUrl = allPages => { const { origin, pathname } = window.location const currentURL = origin + pathname const currentPathLang = pathname.split('/').filter(Boolean)[0] - const lang = getCurrentLang const langPrefix = lang === currentPathLang ? '/' + lang : '' for (const anchorTag of allAnchorTags) { // url替换成slug From 37b6c5b3dd948e050e2858cb9ea57aee6f0b6a46 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Fri, 11 Apr 2025 22:34:15 +0800 Subject: [PATCH 4/4] fix iconfont not found --- components/IconFont.js | 46 ++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/components/IconFont.js b/components/IconFont.js index ddf15d48..a363eae8 100644 --- a/components/IconFont.js +++ b/components/IconFont.js @@ -10,30 +10,34 @@ export default function IconFont() { const router = useRouter() useEffect(() => { - loadExternalResource('/webfonts/iconfont.js').then(u => { - console.log('iconfont loaded') + loadExternalResource('/webfonts/iconfont.js') + .then(u => { + console.log('iconfont loaded'); - // 查找所有 标签且 class 包含 'icon-' - const iElements = document.querySelectorAll('i[class*="icon-"]'); - iElements.forEach(element => { - const className = Array.from(element.classList).find(cls => cls.startsWith('icon-')); - if (className) { - // 创建新的 元素 - const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); - svgElement.setAttribute('class', 'icon'); - svgElement.setAttribute('aria-hidden', 'true'); + // 查找所有 标签且 class 包含 'icon-' + const iElements = document.querySelectorAll('i[class*="icon-"]'); + iElements.forEach(element => { + const className = Array.from(element.classList).find(cls => cls.startsWith('icon-')); + if (className) { + // 创建新的 元素 + const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + svgElement.setAttribute('class', 'icon'); + svgElement.setAttribute('aria-hidden', 'true'); - const useElement = document.createElementNS('http://www.w3.org/2000/svg', 'use'); - useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', `#${className}`); - svgElement.appendChild(useElement); + const useElement = document.createElementNS('http://www.w3.org/2000/svg', 'use'); + useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', `#${className}`); + svgElement.appendChild(useElement); - // 替换原来的 元素 - element.replaceWith(svgElement); - console.log(`Replaced with class "${className}" to `); - } + // 替换原来的 元素 + element.replaceWith(svgElement); + console.log(`Replaced with class "${className}" to `); + } + }); + }) + .catch(error => { + console.warn('Failed to load iconfont.js:', error); }); - }) - }, [router]) + }, [router]); return }