Skip to content

Commit

Permalink
Fixed the issue sphinx-doc#10104 after the code reviews from Adam Tur…
Browse files Browse the repository at this point in the history
…ner and Takeshi Komiya:

- Added the class variable 'builder' to Catalog class, so any instances of Catalog can access the information within the builder, including things such as app, config, srcdir, outdir etc.. see MessageCatalogBuilder for details.
- Initialise this Catalog.builder as the instance of MessageCatalogBuilder is created, ie. in the init()
- As Message class is created, list of locations passing to it will be made singular, using set(), sorted alphabetically for easier searching and identifying origins where the message can be found.
- source dir is used within the __iter__ of the Catalog, using sphinx.util.osutil.relpath, for safer functionality, to work out the relative path for each location entry.
  • Loading branch information
hoangduytranuk committed Apr 24, 2022
1 parent 9fa160a commit ac71858
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions sphinx/builders/gettext.py
Expand Up @@ -32,13 +32,14 @@ class Message:
"""An entry of translatable message."""
def __init__(self, text: str, locations: List[Tuple[str, int]], uuids: List[str]):
self.text = text
self.locations = list(set(locations))
self.locations = list(sorted(list(set(locations)))) #make list unique and sorted alphabetically
self.uuids = uuids


class Catalog:
"""Catalog of translatable messages."""

# make a class var so any instance can share this parent class, where all building info can be found, such as srcdir etc..
builder = None
def __init__(self) -> None:
self.messages: List[str] = [] # retain insertion order, a la OrderedDict

Expand All @@ -56,8 +57,9 @@ def add(self, msg: str, origin: Union[Element, "MsgOrigin"]) -> None:
self.metadata[msg].append((origin.source, origin.line, origin.uid)) # type: ignore

def __iter__(self) -> Generator[Message, None, None]:
src_dir = Catalog.builder.srcdir
for message in self.messages:
positions = [(os.path.relpath(source, start=os.getcwd()), line) for source, line, uuid in self.metadata[message]]
positions = [(relpath(source, start=src_dir), line) for source, line, uuid in self.metadata[message]]
uuids = [uuid for source, line, uuid in self.metadata[message]]
yield Message(message, positions, uuids)

Expand Down Expand Up @@ -213,7 +215,8 @@ class MessageCatalogBuilder(I18nBuilder):
def init(self) -> None:
super().init()
self.create_template_bridge()
self.templates.init(self)
self.templates.init(self)
Catalog.builder = self # make the builder visible to instances of catalog

def _collect_templates(self) -> Set[str]:
template_files = set()
Expand Down

0 comments on commit ac71858

Please sign in to comment.