
การซิงค์ไฟล์แปล : รักษาคีย์ i18n ให้ตรงกันทุกภาษา
ทุกครั้งที่นักพัฒนาเพิ่มคีย์ในภาษาต้นฉบับ ไฟล์ภาษาอีก 15 ไฟล์จะไม่ตรง คีย์หายแสดงพาธดิบ คีย์เก่าสิ้นเปลืองเวลานักแปลและขนาดชุด ระบบซิงค์อัตโนมัติทำให้ทุกอย่างตรงกัน
ปัญหาการซิงค์
ไฟล์แปลคลาดกันตลอด นักพัฒนาเพิ่ม 'settings.notifications.title' ใน en.json แต่ลืมอีก 15 ภาษา อีกคนลบ 'onboarding.welcome' จากโค้ดแต่ทิ้งในทุกไฟล์ อีกคนเปลี่ยน 'user.name' เป็น 'user.displayName' เฉพาะอังกฤษ เมื่อเวลาผ่านไป คีย์จะหาย เก่า หรือไม่ตรงกัน
// The problem: translation files drift out of sync
// Developer adds a new feature with new keys:
// en.json (source of truth)
{
"nav.home": "Home",
"nav.about": "About",
"nav.pricing": "Pricing", // NEW
"nav.changelog": "Changelog" // NEW
}
// de.json (out of sync - missing new keys)
{
"nav.home": "Startseite",
"nav.about": "Über uns"
// nav.pricing - MISSING
// nav.changelog - MISSING
}
// de.json also has stale keys from deleted features:
{
"nav.home": "Startseite",
"nav.about": "Über uns",
"nav.legacy_page": "Alte Seite" // STALE - removed from en.json
}ตรวจหาคีย์ที่หาย
คีย์หายคือคีย์ที่อยู่ในต้นฉบับแต่ไม่มีในเป้าหมาย เป็นปัญหาพบบ่อยและเสียหายที่สุด ผู้ใช้เห็นพาธอย่าง 'settings.notifications.title' แทนข้อความ i18n-validate ตรวจด้วยการเปรียบเทียบโครงสร้างทุกภาษากับต้นฉบับ
# Detect missing and stale keys
npx i18n-validate sync \
--source locales/en.json \
--targets 'locales/*.json'
# Output:
# locales/de.json:
# Missing keys (2):
# - nav.pricing
# - nav.changelog
# Stale keys (1):
# - nav.legacy_page
# Coverage: 66.7% (2/3 keys)
#
# locales/ja.json:
# Missing keys (5):
# - nav.pricing
# - nav.changelog
# - settings.theme
# - settings.language
# - settings.notifications
# Coverage: 50.0% (5/10 keys)ตรวจหาคีย์เก่า
คีย์เก่าคือคีย์ในไฟล์แปลที่โค้ดไม่อ้างแล้ว สิ้นเปลืองเวลานักแปล เพิ่มขนาดชุด และสร้างความสับสน การตรวจต้องเทียบไฟล์ภาษากับการอ้างอิงในโค้ด
# Step 1: Check which files are out of sync
npx i18n-validate sync --source locales/en.json --targets 'locales/*.json'
# Step 2: Auto-fill missing keys with source values (as placeholders)
npx i18n-validate sync \
--source locales/en.json \
--targets 'locales/*.json' \
--fill-missing \
--fill-value "[NEEDS TRANSLATION] {{source}}"
# Step 3: Remove stale keys no longer in source
npx i18n-validate sync \
--source locales/en.json \
--targets 'locales/*.json' \
--remove-stale
# Step 4: Sort keys to match source order (cleaner diffs)
npx i18n-validate sync \
--source locales/en.json \
--targets 'locales/*.json' \
--sort-keys
# All at once:
npx i18n-validate sync \
--source locales/en.json \
--targets 'locales/*.json' \
--fill-missing \
--remove-stale \
--sort-keysทำการซิงค์เป็นอัตโนมัติ
กลยุทธ์ที่มีประสิทธิภาพรวมสามชั้น : 1) ฮุก pre-commit ตรวจทุกคอมมิต 2) CI บล็อกการผสานที่มีปัญหา 3) การตรวจเต็มตามรอบจับการคลาดเคลื่อนจาก hotfix และการแก้ด้วยตนเอง
# .husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# Check if source locale file changed
SOURCE_CHANGED=$(git diff --cached --name-only | grep -c 'locales/en.json')
if [ "$SOURCE_CHANGED" -gt 0 ]; then
echo "Source locale changed - checking sync..."
npx i18n-validate sync \
--source locales/en.json \
--targets 'locales/*.json' \
--fail-on-missing \
--min-coverage 90
if [ $? -ne 0 ]; then
echo ""
echo "Translation files are out of sync!"
echo "Run: npx i18n-validate sync --fill-missing"
exit 1
fi
fi# .github/workflows/i18n-sync.yml
name: Translation Sync Check
on:
pull_request:
paths:
- 'locales/**'
jobs:
sync-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check translation sync
run: npx i18n-validate sync \
--source locales/en.json \
--targets 'locales/*.json' \
--fail-on-missing \
--min-coverage 95
- name: Comment on PR if out of sync
if: failure()
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Translation files are out of sync. Please run npx i18n-validate sync --fill-missing and commit.'
})รายงานความครอบคลุมคำแปล
ความครอบคลุมคือเปอร์เซ็นต์คีย์ต้นฉบับที่มีคำแปลในแต่ละภาษา i18n-validate สร้างรายงานเปอร์เซ็นต์รายภาษาและเน้นภาษาที่ตามหลัง ใช้เกณฑ์ใน CI บล็อกการผสานเมื่อค่าลดต่ำกว่าระดับยอมรับ
// Split large translation files by feature/namespace
// locales/
// ├── en/
// │ ├── common.json (nav, footer, errors)
// │ ├── auth.json (login, register, reset)
// │ ├── dashboard.json (dashboard-specific)
// │ └── settings.json (settings page)
// ├── de/
// │ ├── common.json
// │ ├── auth.json
// │ ├── dashboard.json
// │ └── settings.json
// Sync with namespace support:
npx i18n-validate sync \
--source 'locales/en/*.json' \
--targets 'locales/*/%.json' \
--namespace-pattern '{locale}/{namespace}.json'
// Benefits:
// - Smaller files, easier to review
// - Feature teams own their translations
// - Lazy-load only needed namespaces
// - Parallel translation workflowsลองใช้ i18n Agent ตอนนี้
ลากและวางไฟล์แปลของคุณที่นี่
JSON, YAML, PO, XML, CSV, Markdown, Properties
หรือคลิกเพื่อเลือกไฟล์
ภาษาเป้าหมาย