Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions doc/fix_po_rest_colons.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,38 @@ def _fix_msgstr(msgid: str, msgstr: str) -> str | None:
return fixed if fixed != msgstr else None
if msgstr.endswith(":::"):
return msgstr[:-1]
if msgstr.endswith(":"):
return msgstr[:-1] + "::"
if msgstr.endswith(":"):
return msgstr[:-1] + "::"
return None
for suffix in (":", ":", "。", ".", "!", "!", "?", "?"):
if msgstr.endswith(suffix):
return msgstr[: -len(suffix)] + "::"
return msgstr + "::"


def _write_msgstr(lines: list[str], start: int, end: int, msgstr: str) -> list[str]:
old_chunks: list[str] = []
for line in lines[start : end + 1]:
if line.startswith("msgstr "):
old_chunks.append(line[7:].strip().strip('"'))
elif line.startswith('"'):
old_chunks.append(line.strip().strip('"'))
old_msgstr = "".join(old_chunks)

if old_msgstr and old_msgstr != msgstr:
if old_msgstr.endswith("::") and msgstr.endswith("::"):
suffix_len = len(old_msgstr) - len(old_msgstr.rstrip(":").rstrip(":"))
else:
suffix_len = 0
if suffix_len and msgstr.endswith("::"):
prefix = msgstr[: -len("::")]
old_prefix = old_msgstr[: -suffix_len] if suffix_len else old_msgstr
if prefix == old_prefix or prefix.startswith(old_prefix.rstrip("::")):
msgstr_lines = lines[start : end + 1]
last = msgstr_lines[-1]
if last.startswith("msgstr "):
msgstr_lines[-1] = f'msgstr "{prefix}::"\n'
else:
msgstr_lines[-1] = f'"{prefix}::"\n'
return lines[:start] + msgstr_lines + lines[end + 1 :]

wrapped: list[str] = []
chunk_size = 70
parts = [msgstr[i : i + chunk_size] for i in range(0, len(msgstr), chunk_size)] or [""]
Expand Down
5 changes: 4 additions & 1 deletion doc/scan_po_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def main() -> int:
help="Locale directories to scan",
)
args = parser.parse_args()
exit_code = 0
for locale in args.locales:
if not (LOCALE_DIR / locale).is_dir():
print(f"[skip] missing locale/{locale}")
Expand All @@ -111,7 +112,9 @@ def main() -> int:
print(f" - {item}")
if len(items) > 8:
print(f" ... and {len(items) - 8} more")
return 0
if items:
exit_code = 1
return exit_code


if __name__ == "__main__":
Expand Down
42 changes: 41 additions & 1 deletion doc/source/_static/custom.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Keep the PyData header compact and aligned for the English navigation. */
/* Keep the PyData header compact and aligned across locales. */
@media (min-width: 992px) {
.bd-header .bd-header__inner {
gap: 1rem;
Expand Down Expand Up @@ -28,6 +28,7 @@
}

.bd-header .navbar-header-items__center {
flex: 1 1 auto;
min-width: 0;
}

Expand All @@ -38,7 +39,10 @@
}

.bd-header .bd-navbar-elements {
display: flex;
flex-wrap: nowrap;
gap: 0.25rem;
min-width: 0;
}

.bd-header .bd-navbar-elements .nav-link,
Expand All @@ -60,6 +64,38 @@
.bd-header .search-button__button {
max-width: 11.5rem;
}

/* Translated nav labels are wider; keep the header on one row. */
html:not([lang="en"]) .bd-header .bd-header__inner {
gap: 0.65rem;
}

html:not([lang="en"]) .bd-header .bd-navbar-elements {
gap: 0.15rem;
}

html:not([lang="en"]) .bd-header .bd-navbar-elements .nav-link,
html:not([lang="en"]) .bd-header .bd-navbar-elements .dropdown-toggle {
font-size: 0.88rem;
padding: 0.45rem 0.5rem;
}

html:not([lang="en"]) .bd-header .version-switcher__button {
font-size: 0.85rem;
max-width: 9.5rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

html:not([lang="en"]) .bd-header .search-button__button {
max-width: 9rem;
}

html:not([lang="en"]) .bd-header .navbar-header-items__end {
gap: 0.25rem;
margin-left: 0.5rem;
}
}

.bd-header .navbar-brand {
Expand Down Expand Up @@ -87,7 +123,11 @@
border-radius: 0.45rem;
font-weight: 600;
line-height: 1.2;
max-width: 11rem;
overflow: hidden;
padding: 0.45rem 0.6rem;
text-overflow: ellipsis;
white-space: nowrap;
}

.bd-header .search-button__button {
Expand Down
109 changes: 109 additions & 0 deletions doc/source/init_locales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env python3
# Copyright 2022-2026 XProbe Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Scaffold documentation locale .po files from the zh_CN template."""

from __future__ import annotations

import argparse
from pathlib import Path

from babel.messages import pofile

LOCALE_ROOT = Path(__file__).resolve().parent / "locale"
SOURCE_LOCALE = "zh_CN"
TARGET_LOCALES = ("zh_TW", "ja", "ko", "de", "fr", "es", "it", "pt_BR")


def _clear_message_string(message) -> None:
if isinstance(message.string, (list, tuple)):
message.string = type(message.string)("" for _ in message.string)
else:
message.string = ""


def _clear_translations(catalog, language: str) -> None:
catalog.locale = language
catalog.language_team = f"{language} <LL@li.org>"
for message in catalog:
message.flags.discard("fuzzy")
if not message.id:
continue
_clear_message_string(message)
# Also clear obsolete entries so zh_CN translations don't leak into other locales
catalog.obsolete.clear()


def scaffold_locale(source_po: Path, target_po: Path, language: str) -> None:
with source_po.open("rb") as inpf:
catalog = pofile.read_po(inpf, locale=language)
_clear_translations(catalog, language)
target_po.parent.mkdir(parents=True, exist_ok=True)
with target_po.open("wb") as outf:
pofile.write_po(outf, catalog, width=76)


def main() -> None:
parser = argparse.ArgumentParser(
description="Create empty .po scaffolds for additional doc locales."
)
parser.add_argument(
"--source-locale",
default=SOURCE_LOCALE,
help=f"Template locale directory name (default: {SOURCE_LOCALE})",
)
parser.add_argument(
"--locales",
nargs="+",
default=list(TARGET_LOCALES),
help="Target locale directory names to scaffold.",
)
parser.add_argument(
"--force",
action="store_true",
help="Overwrite existing target .po files.",
)
args = parser.parse_args()

source_root = LOCALE_ROOT / args.source_locale / "LC_MESSAGES"
if not source_root.is_dir():
raise SystemExit(f"Source locale not found: {source_root}")

source_files = sorted(source_root.rglob("*.po"))
if not source_files:
raise SystemExit(f"No .po files found under {source_root}")

created = 0
skipped = 0
for language in args.locales:
if language == args.source_locale:
continue
for source_po in source_files:
rel_path = source_po.relative_to(source_root)
target_po = LOCALE_ROOT / language / "LC_MESSAGES" / rel_path
if target_po.exists() and not args.force:
skipped += 1
continue
scaffold_locale(source_po, target_po, language)
created += 1

print(
f"Scaffolded {created} .po file(s) for {len(args.locales)} locale(s); "
f"skipped {skipped} existing file(s)."
)


if __name__ == "__main__":
main()
Loading
Loading