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
23 changes: 23 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from glob import glob
from pathlib import Path

from babel.messages.mofile import write_mo
from babel.messages.pofile import read_po
from docutils.nodes import make_id
from ocds_babel.translate import translate
from ocdskit.mapping_sheet import mapping_sheet
Expand Down Expand Up @@ -115,6 +117,16 @@
]


def compile_catalogs(localedir, language, domains):
"""Compile the catalogs that `translate` reads, as `pybabel compile --use-fuzzy` does."""
for domain in domains:
path = localedir / language / "LC_MESSAGES" / f"{domain}.po"
with path.open() as f:
catalog = read_po(f, locale=language, domain=domain)
with path.with_suffix(".mo").open("wb") as f:
write_mo(f, catalog, use_fuzzy=True)


def setup(app):
# The root of the repository.
basedir = Path(__file__).resolve().parents[1]
Expand All @@ -132,8 +144,19 @@ def setup(app):
standard_dir = basedir / "schema"
standard_build_dir = basedir / "build" / language

# `build/current_lang` paths in directives resolve to the language being built.
standard_build_dir.parent.mkdir(parents=True, exist_ok=True)
current_lang = standard_build_dir.parent / "current_lang"
current_lang.unlink(missing_ok=True)
current_lang.symlink_to(language)
app.connect("build-finished", lambda *args: current_lang.unlink(missing_ok=True))

branch = os.getenv("GITHUB_REF_NAME", "latest")

# English is the source language, and has no catalogs.
if language != "en":
compile_catalogs(localedir, language, (schema_domain, codelists_domain))

translate(
[
# The glob patterns in `babel_ocds_schema.cfg` should match these filenames.
Expand Down
35 changes: 11 additions & 24 deletions include/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ extract_schema: $(POT_DIR)
# directives to succeed, but the contents of the files have no effect on the generated .pot files.
# See https://www.sphinx-doc.org/en/master/usage/builders/index.html#sphinx.builders.gettext.MessageCatalogBuilder
.PHONY: extract_markdown
extract_markdown: current_lang.en
extract_markdown:
sphinx-build -nW --keep-going -q -b gettext $(DOCS_DIR) $(POT_DIR)

.PHONY: extract
extract: extract_theme extract_codelists extract_schema $(EXTRACT_TARGETS) extract_markdown clean_current_lang
extract: extract_theme extract_codelists extract_schema $(EXTRACT_TARGETS) extract_markdown

$(TRANSLATIONS:.%=docs/locale/%): docs/locale/%: FORCE
sphinx-intl update -p $(POT_DIR) -d $(LOCALE_DIR) -l "$*"
Expand All @@ -63,43 +63,30 @@ docs/locale: $(TRANSLATIONS:.%=docs/locale/%)
pocount:
find $(LOCALE_DIR) -name LC_MESSAGES -exec pocount --incomplete --short "{}" +

### Current language

# Create a symlink for the language, so that file paths in `jsonschema` directives resolve.
# (Don't use clean_current_lang as a prerequisite, as then it won't run as a prerequisite later.)
$(LANGUAGES:.%=current_lang.%): current_lang.%: $(BUILD_DIR)
rm -f $(BUILD_DIR)/current_lang
ln -s $* $(BUILD_DIR)/current_lang

# Deploy script complains if current_lang is present.
.PHONY: clean_current_lang
clean_current_lang:
rm $(BUILD_DIR)/current_lang

### Build

# Build the source documentation.
# See https://www.sphinx-doc.org/en/master/usage/builders/index.html#sphinx.builders.html.DirectoryHTMLBuilder
.PHONY: build_source
build_source: current_lang.en
build_source:
sphinx-build -nW --keep-going -q -b dirhtml $(DOCS_DIR) $(BUILD_DIR)/en

# Build the translated documentation. (Same as source, but with a language configuration setting.)
$(TRANSLATIONS:.%=build.%): build.%: current_lang.%
$(TRANSLATIONS:.%=build.%): build.%:
sphinx-build -nW --keep-going -q -b dirhtml $(DOCS_DIR) $(BUILD_DIR)/$* -D language="$*"

.PHONY: source
source: build_source clean_current_lang
source: build_source

$(TRANSLATIONS:.%=%): %: build_source compile build.% clean_current_lang
$(TRANSLATIONS:.%=%): %: build_source build.%

.PHONY: all
all: build_source compile $(TRANSLATIONS:.%=build.%) clean_current_lang
all: build_source $(TRANSLATIONS:.%=build.%)

### Development

.PHONY: autobuild
autobuild: current_lang.en
autobuild:
sphinx-autobuild $(SPHINX_AUTOBUILD_EXTRA_ARGS) -nW -q -b dirhtml $(DOCS_DIR) $(BUILD_DIR)/en

.PHONY: update
Expand All @@ -111,14 +98,14 @@ update: clean_dist
# "-" ignores the exit status. Schema files might contain old URLs that redirect, which can only be updated in a new version.

.PHONY: linkcheck_source
linkcheck_source: current_lang.en
linkcheck_source:
-sphinx-build -q -b linkcheck $(DOCS_DIR) $(BUILD_DIR)/en

$(TRANSLATIONS:.%=linkcheck.%): linkcheck.%: current_lang.%
$(TRANSLATIONS:.%=linkcheck.%): linkcheck.%:
-sphinx-build -q -b linkcheck $(DOCS_DIR) $(BUILD_DIR)/$* -D language="$*"

.PHONY: linkcheck
linkcheck: linkcheck_source compile $(TRANSLATIONS:.%=linkcheck.%) clean_current_lang
linkcheck: linkcheck_source $(TRANSLATIONS:.%=linkcheck.%)

### PDF generation

Expand Down
6 changes: 0 additions & 6 deletions include/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,4 @@ PDF_PAGES={,primer/{,what/,how/,releases_and_records/,next/},guidance/{,design/,
# be an indication of an iframe taking too long to load."
PDF_DELAY=20000

# Compile PO files for codelists and schema to MO files, so that `translate` succeeds.
.PHONY: compile
compile:
pybabel compile --use-fuzzy -d $(LOCALE_DIR) -D $(DOMAIN_PREFIX)schema
pybabel compile --use-fuzzy -d $(LOCALE_DIR) -D $(DOMAIN_PREFIX)codelists

# Put local targets below.
Loading