ความจริงที่ไม่มีเอกสารก็คือสฟิงซ์ไม่ได้ทำเช่นนี้ ในความเป็นจริง ฟังก์ชันการทำงานของ sphinx i18n หยุดทำงานเพียงขั้นตอนเดียวสำหรับการสร้างไดเร็กทอรีเฉพาะภาษาของคุณทั้งหมด โดยนัยคือคุณต้องออกแบบและล้อม sphinx-build
ด้วยสคริปต์ของคุณเองเพื่อตั้งค่านี้
หากต้องการแปลไฟล์ Sphinx reST เป็นภาษาอื่น คุณไม่จำเป็นต้องอัปเดตไฟล์ '.rst' ด้วยตนเอง สฟิงซ์เข้าใจแล้วว่าบล็อกข้อความมีลักษณะอย่างไร และสามารถแบ่งสตริงหัวเรื่อง คำอธิบาย ย่อหน้าที่คั่นด้วยบรรทัดใหม่ ฯลฯ ออกเป็นสตริงต้นฉบับที่ไม่ซ้ำกัน (msgid) และใส่ลงในไฟล์ภาษาต้นฉบับ '.pot' และไฟล์ภาษาปลายทาง '.po'
ขั้นแรก ให้รัน make gettext
จากไดเร็กทอรี 'docs/' สิ่งนี้จะบอกสฟิงซ์ให้แยกวิเคราะห์ไฟล์ reST ของคุณและค้นหาสตริงที่จะแปลโดยอัตโนมัติและให้ msgid
ที่ไม่ซ้ำกับพวกมัน
user@host:~/rtd-github-pages$ cd docs/
user@host:~/rtd-github-pages/docs$ ls
autodoc.rst buildDocs.sh conf.py.orig locales Makefile
_build conf.py index.rst make.bat _static
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ make gettext
Running Sphinx v1.8.4
making output directory...
building [gettext]: targets for 0 template files
building [gettext]: targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
Hello Worldrces... [ 50%] autodoc
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
writing message catalogs... [100%] index
build succeeded.
The message catalogs are in _build/gettext.
user@host:~/rtd-github-pages/docs$
การดำเนินการข้างต้นควรสร้างไฟล์ต่อไปนี้
user@host:~/rtd-github-pages/docs$ ls _build/gettext/
autodoc.pot index.pot
user@host:~/rtd-github-pages/docs$
นี่เป็นตัวอย่างจาก '_build/gettext/index.pot' ที่แสดงสองสตริงในหน้าหลักของเอกสารประกอบของเราซึ่งเราจะแปลจากภาษาอังกฤษเป็นภาษาสเปน
user@host:~/rtd-github-pages/docs$ grep -m2 -A2 .rst _build/gettext/index.pot
#: ../../index.rst:7
msgid "Welcome to helloWorld's documentation!"
msgstr ""
--
#: ../../index.rst:9
msgid "Contents:"
msgstr ""
user@host:~/rtd-github-pages/docs$
ต่อไป เรามาบอกสฟิงซ์ให้เตรียมไฟล์ '.po' ภาษาปลายทางภาษาสเปนจากไฟล์ '.pot' ที่เป็นภาษาปลายทางที่เราสร้างขึ้นข้างต้น
ก่อนที่จะดำเนินการขั้นตอนนี้ คุณจะต้องติดตั้ง sphinx-intl
และโมดูล python Stemmer
หากคุณใช้ดิสโทรแบบ Debian คุณสามารถทำได้ด้วยคำสั่งต่อไปนี้
sudo apt-get install -y sphinx-intl python3-stemmer
ดำเนินการคำสั่งต่อไปนี้เพื่อเตรียมไฟล์แปลเฉพาะภาษาสเปนของเรา
user@host:~/rtd-github-pages/docs$ sphinx-intl update -p _build/gettext -l es
Create: locales/es/LC_MESSAGES/index.po
Create: locales/es/LC_MESSAGES/autodoc.po
user@host:~/rtd-github-pages/docs$
การดำเนินการข้างต้นได้สร้างไฟล์ '.po' สองไฟล์: หนึ่งไฟล์สำหรับไฟล์ภาษาต้นฉบับ '.pot' แต่ละไฟล์ ซึ่งสัมพันธ์โดยตรงกับไฟล์ '.rst' แต่ละไฟล์ของเรา (index.rst และ autodoc.rst) สมบูรณ์แบบ.
หากเรา grep ไฟล์ 'docs/locales/es/LC_MESSAGES/index.po' เฉพาะภาษาสเปนใหม่ เราจะพบว่ามีเนื้อหาเหมือนกับไฟล์ต้นฉบับ '.pot'
user@host:~/rtd-github-pages/docs$ grep -m2 -A2 .rst locales/es/LC_MESSAGES/index.po
#: ../../index.rst:7
msgid "Welcome to helloWorld's documentation!"
msgstr ""
--
#: ../../index.rst:9
msgid "Contents:"
msgstr ""
user@host:~/rtd-github-pages/docs$
ไฟล์ '.po' เฉพาะภาษาเหล่านี้เป็นที่ที่เราแปลจริงๆ หากคุณเป็นโครงการขนาดใหญ่ คุณอาจต้องการใช้โปรแกรมพิเศษหรือบริการ เพื่อแปล ไฟล์เหล่านี้ แต่เพื่อความชัดเจน เราจะแก้ไขไฟล์โดยตรง
user@host:~/rtd-github-pages/docs$ perl -pi -0e "s^(msgid \"Welcome to helloWorld's documentation\!\"\n)msgstr \"\"^\1msgstr \"¡Bienvenido a la documentación de helloWorld\!\"^" locales/es/LC_MESSAGES/index.po
user@host:~/rtd-github-pages/docs$ perl -pi -0e "s^(msgid \"Contents:\"\n)msgstr \"\"^\1msgstr \"Contenidos:\"^" locales/es/LC_MESSAGES/index.po
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ grep -m2 -A2 .rst locales/es/LC_MESSAGES/index.po
#: ../../index.rst:7
msgid "Welcome to helloWorld's documentation!"
msgstr "¡Bienvenido a la documentación de helloWorld!"
--
#: ../../index.rst:9
msgid "Contents:"
msgstr "Contenidos"
user@host:~/rtd-github-pages/docs$
อย่างที่คุณเห็น การดำเนินการข้างต้นเต็มไปด้วยเนื้อหา msgstr ""
พร้อมด้วยคำแปลภาษาสเปนของบรรทัด msgid
ที่เกี่ยวข้องด้านบนในภาษาต้นฉบับ (ภาษาอังกฤษ)
ตอนนี้เรามาสร้างเนื้อหาคงที่ html ของเราสองเวอร์ชัน: [1] เป็นภาษาอังกฤษและ [2] เป็นภาษาสเปน
user@host:~/rtd-github-pages/docs$ sphinx-build -b html . _build/html/en -D language='en'
Running Sphinx v1.8.4
loading translations [en]... done
making output directory...
building [mo]: targets for 0 po files that are out of date
building : targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
Hello Worldrces... [ 50%] autodoc
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
generating indices... genindex py-modindex
highlighting module code... [100%] helloWorld
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded.
The HTML pages are in _build/html/en.
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ sphinx-build -b html . _build/html/es -D language='es'
Running Sphinx v1.8.4
loading translations [es]... done
making output directory...
building [mo]: targets for 1 po files that are out of date
writing output... [100%] locales/es/LC_MESSAGES/index.mo
building : targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
Hello Worldrces... [ 50%] autodoc
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
generating indices... genindex py-modindex
highlighting module code... [100%] helloWorld
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in Spanish (code: es) ... done
dumping object inventory... done
build succeeded.
The HTML pages are in _build/html/es.
user@host:~/rtd-github-pages/docs$
user@host:~/rtd-github-pages/docs$ firefox _build/html/en/index.html _build/html/es/index.html &
[1] 12134
user@host:~/rtd-github-pages/docs$
คำสั่ง firefox
ในการดำเนินการข้างต้นควรเปิดเบราว์เซอร์ของคุณด้วยสองแท็บ: (1) ในภาษาอังกฤษและ (2) ในภาษาสเปน
สำหรับข้อมูลเพิ่มเติม โปรดดูบทความนี้ใน วิธีการตั้งค่า ความเป็นสากลในสฟิงซ์
person
Michael Altfield
schedule
23.07.2020