Registry / sphinx-intl

sphinx-intl

JSON →
library2.3.2pypypiunverified

sphinx-intl is a utility for Sphinx that simplifies the process of translating documentation. It helps manage `.pot` (Portable Object Template) and `.po` (Portable Object) files, making it easier to extract translatable messages, update translations, and build translated versions of Sphinx projects. The current version is 2.3.2, and it typically sees a few releases per year, keeping pace with Python and Sphinx updates.

pip install sphinx-intl
INSTALL
IMPORT
SIG · SPHINX-INTL
S
sphinx-intl
pythonv2.3.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

This quickstart demonstrates the core workflow of sphinx-intl using a temporary Sphinx project. It covers generating `.pot` files, initializing a locale, updating `.po` files, simulating translation, building `.mo` files, and finally building the translated HTML output.

import os import shutil import subprocess import tempfile # Create a temporary directory for the Sphinx project temp_dir = tempfile.mkdtemp() print(f"Working in temporary directory: {temp_dir}") try: project_dir = os.path.join(temp_dir, "myproject") os.makedirs(project_dir) # 1. Create a minimal conf.py conf_content = ''' import os import sys sys.path.insert(0, os.path.abspath('.')) project = 'My Translated Project' copyright = '2024, Author' extensions = [] html_theme = 'alabaster' ''' with open(os.path.join(project_dir, "conf.py"), "w") as f: f.write(conf_content) # 2. Create a minimal index.rst with translatable content index_content = ''' .. _index: Welcome to My Translated Project ================================ .. rst-class:: special This is a paragraph that needs translation. Another paragraph. ''' with open(os.path.join(project_dir, "index.rst"), "w") as f: f.write(index_content) # 3. Generate POT files using sphinx-build print("\n--- Generating .pot files ---") gettext_output_dir = os.path.join(project_dir, "_build", "gettext") subprocess.run(["sphinx-build", "-b", "gettext", project_dir, gettext_output_dir], check=True, capture_output=True, text=True) # 4. Initialize locale directory for a target language (e.g., French) print("\n--- Initializing French locale ---") locale_dir = os.path.join(project_dir, "_build", "locale") subprocess.run(["sphinx-intl", "init", "-l", "fr", "-d", locale_dir, "-p", gettext_output_dir], check=True, capture_output=True, text=True) # 5. Update PO files (no changes yet, but good practice) print("\n--- Updating PO files (no changes expected yet) ---") subprocess.run(["sphinx-intl", "update", "-l", "fr", "-d", locale_dir, "-p", gettext_output_dir], check=True, capture_output=True, text=True) # Simulate manual translation: modify the .po file po_file_path = os.path.join(locale_dir, "fr", "LC_MESSAGES", "index.po") if os.path.exists(po_file_path): with open(po_file_path, "r") as f: po_content = f.read() po_content = po_content.replace( 'msgid "This is a paragraph that needs translation."', 'msgid "This is a paragraph that needs translation."\nmsgstr "Ceci est un paragraphe à traduire." ) po_content = po_content.replace( 'msgid "Another paragraph."', 'msgid "Another paragraph."\nmsgstr "Un autre paragraphe." ) with open(po_file_path, "w") as f: f.write(po_content) print(f"--- Translated {po_file_path} ---") else: print(f"Error: {po_file_path} not found.") # 6. Build MO files from PO files using sphinx-intl build print("\n--- Building MO files from PO files ---") subprocess.run(["sphinx-intl", "build", "-d", locale_dir], check=True, capture_output=True, text=True) # 7. Build HTML output for the translated language print("\n--- Building translated HTML documentation ---") html_output_dir_fr = os.path.join(project_dir, "_build", "html", "fr") subprocess.run(["sphinx-build", "-b", "html", "-D", "language=fr", project_dir, html_output_dir_fr], check=True, capture_output=True, text=True) print(f"\nSuccessfully built translated documentation in: {html_output_dir_fr}") print("Check the generated HTML files, e.g., _build/html/fr/index.html") except subprocess.CalledProcessError as e: print(f"Error executing command: {e.cmd}") print(f"Return code: {e.returncode}") print(f"Stdout:\n{e.stdout}") if e.stderr: print(f"Stderr:\n{e.stderr}") except Exception as e: print(f"An unexpected error occurred: {e}") finally: # Clean up the temporary directory print(f"\nCleaning up temporary directory: {temp_dir}") shutil.rmtree(temp_dir)
sphinx-intl --version
Debug
Known issues
breakingsphinx-intl dropped support for Python versions older than 3.9 in version 2.3.0. Projects using older Python versions will need to upgrade their Python environment or stick to an older sphinx-intl release.
fix
Upgrade your Python environment to 3.9 or newer. If this is not possible, pin `sphinx-intl<2.3.0` in your project's dependencies.
affects: >=2.3.0
gotchaPrior to version 2.3.2, sphinx-intl's `update` command might not have correctly respected the `locale_dirs` setting when specified via `-c conf.py`. This could lead to `.po` files being generated or updated in unexpected locations.
fix
Upgrade to sphinx-intl 2.3.2 or newer to ensure `locale_dirs` is correctly honored when provided via `conf.py` with the `-c` option. Verify `locale_dirs` in your `conf.py` and potentially use the `-d` option explicitly with `sphinx-intl` commands.
affects: <2.3.2
gotchaThe behavior of `resource_name` in `.tx/config` files (used for Transifex integration) changed before 2.1.0 and was explicitly preserved to pre-2.1.0 behavior in 2.2.0. Users upgrading from very old versions might encounter unexpected resource naming if relying on Transifex.
fix
If using Transifex and upgrading from versions significantly older than 2.2.0, review your `.tx/config` and test translation workflows carefully. For new projects or upgrades, 2.2.0 and newer should provide consistent `resource_name` behavior.
affects: 2.1.0
Errors
Common errors & fixes
ERROR: Package 'sphinx-intl' requires a different Python: X.Y.Z not in '>=3.9'
Your Python version is too old for the installed or requested version of sphinx-intl.
fix
Upgrade your Python environment to version 3.9 or newer, or install an older compatible version of sphinx-intl (e.g., `pip install 'sphinx-intl<2.3.0'`).
sphinx-intl: command not found
The `sphinx-intl` command-line tool is not installed or not available in your system's PATH.
fix
Ensure sphinx-intl is installed in your active Python environment (`pip install sphinx-intl`) and that your environment's `Scripts` or `bin` directory is correctly included in your system's PATH.
sphinx-intl: error: argument -d/--locale-dir: directory not found: '_build/locale'
The directory specified with `-d` or `--locale-dir` does not exist. `sphinx-intl` requires this directory for `.po` and `.mo` files.
fix
Ensure the target locale directory exists before running `sphinx-intl` commands, or create it manually (e.g., `mkdir -p _build/locale`). The `init` command usually creates the initial structure, but `update` or `build` expect it to be present.
WARNING: No .pot files found in the specified path: _build/gettext
The path provided to the `-p` or `--pot-dir` option either does not exist or contains no `.pot` (Portable Object Template) files.
fix
First, ensure `sphinx-build -b gettext ...` has been run successfully to generate `.pot` files in the specified directory. Then, verify that the `--pot-dir` argument points to the correct location.
Upgrade
Version history
2.3.2latest on PyPI · released Aug 2, 2025
Audit
Dependencies
sphinxrequiredSphinx-intl is an extension and utility for Sphinx and requires Sphinx to be installed and used effectively.
Agent activity
3 hits · last 30 days
node
2
Resources
sphinx-intl — pip install sphinx-intl · libregistry