การผสานการทำงานกับ Framework
การตั้งค่า i18n-rosetta ทีละขั้นตอนสำหรับ framework ยอดนิยม
- Hugo
- Next.js (next-intl)
- React (react-i18next)
Hugo (TOML / YAML / Markdown)
โครงสร้างโปรเจกต์
Hugo ใช้ i18n/ สำหรับการแปลสตริง และ content/ สำหรับเนื้อหาหน้าเว็บ:
my-hugo-site/
├── i18n/
│ ├── en.toml ← source of truth
│ ├── fr.toml
│ └── ja.toml
├── content/
│ ├── posts/
│ │ ├── hello.md ← source (English)
│ │ ├── hello.fr.md
│ │ └── hello.ja.md
│ └── about.md
└── .env.local
การตั้งค่า
npm install --save-dev i18n-rosetta
{
"version": 3,
"inputLocale": "en",
"localesDir": "./i18n",
"contentDir": "./content",
"format": "auto",
"languages": ["fr", "de", "ja", "es", "ko", "zh"]
}
i18n-rosetta sync # sync i18n string files + content files
i18n-rosetta sync --dry # preview changes without writing
รายละเอียดการแปลเนื้อหา
Front matter: รองรับตัวคั่นทั้งแบบ YAML (---) และ TOML (+++) โดยจะแปล title, description, summary, subtitle, caption และ linkTitle เป็นค่าเริ่มต้น ฟิลด์อื่นๆ ทั้งหมด (date, draft, tags, weight, slug ฯลฯ) จะถูกเก็บไว้ตามเดิม คุณสามารถปรับแต่งได้ด้วย translatableFields ใน config ของคุณ
Block protection: Code blocks, Hugo shortcodes, inline code และ raw HTML จะถูกป้องกันโดยอัตโนมัติโดยใช้ Unicode sentinel placeholders ซึ่งจะถูกส่งผ่านโดยไม่มีการเปลี่ยนแปลงใดๆ
Filename convention: เป็นไปตามรูปแบบการแปลด้วยชื่อไฟล์ของ Hugo:
my-post.md→my-post.fr.mdmy-post.en.md→my-post.fr.md(ตัดคำต่อท้ายของต้นฉบับออก)
Skip existing: ไฟล์ที่แปลแล้วจะไม่มีการเขียนทับ หากต้องการบังคับให้แปลใหม่ ให้ลบไฟล์ปลายทางทิ้ง
รูปพหูพจน์ (Plural Forms)
Locale แบบ TOML และ YAML รองรับรูปพหูพจน์แบบ CLDR:
[items]
one = "{{ .Count }} item"
other = "{{ .Count }} items"
ในระบบภายในจะแสดงเป็น items.one และ items.other สำหรับการเปรียบเทียบความแตกต่าง (diffing) จากนั้นจะถูกแปลงกลับ (re-serialized) ให้อยู่ในรูปแบบส่วนที่ถูกต้องเมื่อทำการเขียนไฟล์
next-intl (JSON)
โครงสร้างโปรเจกต์
my-app/
├── messages/
│ └── en.json ← source of truth
├── src/
│ ├── i18n/
│ │ ├── routing.ts
│ │ └── request.ts
│ └── middleware.ts
└── .env.local
การตั้งค่า
npm install --save-dev i18n-rosetta
{
"version": 3,
"inputLocale": "en",
"localesDir": "./messages",
"languages": ["fr", "de", "ja", "es", "ko", "zh", "pt", "ar"]
}
npx i18n-rosetta sync
สร้าง messages/fr.json, messages/ja.json ฯลฯ — ซึ่งแปลเสร็จสมบูรณ์ โดยยังคงโครงสร้างคีย์ที่ซ้อนกัน (nested key) ของคุณไว้ next-intl จะนำไปใช้งานโดยอัตโนมัติ
กระบวนการพัฒนา (Development Workflow)
{
"scripts": {
"dev": "i18n-rosetta watch & next dev",
"i18n:sync": "i18n-rosetta sync",
"i18n:audit": "i18n-rosetta audit"
}
}
react-i18next (JSON)
โครงสร้างไฟล์แบบ Flat (แนะนำ)
locales/
├── en.json
├── fr.json
└── ja.json
{
"version": 3,
"inputLocale": "en",
"localesDir": "./locales",
"languages": ["fr", "de", "ja"]
}
npx i18n-rosetta sync
โครงสร้างไดเรกทอรีแบบซ้อนกัน (Nested Directory)
หากคุณใช้โครงสร้างแบบ {locale}/{namespace}.json ให้สร้างสคริปต์ซิงค์เพื่อทำ flatten → แปล → unflatten ดูรายละเอียดเพิ่มเติมได้ที่ เอกสารประกอบของ react-i18next