10000 gh-126609: localize list, allow translation of a phrase, use ge operator character in the availability directive by m-aciek · Pull Request #129597 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126609: localize list, allow translation of a phrase, use ge operator character in the availability directive #129597

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Docs: allow translation of a phrase in availability directive
  • Loading branch information
m-aciek committed Feb 2, 2025
commit a2399c0fc1c57e9a3d51c4594a5cd1fbcfe4475c
20 changes: 18 additions & 2 deletions Doc/tools/extensions/availability.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from itertools import starmap
from typing import TYPE_CHECKING

from docutils import nodes
Expand All @@ -11,6 +12,8 @@
from sphinx.util.docutils import SphinxDirective

if TYPE_CHECKING:
from collections.abc import Callable

from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata

Expand Down Expand Up @@ -49,6 +52,16 @@
KNOWN_PLATFORMS = _PLATFORMS | _LIBC | _THREADING


def _print_platform(
platform: str, version: str | bool
) -> str | Callable[[str], str]:
if version is True:
return platform
if not version:
return sphinx_gettext("not {platform}").format(platform=platform)
return f"{platform} >= {version}"


class Availability(SphinxDirective):
has_content = True
required_arguments = 1
Expand All @@ -68,14 +81,17 @@ def run(self) -> list[nodes.container]:
refwarn=True,
)
sep = nodes.Text(": ")
parsed, msgs = self.state.inline_text(self.arguments[0], self.lineno)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is somewhat surprising that this doesn't work, as sphinx.util.nodes.extract_messages() should pick up the rawsource attributes -- we shouldn't need to parse & reparse.

Copy link
Contributor Author
@m-aciek m-aciek Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even when I've added not WASI, not Android. translation string by hand, it wasn't picked up in the build process.

platforms = self.parse_platforms()
platforms_text = (
f"{', '.join(starmap(_print_platform, platforms.items()))}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if babel is available here, but format_list might be a better option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Babel is Sphinx's dependency, it is available. Thank you for the suggestion, I've added it.

)
parsed, msgs = self.state.inline_text(platforms_text, self.lineno)
pnode = nodes.paragraph(title, "", refnode, sep, *parsed, *msgs)
self.set_source_info(pnode)
cnode = nodes.container("", pnode, classes=["availability"])
self.set_source_info(cnode)
if self.content:
self.state.nested_parse(self.content, self.content_offset, cnode)
self.parse_platforms()

return [cnode]

Expand Down
1 change: 1 addition & 0 deletions Doc/tools/templates/dummy.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
In extensions/availability.py:

{% trans %}Availability{% endtrans %}
{% trans %}not {platform}{% endtrans %}

In extensions/c_annotations.py:

Expand Down
0