Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Nov 9, 2024
2 parents 2dd936c + c2d9324 commit c650d3f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ const BLOG = {
process.env.NEXT_PUBLIC_COMMENT_TWIKOO_COUNT_ENABLE || true, // 博客列表是否显示评论数
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:
Expand Down
6 changes: 5 additions & 1 deletion lib/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ export function GlobalContextProvider(props) {
}
}

// 添加路由变化时的语言处理
useEffect(() => {
initLocale(router.locale, changeLang, updateLocale)
}, [router])

useEffect(() => {
initDarkMode(updateDarkMode, defaultDarkMode)
initLocale(lang, locale, updateLang, updateLocale)
if (
NOTION_CONFIG?.REDIRECT_LANG &&
JSON.parse(NOTION_CONFIG?.REDIRECT_LANG)
Expand Down
43 changes: 18 additions & 25 deletions lib/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,32 @@ export function generateLocaleDict(langString) {
}

/**
* 初始化站点翻译
* 根据用户当前浏览器语言进行切换
* 站点翻译
* 借助router中的locale机制,根据locale自动切换对应的语言
*/
export function initLocale(lang, locale, changeLang, changeLocale) {
export function initLocale(locale, changeLang, updateLocale) {
if (isBrowser) {
// 用户请求的语言
let queryLang =
getQueryVariable('locale') ||
getQueryVariable('lang') ||
loadLangFromLocalStorage()
// 根据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) {
// 用正则表达式匹配有效的语言标识符例如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)
updateLocale(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 {*}
Expand Down
7 changes: 5 additions & 2 deletions themes/starter/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
{/* <!-- ====== Footer Section Start --> */}
Expand Down
4 changes: 2 additions & 2 deletions themes/starter/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const Header = props => {
return () => {
window.removeEventListener('scroll', navBarScollListener)
}
}, [[isDarkMode]])
}, [isDarkMode])

// 滚动监听
const throttleMs = 200
Expand All @@ -58,7 +58,7 @@ export const Header = props => {
<div className='container'>
<div className='relative -mx-4 flex items-center justify-between'>
{/* Logo */}
<Logo />
<Logo {...props} />

<div className='flex w-full items-center justify-between px-4'>
{/* 中间菜单 */}
Expand Down
14 changes: 9 additions & 5 deletions themes/starter/components/Logo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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)
Expand All @@ -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文字 */}
Expand Down
2 changes: 1 addition & 1 deletion themes/starter/components/Team.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
{/* <!-- ====== Team Section Start --> */}
Expand Down
3 changes: 2 additions & 1 deletion themes/starter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down Expand Up @@ -108,7 +109,7 @@ const LayoutIndex = props => {
<Blog posts={posts} />
<div className='container mx-auto flex justify-end mb-4'>
<Link className='text-lg underline' href={'/archive'}>
<span>查看全部</span>
<span>{locale.COMMON.MORE}</span>
<i className='ml-2 fas fa-arrow-right' />
</Link>
</div>
Expand Down

0 comments on commit c650d3f

Please sign in to comment.