跳到主要内容

配置starlight默认语言为中文

最后更新于:

配置starlight默认语言为中文

starlight主题默认是英文的,如何配置成默认中文呢?

把 Starlight 设为默认中文,核心是改 astro.config.mjsdefaultLocalelocales,并调整文件目录。

一、单语言站点(纯中文,无英文)

  1. 修改配置文件 打开 astro.config.mjs,设置:
  • defaultLocale: 'root'
  • locales 里用 root 对应中文
 1// astro.config.mjs
 2import { defineConfig } from 'astro/config';
 3import starlight from '@astrojs/starlight';
 4
 5export default defineConfig({
 6  integrations: [
 7    starlight({
 8      title: '我的中文文档',
 9      // 设为默认中文
10      defaultLocale: 'root',
11      locales: {
12        // 根目录存放中文内容 src/content/docs/
13        root: {
14          label: '简体中文',
15          lang: 'zh-CN',
16        },
17      },
18    }),
19  ],
20});
  1. 调整文件目录
  • 把原有 src/content/docs/en/ 下的所有文件
  • 直接移到 src/content/docs/(根目录)
  • 删除空的 en 文件夹

二、双语站点(中英并存,默认中文)

  1. 修改配置文件
 1// astro.config.mjs
 2import { defineConfig } from 'astro/config';
 3import starlight from '@astrojs/starlight';
 4
 5export default defineConfig({
 6  integrations: [
 7    starlight({
 8      title: '我的文档',
 9      // 默认语言改为中文
10      defaultLocale: 'zh-cn',
11      locales: {
12        // 中文:src/content/docs/zh-cn/
13        'zh-cn': {
14          label: '简体中文',
15          lang: 'zh-CN',
16        },
17        // 英文:src/content/docs/en/
18        en: {
19          label: 'English',
20        },
21      },
22    }),
23  ],
24});
  1. 调整文件目录
  • 新建 src/content/docs/zh-cn/
  • 把中文内容放 zh-cn/
  • 英文仍在 en/
  • 访问首页会自动进入 /zh-cn/(默认中文)

三、验证

  • 重启开发服务:astro dev
  • 界面(搜索、上一页、下一页、编辑链接等)会变成中文
  • 单语言:URL 无 /en//zh-cn/
  • 双语:默认走 /zh-cn/

要不要我给你一份可直接复制的完整 astro.config.mjs 配置(单语/双语二选一)?

😎

最终正确配置(测试成功)-2026.4.12

编辑astro.config.mjs

核心配置:

1			// 设为默认中文
2			defaultLocale: 'root',
3			locales: {
4				// 根目录存放中文内容 src/content/docs/
5				root: {
6					label: '简体中文',
7					lang: 'zh-CN',
8				},

完整配置:

 1
 2// @ts-check
 3import { defineConfig } from 'astro/config';
 4import starlight from '@astrojs/starlight';
 5
 6// https://astro.build/config
 7export default defineConfig({
 8	integrations: [
 9		starlight({
10			title: 'My Docs',
11
12			// 设为默认中文
13			defaultLocale: 'root',
14			locales: {
15				// 根目录存放中文内容 src/content/docs/
16				root: {
17					label: '简体中文',
18					lang: 'zh-CN',
19				},
20			},
21
22
23			social: [{ icon: 'github', label: 'GitHub', href: 'https://github.com/withastro/starlight' }],
24			sidebar: [
25				{
26				label: '指南',
27				items: [
28					{ label: '示例指南', link: '/guides/example/' },
29				],
30				},
31				{
32				label: '参考',
33				items: [
34					{ label: '示例参考', link: '/reference/example/' },
35				],
36				},
37			],
38		}),
39	],
40});
最新文章

文档导航