Skip to content

Commit 5329d39

Browse files
author
jiangjiawei
committed
fix(docs): sync i18n build infrastructure from es branch
1 parent 3531f1f commit 5329d39

3 files changed

Lines changed: 56 additions & 53 deletions

File tree

doc/build_i18n.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
from i18n_locales import KNOWN_LOCALES, resolve_sphinx_language
1212

13+
try:
14+
import babel # noqa: F401
15+
16+
HAS_BABEL = True
17+
except ImportError:
18+
HAS_BABEL = False
19+
1320
DOC_DIR = Path(__file__).resolve().parent
1421
LOCALE_DIR = DOC_DIR / "source" / "locale"
1522

@@ -64,21 +71,6 @@ def _build_mo_with_sphinx_intl(locale: str) -> bool:
6471
return True
6572

6673

67-
def _babel_available() -> bool:
68-
try:
69-
import babel # noqa: F401
70-
71-
return True
72-
except ImportError:
73-
return False
74-
75-
76-
def _build_locale(locale: str, *, has_babel: bool) -> bool:
77-
if has_babel:
78-
return build_mo(locale)
79-
return _build_mo_with_sphinx_intl(locale)
80-
81-
8274
def main(argv: list[str] | None = None) -> int:
8375
parser = argparse.ArgumentParser(description=__doc__)
8476
parser.add_argument(
@@ -93,25 +85,31 @@ def main(argv: list[str] | None = None) -> int:
9385
)
9486
args = parser.parse_args(argv)
9587

96-
has_babel = _babel_available()
97-
9888
if args.all:
99-
built: list[str] = []
100-
for loc in KNOWN_LOCALES:
101-
if not _messages_dir(loc).is_dir():
102-
continue
103-
if _build_locale(loc, has_babel=has_babel):
104-
built.append(loc)
105-
if not built:
106-
print("[build_i18n] no locale directories found")
89+
if HAS_BABEL:
90+
built = [loc for loc in KNOWN_LOCALES if build_mo(loc)]
91+
if not built:
92+
print("[build_i18n] no locale directories found")
93+
else:
94+
print(
95+
"[build_i18n] babel not installed, falling back to sphinx-intl "
96+
"for all locales...",
97+
flush=True,
98+
)
99+
for loc in KNOWN_LOCALES:
100+
if _messages_dir(loc).is_dir():
101+
_build_mo_with_sphinx_intl(loc)
107102
return 0
108103

109104
locale = resolve_sphinx_language(explicit=args.locale)
110105
if not locale:
111106
print("[build_i18n] English build; skipping mo compilation")
112107
return 0
113108

114-
_build_locale(locale, has_babel=has_babel)
109+
if HAS_BABEL:
110+
build_mo(locale)
111+
else:
112+
_build_mo_with_sphinx_intl(locale)
115113
return 0
116114

117115

doc/i18n_locales.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def resolve_sphinx_language(
2929
explicit: str | None = None,
3030
) -> str | None:
3131
"""Return Sphinx ``language`` config value, or ``None`` for English."""
32-
if explicit:
33-
return explicit
34-
slug = (rtd_language or os.environ.get("READTHEDOCS_LANGUAGE") or "").strip().lower()
35-
if not slug or slug == "en":
32+
slug = (
33+
explicit or rtd_language or os.environ.get("READTHEDOCS_LANGUAGE") or ""
34+
).strip().lower()
35+
if not slug or slug == "en" or slug.startswith(("en-", "en_")):
3636
return None
3737
return RTD_TO_SPHINX_LOCALE.get(slug, slug.replace("-", "_"))

doc/source/conf.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,24 @@ def _compile_mo_catalog(locale: str) -> None:
149149
"""Compile .po -> .mo when catalogs are missing or stale."""
150150
if not _locale_po_files(locale):
151151
return
152-
subprocess.run(
153-
[sys.executable, str(_doc_root / "build_i18n.py"), locale],
154-
cwd=str(_doc_root),
155-
check=True,
156-
)
152+
try:
153+
subprocess.run(
154+
[sys.executable, str(_doc_root / "build_i18n.py"), locale],
155+
cwd=str(_doc_root),
156+
check=True,
157+
)
158+
except (subprocess.CalledProcessError, OSError) as exc:
159+
print(
160+
f"[sphinx] warning: failed to compile locale/{locale} catalogs "
161+
f"({exc}); continuing with existing .mo files if any",
162+
flush=True,
163+
)
157164

158165

159166
if _sphinx_language and _needs_mo_compile(_sphinx_language):
160167
_compile_mo_catalog(_sphinx_language)
161168

162-
if version_match == "zh-cn" or _sphinx_language == "zh_CN":
169+
if version_match == 'zh-cn' or _sphinx_language == "zh_CN":
163170
tags.add("zh_cn")
164171

165172

@@ -205,20 +212,7 @@ def _resolve_switcher_version(app):
205212
}
206213

207214

208-
def _apply_switcher_theme_options(theme_options: dict, switcher_version: str) -> None:
209-
"""Apply locale-specific PyData theme options from one place."""
210-
switcher = theme_options.setdefault("switcher", {})
211-
switcher.setdefault("json_url", json_url)
212-
switcher["version_match"] = switcher_version
213-
theme_options["external_links"] = [
214-
_EXTERNAL_LINKS_BY_LOCALE.get(switcher_version, _DEFAULT_EXTERNAL_LINK)
215-
]
216-
theme_options["header_dropdown_text"] = _HEADER_DROPDOWN_TEXT_BY_LOCALE.get(
217-
switcher_version, "More"
218-
)
219-
220-
221-
if version_match != "zh-cn":
215+
if version_match != 'zh-cn':
222216
html_theme_options['icon_links'].extend([{
223217
"name": "Discord",
224218
"url": "https://discord.gg/Xw9tszSkr5",
@@ -240,16 +234,27 @@ def _apply_switcher_theme_options(theme_options: dict, switcher_version: str) ->
240234
"type": "fontawesome",
241235
}])
242236

243-
_apply_switcher_theme_options(html_theme_options, version_match)
237+
html_theme_options["external_links"] = [
238+
_EXTERNAL_LINKS_BY_LOCALE.get(version_match, _DEFAULT_EXTERNAL_LINK)
239+
]
240+
html_theme_options["header_dropdown_text"] = _HEADER_DROPDOWN_TEXT_BY_LOCALE.get(
241+
version_match, "More"
242+
)
244243

245244
html_favicon = "_static/xinference-favicon.png"
246245

247246

248247
def _apply_locale_theme_options(app, config):
249248
switcher_version = _resolve_switcher_version(app)
250-
_apply_switcher_theme_options(config.html_theme_options, switcher_version)
249+
config.html_theme_options.setdefault("switcher", {})["version_match"] = switcher_version
250+
config.html_theme_options["external_links"] = [
251+
_EXTERNAL_LINKS_BY_LOCALE.get(switcher_version, _DEFAULT_EXTERNAL_LINK)
252+
]
253+
config.html_theme_options["header_dropdown_text"] = (
254+
_HEADER_DROPDOWN_TEXT_BY_LOCALE.get(switcher_version, "More")
255+
)
251256
if switcher_version == "zh-cn":
252-
config.tags.add("zh_cn")
257+
app.tags.add("zh_cn")
253258

254259

255260
def _remove_non_zh_cn_nodes(app, doctree, docname):

0 commit comments

Comments
 (0)