跳至內容
Fumadocs

設定

共用的 i18n 設定

僅限核心 API

框架整合指南請參閱國際化。

定義設定

Fumadocs core 提供了支援 i18n 所需的中介軟體與工具函式。

您可以定義一份設定,讓各個工具函式共用。

lib/i18n.ts
import { defineI18n } from 'fumadocs-core/i18n';

export const i18n = defineI18n({
  defaultLanguage: 'en',
  languages: ['en', 'cn'],
});

隱藏語系前綴

若要隱藏語系前綴 (例如 /en/page -> /page) ,請使用 hideLocale 選項。

import { defineI18n } from 'fumadocs-core/i18n';

export const i18n = defineI18n({
  defaultLanguage: 'en',
  languages: ['en', 'cn'],
  hideLocale: 'default-locale',
});
模式說明
always一律隱藏前綴,並從 cookie 偵測語系
default-locale僅隱藏預設語系的前綴
never一律不隱藏前綴 (預設)

使用 always

在 always 模式下,語系會以 cookie 的形式儲存 (由中介軟體設定) ,這對靜態網站來說並非理想做法。

這可能會導致非預期的快取問題,且需要特別留意 SEO,以確保搜尋引擎能正確索引您的頁面。

備用語言

當檔案缺少翻譯時所使用的備用語言,預設為您的 defaultLanguage。

其值須為您 languages 陣列中的其中一個語言。

import { defineI18n } from 'fumadocs-core/i18n';

export const i18n = defineI18n({
  languages: ['en', 'cn'],
  defaultLanguage: 'en',
  // 若檔案缺少英文版本,則使用中文版本
  fallbackLanguage: 'cn',
});

若要停用備用機制,請將其設為 null:

import { defineI18n } from 'fumadocs-core/i18n';

export const i18n = defineI18n({
  languages: ['en', 'cn'],
  defaultLanguage: 'en',
  fallbackLanguage: null,
});

停用後將不會使用任何備用內容 (包括 meta.json) 。您仍可透過 $ 建立所有語系共用的檔案:

my-page.$.md
meta.$.json

How is this guide?

最後更新於