feat: 多语言将中文设置为默认的语言
This commit is contained in:
+5
-3
@@ -12,12 +12,14 @@ export function getCopy(lang: string | undefined): SiteCopy {
|
|||||||
return copies[DEFAULT_LANG];
|
return copies[DEFAULT_LANG];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Astro `getStaticPaths`:为四种语言各生成一页 */
|
/** Astro `getStaticPaths`:默认语言生成无前缀路径,其他语言带前缀 */
|
||||||
export function langStaticPaths() {
|
export function langStaticPaths() {
|
||||||
return LANGS.map((lang) => ({ params: { lang } }));
|
return LANGS.map((lang) => ({
|
||||||
|
params: { lang: lang === DEFAULT_LANG ? undefined : lang },
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
export type { Lang } from './locales';
|
export type { Lang } from './locales';
|
||||||
export type { SiteCopy } from './types';
|
export type { SiteCopy } from './types';
|
||||||
export { LANGS, DEFAULT_LANG, htmlLang, langLabels, hrefFor, isLang } from './locales';
|
export { LANGS, DEFAULT_LANG, htmlLang, langLabels, hrefFor, isLang, resolveLang } from './locales';
|
||||||
export type { RouteKey } from './locales';
|
export type { RouteKey } from './locales';
|
||||||
|
|||||||
+8
-2
@@ -26,6 +26,12 @@ export function isLang(value: string | undefined): value is Lang {
|
|||||||
export type RouteKey = '' | 'teaching-philosophy' | 'curriculum' | 'faculty' | 'about';
|
export type RouteKey = '' | 'teaching-philosophy' | 'curriculum' | 'faculty' | 'about';
|
||||||
|
|
||||||
export function hrefFor(lang: Lang, segment: RouteKey): string {
|
export function hrefFor(lang: Lang, segment: RouteKey): string {
|
||||||
if (!segment) return `/${lang}`;
|
const prefix = lang === DEFAULT_LANG ? '' : `/${lang}`;
|
||||||
return `/${lang}/${segment}`;
|
if (!segment) return prefix || '/';
|
||||||
|
return `${prefix}/${segment}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 从 [...lang] rest 参数解析语言,undefined 时返回默认语言 */
|
||||||
|
export function resolveLang(param: string | undefined): Lang {
|
||||||
|
return isLang(param) ? param : DEFAULT_LANG;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
---
|
---
|
||||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||||
import { getCopy, langStaticPaths, type Lang } from '../../i18n';
|
import { getCopy, langStaticPaths, resolveLang } from '../../i18n';
|
||||||
|
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return langStaticPaths();
|
return langStaticPaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { lang } = Astro.params;
|
const locale = resolveLang(Astro.params.lang);
|
||||||
const L = getCopy(lang);
|
const L = getCopy(locale);
|
||||||
const locale = lang as Lang;
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<BaseLayout
|
<BaseLayout
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
---
|
---
|
||||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||||
import { getCopy, langStaticPaths, type Lang } from '../../i18n';
|
import { getCopy, langStaticPaths, resolveLang } from '../../i18n';
|
||||||
|
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return langStaticPaths();
|
return langStaticPaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { lang } = Astro.params;
|
const locale = resolveLang(Astro.params.lang);
|
||||||
const L = getCopy(lang);
|
const L = getCopy(locale);
|
||||||
const locale = lang as Lang;
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<BaseLayout
|
<BaseLayout
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
---
|
---
|
||||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||||
import { Icon } from 'astro-icon/components';
|
import { Icon } from 'astro-icon/components';
|
||||||
import { getCopy, hrefFor, langStaticPaths, type Lang } from '../../i18n';
|
import { getCopy, hrefFor, langStaticPaths, resolveLang } from '../../i18n';
|
||||||
|
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return langStaticPaths();
|
return langStaticPaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { lang } = Astro.params;
|
const locale = resolveLang(Astro.params.lang);
|
||||||
const L = getCopy(lang);
|
const L = getCopy(locale);
|
||||||
const locale = lang as Lang;
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<BaseLayout
|
<BaseLayout
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
---
|
---
|
||||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||||
import { Icon } from 'astro-icon/components';
|
import { Icon } from 'astro-icon/components';
|
||||||
import { getCopy, hrefFor, langStaticPaths, type Lang } from '../../i18n';
|
import { getCopy, hrefFor, langStaticPaths, resolveLang } from '../../i18n';
|
||||||
|
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return langStaticPaths();
|
return langStaticPaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { lang } = Astro.params;
|
const locale = resolveLang(Astro.params.lang);
|
||||||
const L = getCopy(lang);
|
const L = getCopy(locale);
|
||||||
const locale = lang as Lang;
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<BaseLayout
|
<BaseLayout
|
||||||
+3
-4
@@ -1,14 +1,13 @@
|
|||||||
---
|
---
|
||||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||||
import { getCopy, langStaticPaths, type Lang } from '../../i18n';
|
import { getCopy, langStaticPaths, resolveLang } from '../../i18n';
|
||||||
|
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
return langStaticPaths();
|
return langStaticPaths();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { lang } = Astro.params;
|
const locale = resolveLang(Astro.params.lang);
|
||||||
const L = getCopy(lang);
|
const L = getCopy(locale);
|
||||||
const locale = lang as Lang;
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<BaseLayout
|
<BaseLayout
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
---
|
|
||||||
/**
|
|
||||||
* 根路径立即跳转默认语言。不用 Astro.redirect,静态构建否则会生成 2 秒 meta refresh。
|
|
||||||
*/
|
|
||||||
import { DEFAULT_LANG } from '../i18n';
|
|
||||||
|
|
||||||
const target = `/${DEFAULT_LANG}`;
|
|
||||||
---
|
|
||||||
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="zh-CN">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
||||||
<meta http-equiv="refresh" content={`0;url=${target}`} />
|
|
||||||
<link rel="canonical" href={target} />
|
|
||||||
<meta name="robots" content="noindex" />
|
|
||||||
<title>Redirecting…</title>
|
|
||||||
<script is:inline define:vars={{ target }}>
|
|
||||||
location.replace(target);
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
<body
|
|
||||||
style="margin:0;font-family:system-ui,sans-serif;display:grid;place-items:center;min-height:100vh;background:#fffdfb;color:#444"
|
|
||||||
>
|
|
||||||
<p style="margin:0;font-size:14px">
|
|
||||||
<a href={target} style="color:#4b76eb">继续前往首页 →</a>
|
|
||||||
</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
Reference in New Issue
Block a user