EmDash 與 Astro 的內建 i18n 路由整合,提供多語言內容管理。Astro 處理 URL 路由和語言環境偵測;EmDash 處理翻譯內容的儲存和檢索。
每個翻譯都是一個完整、獨立的內容項目,具有自己的 slug、狀態和修訂歷史。文章的法語版本可以處於草稿狀態,而英語版本已發布。
設定
透過在 Astro 設定中新增 i18n 區塊來啟用 i18n。EmDash 讀取相同的設定來取得語言環境清單、預設語言環境和回退鏈。
import { defineConfig } from "astro/config";
import emdash, { local } from "emdash/astro";
import { sqlite } from "emdash/db";
export default defineConfig({
i18n: {
defaultLocale: "en",
locales: ["en", "fr", "es"],
fallback: { fr: "en", es: "en" },
},
integrations: [
emdash({
database: sqlite({ url: "file:./data.db" }),
storage: local({
directory: "./uploads",
baseUrl: "/_emdash/api/media/file",
}),
}),
],
});
當 Astro 設定中不存在 i18n 時,所有 i18n 功能都會被停用,EmDash 會表現為單一語言 CMS。
翻譯的運作原理
EmDash 使用每個語言環境一列的模型。每個翻譯在資料庫中都是自己的列,具有自己的 ID、slug 和狀態,透過共享的 translation_group 識別碼與其他翻譯連結。具有三個翻譯的 posts 表格如下所示:
ec_posts:
id | slug | locale | translation_group | status
---------|-------------|--------|-------------------|----------
01ABC... | my-post | en | 01ABC... | published
01DEF... | mon-article | fr | 01ABC... | draft
01GHI... | mi-entrada | es | 01ABC... | published
這種設計意味著:
- 每個語言環境的 slug —
/blog/my-post和/fr/blog/mon-article自然地運作 - 每個語言環境的發布 — 發布英語版本,同時保持法語版本為草稿
- 每個語言環境的修訂 — 每個翻譯都有自己的修訂歷史
- 單一語言環境查詢 — 清單查詢僅傳回一個語言環境的項目
查詢翻譯內容
單一項目
將 locale 傳遞給 getEmDashEntry 以檢索特定翻譯。當省略時,預設為請求的目前語言環境(由 Astro 的 i18n 中介軟體設定)。
---
import { getEmDashEntry } from "emdash";
const { slug } = Astro.params;
const { entry: post, error } = await getEmDashEntry("posts", slug, {
locale: Astro.currentLocale,
});
if (!post) return Astro.redirect("/404");
---
<article>
<h1>{post.data.title}</h1>
</article>
回退鏈
當請求的語言環境不存在內容時,EmDash 遵循 Astro 設定中定義的回退鏈。給定 fallback: { fr: "en" }:
- 嘗試請求的語言環境(
fr) - 嘗試回退語言環境(
en) - 嘗試預設語言環境
回退僅適用於單一項目查詢。清單查詢僅傳回請求的語言環境的項目。
選單
選單是按語言環境的 — 相同的 name(例如 "primary")可以存在於多個語言環境中,所有語言環境都透過共享的 translation_group 連結。選單項目根據活動語言環境版本的引用內容解析其內容引用。
以下元件取得活動語言環境的主選單:
---
import { getMenu } from "emdash";
const menu = await getMenu("primary", { locale: Astro.currentLocale });
---
<nav aria-label="Primary">
<ul>
{menu?.items.map((item) => (
<li><a href={item.url}>{item.label}</a></li>
))}
</ul>
</nav>
從管理員的選單清單建立現有選單的翻譯 — 項目使用完整的 reference_id 複製(它儲存引用內容的 translation_group),因此新選單的連結會自動指向正確的按語言環境的內容。
分類法(類別、標籤)
術語是按語言環境的。定義(_emdash_taxonomy_defs)也是按語言環境的,因此 label / labelSingular 也可以翻譯。樞紐 content_taxonomies.taxonomy_id 儲存術語的 translation_group,因此單一分配跨越內容的每個語言環境。
以下範例取得活動語言環境的類別和文章的術語:
---
import { getTaxonomyTerms, getEntryTerms } from "emdash";
const categories = await getTaxonomyTerms("category", {
locale: Astro.currentLocale,
});
const terms = await getEntryTerms("posts", post.id, undefined, {
locale: Astro.currentLocale,
});
---
翻譯內容會自動繼承來源的術語分配 — 您只需要翻譯術語本身一次,使用它們的每篇文章都會在讀取時解析為正確的語言環境。
集合清單
按語言環境過濾集合:
---
import { getEmDashCollection } from "emdash";
const { entries: posts } = await getEmDashCollection("posts", {
locale: Astro.currentLocale,
status: "published",
});
---
<ul>
{posts.map((post) => (
<li><a href={`/${post.data.slug}`}>{post.data.title}</a></li>
))}
</ul>
語言切換器
使用 getTranslations 建構連結到目前項目現有翻譯的語言切換器:
---
import { getTranslations } from "emdash";
import { getRelativeLocaleUrl } from "astro:i18n";
interface Props {
collection: string;
entryId: string;
}
const { collection, entryId } = Astro.props;
const { translations } = await getTranslations(collection, entryId);
---
<nav aria-label="Language">
<ul>
{translations.map((t) => (
<li>
<a
href={getRelativeLocaleUrl(t.locale, `/blog/${t.slug}`)}
aria-current={t.locale === Astro.currentLocale ? "page" : undefined}
>
{t.locale.toUpperCase()}
</a>
</li>
))}
</ul>
</nav>
getTranslations 函式傳回同一翻譯群組中的所有語言環境變體:
const { translationGroup, translations } = await getTranslations("posts", post.entry.id);
// translations: [
// { locale: "en", id: "01ABC...", slug: "my-post", status: "published" },
// { locale: "fr", id: "01DEF...", slug: "mon-article", status: "draft" },
// ]
在管理員中管理翻譯
內容清單
啟用 i18n 後,內容清單顯示:
- 顯示每個項目的語言環境的語言環境欄
- 工具列中的語言環境篩選器,用於在語言環境之間切換
建立翻譯
在編輯器中開啟任何內容項目。側邊欄顯示一個翻譯面板,列出所有已設定的語言環境。對於每個語言環境:
- 「翻譯」 — 對於沒有翻譯的語言環境顯示 — 點選以建立一個
- 「編輯」 — 對於具有現有翻譯的語言環境顯示 — 點選以導覽到它
- 目前語言環境用勾選標記標記
建立翻譯時,新項目會預先填入來源語言環境的資料,並分配預設 slug {來源-slug}-{語言環境}。根據需要調整 slug 和內容,然後儲存。
按語言環境發布
每個翻譯都有自己的狀態。獨立發布、取消發布或安排翻譯。法語版本可以是草稿,而英語版本是即時的。
內容 API
locale 參數
所有內容 API 路由都接受可選的 locale 查詢參數:
GET /_emdash/api/content/posts?locale=fr
GET /_emdash/api/content/posts/my-post?locale=fr
省略時,預設為已設定的預設語言環境。
透過 API 建立翻譯
透過將 locale 和 translationOf 傳遞給內容建立端點來建立翻譯:
POST /_emdash/api/content/posts
Content-Type: application/json
{
"locale": "fr",
"translationOf": "01ABC...",
"data": {
"title": "Mon Article",
"slug": "mon-article"
}
}
新項目共享來源項目的 translation_group 並作為草稿開始。
列出翻譯
檢索給定項目的所有翻譯:
GET /_emdash/api/content/posts/01ABC.../translations
傳回翻譯群組 ID 和具有其 ID、slug 和狀態的語言環境變體陣列。
CLI
CLI 支援內容命令上的 --locale 旗標:
# 列出法語文章
emdash content list posts --locale fr
# 取得法語的特定項目
emdash content get posts my-post --locale fr
# 建立現有項目的法語翻譯
emdash content create posts --locale fr --translation-of 01ABC...
多語言內容種子
種子檔案使用 locale 和 translationOf 表達翻譯:
{
"content": {
"posts": [
{
"id": "welcome",
"slug": "welcome",
"locale": "en",
"status": "published",
"data": { "title": "Welcome" }
},
{
"id": "welcome-fr",
"slug": "bienvenue",
"locale": "fr",
"translationOf": "welcome",
"status": "draft",
"data": { "title": "Bienvenue" }
}
]
}
}
來源語言環境項目必須出現在種子檔案中的翻譯之前,以便 translationOf 引用正確解析。
欄位可翻譯性
每個欄位都有一個 translatable 設定(預設:true)。建立翻譯時:
- 可翻譯欄位從來源語言環境預先填入以供編輯
- 不可翻譯欄位被複製並在群組中的所有翻譯之間保持同步
系統欄位(如 status、published_at 和 author_id)始終按語言環境,從不同步。
URL 策略
EmDash 不管理語言環境 URL — Astro 處理路由。常見模式:
# prefix-other-locales(Astro 預設)
/blog/my-post → en(預設語言環境,無前綴)
/fr/blog/mon-article → fr
# prefix-always
/en/blog/my-post → en
/fr/blog/mon-article → fr
使用 astro:i18n 中的 getRelativeLocaleUrl 建構正確的 URL,無論路由模式如何。
匯入多語言內容
透過管理員遷移工具匯入 WordPress 內容 — 請參閱內容匯入和從 WordPress 遷移。WXR 匯出不包含 WPML 或 Polylang 新增的語言環境和翻譯群組結構,因此匯入的內容會到達您的預設語言環境。
要從匯入的內容建構翻譯,請建立翻譯的項目並將其連結到原始項目:
emdash content create posts --locale fr --translation-of 01ABC...
這與上面多語言內容種子中顯示的 --locale / --translation-of 工作流程相同,在匯入完成後套用。
後續步驟
- 查詢內容 — 完整的查詢 API 參考
- 使用內容 — 管理員內容管理
- Astro i18n 路由 — Astro 的路由設定