@ -3,6 +3,7 @@ import '../styles/global.css'
|
||||
import { getRelativeLocaleUrl } from 'astro:i18n'
|
||||
import { useTranslations, DEFAULT_LOCALE, type Locale } from '../i18n/utils'
|
||||
import { LOCALES, LOCALE_NAMES, isLocaleSegment } from '../i18n/config'
|
||||
import LanguageDetector from '../components/LanguageDetector.astro'
|
||||
|
||||
const {
|
||||
title = 'Portfolio Studio',
|
||||
@ -14,43 +15,17 @@ const {
|
||||
const locale = (Astro.currentLocale as Locale) ?? DEFAULT_LOCALE
|
||||
const { t } = useTranslations(locale)
|
||||
|
||||
|
||||
// 当前路径去掉语言前缀与尾随 .html(format:'file' 产物如 cn.html / shows.html),
|
||||
// 用于语言切换器拼出「同页面、另一语言」的链接(逻辑路径,不带 .html)
|
||||
// 例:/cn.html → / ,/cn/shows.html → /shows ,/shows.html → /shows
|
||||
let _segs = Astro.url.pathname.split('/').filter(Boolean);
|
||||
if (_segs[0] && isLocaleSegment(_segs[0])) _segs = _segs.slice(1);
|
||||
const _last = _segs[_segs.length - 1];
|
||||
if (_last && _last.endsWith('.html')) _segs = _segs.slice(0, -1);
|
||||
const currentPath = _segs.length === 0 ? '/' : '/' + _segs.join('/');
|
||||
|
||||
---
|
||||
<!doctype html>
|
||||
<html lang={locale === 'cn' ? 'zh-CN' : 'en'}>
|
||||
<head>
|
||||
<!-- <LanguageDetector /> -->
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
<meta name="generator" content={Astro.generator} />
|
||||
<title>{title}</title>
|
||||
<!-- 语言自动检测:无前缀且浏览器为中文环境时,跳转到 /cn 前缀 -->
|
||||
<script is:inline>
|
||||
(function () {
|
||||
try {
|
||||
var SUPPORTED = ['en', 'cn'];
|
||||
var path = location.pathname;
|
||||
var seg = path.split('/').filter(Boolean)[0];
|
||||
if (SUPPORTED.indexOf(seg) !== -1) return; // 已有语言前缀
|
||||
var lang = (navigator.language || 'en').toLowerCase();
|
||||
var target = lang.indexOf('zh') === 0 ? 'cn' : 'en';
|
||||
if (target === 'en') return; // 默认英文无需跳转
|
||||
if (sessionStorage.getItem('lang-redirect')) return;
|
||||
sessionStorage.setItem('lang-redirect', '1');
|
||||
location.pathname = '/' + target + path;
|
||||
} catch (e) {}
|
||||
})();
|
||||
</script>
|
||||
</head>
|
||||
<body class="bg-white text-black min-h-screen flex flex-col p-[30px_50px] font-sans">
|
||||
|
||||
@ -106,7 +81,7 @@ const currentPath = _segs.length === 0 ? '/' : '/' + _segs.join('/');
|
||||
{LOCALES.map((l) => (
|
||||
<li>
|
||||
<a
|
||||
href={getRelativeLocaleUrl(l, currentPath)}
|
||||
href={getRelativeLocaleUrl(l)}
|
||||
@click="open = false"
|
||||
class:list={[
|
||||
'block px-4 py-1.5 text-[12px] uppercase tracking-[0.5px] hover:bg-black/5 transition-colors',
|
||||
|
||||
Reference in New Issue
Block a user