Repository navigation
doc: add Spanish (es) documentation translations #5112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
88ca324
doc: add Spanish (es) documentation translations
594c9eb
feat(docs): sync shared i18n switcher and RTD build config
5378304
fix(docs): use underscore locale ids for babel po compilation
50ad134
fix(docs): harden i18n build fallback and locale resolution
1c77b43
fix(docs): treat English sub-locales as default and guard switcher key
3a720dd
fix(docs): harden i18n conf import and correct launch translations
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #!/usr/bin/env python3 | ||
| """Compile .po catalogs to .mo before Sphinx html build (RTD + local).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from i18n_locales import KNOWN_LOCALES, resolve_sphinx_language | ||
|
|
||
| try: | ||
| import babel # noqa: F401 | ||
|
|
||
| HAS_BABEL = True | ||
| except ImportError: | ||
| HAS_BABEL = False | ||
|
|
||
| DOC_DIR = Path(__file__).resolve().parent | ||
| LOCALE_DIR = DOC_DIR / "source" / "locale" | ||
|
|
||
|
|
||
| def _messages_dir(locale: str) -> Path: | ||
| return LOCALE_DIR / locale / "LC_MESSAGES" | ||
|
|
||
|
|
||
| def _compile_po_file(po: Path, locale: str) -> None: | ||
| from babel.messages.mofile import write_mo | ||
| from babel.messages.pofile import read_po | ||
|
|
||
| with po.open("rb") as f: | ||
| catalog = read_po(f, locale=locale) | ||
| mo = po.with_suffix(".mo") | ||
| with mo.open("wb") as f: | ||
| write_mo(f, catalog) | ||
|
|
||
|
|
||
| def build_mo(locale: str) -> bool: | ||
| po_root = _messages_dir(locale) | ||
| if not po_root.is_dir(): | ||
| print(f"[build_i18n] skip {locale}: missing {po_root}", flush=True) | ||
| return False | ||
| po_files = sorted(po_root.rglob("*.po")) | ||
| if not po_files: | ||
| print(f"[build_i18n] skip {locale}: no .po files under {po_root}", flush=True) | ||
| return False | ||
|
|
||
| total = len(po_files) | ||
| print(f"[build_i18n] compiling {total} catalogs for locale/{locale} ...", flush=True) | ||
| for index, po in enumerate(po_files, start=1): | ||
| rel = po.relative_to(po_root) | ||
| print(f"[build_i18n] [{index}/{total}] {rel}", flush=True) | ||
| try: | ||
| _compile_po_file(po, locale) | ||
| except Exception as exc: | ||
| print(f"[build_i18n] failed on {rel}: {exc}", flush=True) | ||
| raise | ||
| print(f"[build_i18n] done locale/{locale}", flush=True) | ||
| return True | ||
|
|
||
|
|
||
| def _build_mo_with_sphinx_intl(locale: str) -> bool: | ||
| print(f"[build_i18n] compiling locale/{locale} via sphinx-intl ...", flush=True) | ||
| subprocess.run( | ||
| ["sphinx-intl", "build", "-l", locale, "-d", str(LOCALE_DIR)], | ||
| cwd=DOC_DIR, | ||
| check=True, | ||
| ) | ||
| print(f"[build_i18n] done locale/{locale}", flush=True) | ||
| return True | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "locale", | ||
| nargs="?", | ||
| help="Sphinx locale folder name (e.g. de, zh_CN). Defaults to READTHEDOCS_LANGUAGE.", | ||
| ) | ||
| parser.add_argument( | ||
| "--all", | ||
| action="store_true", | ||
| help="Compile every known locale directory that exists under source/locale/.", | ||
| ) | ||
| args = parser.parse_args(argv) | ||
|
|
||
| if args.all: | ||
| if HAS_BABEL: | ||
| built = [loc for loc in KNOWN_LOCALES if build_mo(loc)] | ||
| if not built: | ||
| print("[build_i18n] no locale directories found") | ||
| else: | ||
| print( | ||
| "[build_i18n] babel not installed, falling back to sphinx-intl " | ||
| "for all locales...", | ||
| flush=True, | ||
| ) | ||
| for loc in KNOWN_LOCALES: | ||
| if _messages_dir(loc).is_dir(): | ||
| _build_mo_with_sphinx_intl(loc) | ||
| return 0 | ||
|
|
||
| locale = resolve_sphinx_language(explicit=args.locale) | ||
| if not locale: | ||
| print("[build_i18n] English build; skipping mo compilation") | ||
| return 0 | ||
|
|
||
| if HAS_BABEL: | ||
| build_mo(locale) | ||
| else: | ||
| _build_mo_with_sphinx_intl(locale) | ||
| return 0 | ||
|
kejhz653-stack marked this conversation as resolved.
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """Shared RTD / Sphinx locale mapping for documentation i18n.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| # RTD READTHEDOCS_LANGUAGE slug (lower case) -> doc/source/locale/ directory name | ||
| RTD_TO_SPHINX_LOCALE: dict[str, str] = { | ||
| "de": "de", | ||
| "es": "es", | ||
| "fr": "fr", | ||
| "it": "it", | ||
| "ja": "ja", | ||
| "ko": "ko", | ||
| "pt-br": "pt_BR", | ||
| "pt_br": "pt_BR", | ||
| "zh-cn": "zh_CN", | ||
| "zh_cn": "zh_CN", | ||
| "zh-tw": "zh_TW", | ||
| "zh_tw": "zh_TW", | ||
| } | ||
|
|
||
| KNOWN_LOCALES: tuple[str, ...] = tuple(sorted(set(RTD_TO_SPHINX_LOCALE.values()))) | ||
|
|
||
|
|
||
| def resolve_sphinx_language( | ||
| rtd_language: str | None = None, | ||
| *, | ||
| explicit: str | None = None, | ||
| ) -> str | None: | ||
| """Return Sphinx ``language`` config value, or ``None`` for English.""" | ||
| slug = ( | ||
| explicit or rtd_language or os.environ.get("READTHEDOCS_LANGUAGE") or "" | ||
| ).strip().lower() | ||
| if not slug or slug == "en" or slug.startswith(("en-", "en_")): | ||
| return None | ||
| return RTD_TO_SPHINX_LOCALE.get(slug, slug.replace("-", "_")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,53 @@ | ||
| [ | ||
| { | ||
| "name": "简体中文(Chinese)", | ||
| "version": "zh-cn", | ||
| "url": "https://inference.readthedocs.io/zh-cn/latest/" | ||
| }, | ||
| { | ||
| "name": "English", | ||
| "version": "en", | ||
| "url": "https://inference.readthedocs.io/en/latest/", | ||
| "preferred": true | ||
| }, | ||
| { | ||
| "name": "简体中文 (Chinese)", | ||
| "version": "zh-cn", | ||
| "url": "https://inference.readthedocs.io/zh-cn/latest/" | ||
| }, | ||
| { | ||
| "name": "繁體中文 (Traditional Chinese)", | ||
| "version": "zh-tw", | ||
| "url": "https://inference.readthedocs.io/zh-tw/latest/" | ||
| }, | ||
| { | ||
| "name": "日本語 (Japanese)", | ||
| "version": "ja", | ||
| "url": "https://inference.readthedocs.io/ja/latest/" | ||
| }, | ||
| { | ||
| "name": "한국어 (Korean)", | ||
| "version": "ko", | ||
| "url": "https://inference.readthedocs.io/ko/latest/" | ||
| }, | ||
| { | ||
| "name": "Deutsch (German)", | ||
| "version": "de", | ||
| "url": "https://inference.readthedocs.io/de/latest/" | ||
| }, | ||
| { | ||
| "name": "Français (French)", | ||
| "version": "fr", | ||
| "url": "https://inference.readthedocs.io/fr/latest/" | ||
| }, | ||
| { | ||
| "name": "Español (Spanish)", | ||
| "version": "es", | ||
| "url": "https://inference.readthedocs.io/es/latest/" | ||
| }, | ||
| { | ||
| "name": "Italiano (Italian)", | ||
| "version": "it", | ||
| "url": "https://inference.readthedocs.io/it/latest/" | ||
| }, | ||
| { | ||
| "name": "Português (Portuguese)", | ||
| "version": "pt-br", | ||
| "url": "https://inference.readthedocs.io/pt-br/latest/" | ||
| } | ||
| ] | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.