Skip to content

Commit bd6f87d

Browse files
authored
Use Babel errors for the PR body in the transifex workflow (#12562)
Currently, when Babel fails for a locale, the entire PR fails. Instead, just skip compiling the catalogue for that locale, and report the failures in the body of the PR to be more visible.
1 parent 7011459 commit bd6f87d

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

.github/workflows/transifex.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
run: python utils/babel_runner.py extract
3232
- name: Push translations to transifex.com
3333
run: |
34-
cd sphinx/locale
34+
cd sphinx/locale
3535
/tmp/tx_cli/tx push --source --use-git-timestamps --workers 10
3636
env:
3737
TX_TOKEN: ${{ secrets.TX_TOKEN }}
@@ -60,7 +60,7 @@ jobs:
6060
run: python utils/babel_runner.py extract
6161
- name: Pull translations from transifex.com
6262
run: |
63-
cd sphinx/locale
63+
cd sphinx/locale
6464
/tmp/tx_cli/tx pull --translations --all --force --use-git-timestamps --workers 10
6565
env:
6666
TX_TOKEN: ${{ secrets.TX_TOKEN }}
@@ -73,3 +73,4 @@ jobs:
7373
branch: bot/pull-translations
7474
title: "[bot]: Update message catalogues"
7575
labels: "internals:internationalisation"
76+
body-path: babel_compile.txt

utils/babel_runner.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from babel.util import pathmatch
3232
from jinja2.ext import babel_extract as extract_jinja2
3333

34+
IS_CI = 'CI' in os.environ
3435
ROOT = os.path.realpath(os.path.join(os.path.abspath(__file__), '..', '..'))
3536
TEX_DELIMITERS = {
3637
'variable_start_string': '<%=',
@@ -185,11 +186,10 @@ def run_compile() -> None:
185186
log.info('catalog %s is marked as fuzzy, skipping', po_file)
186187
continue
187188

189+
locale_errors = 0
188190
for message, errors in catalog.check():
189-
if locale not in total_errors:
190-
total_errors[locale] = 0
191191
for error in errors:
192-
total_errors[locale] += 1
192+
locale_errors += 1
193193
log.error(
194194
'error: %s:%d: %s\nerror: in message string: %r',
195195
po_file,
@@ -198,6 +198,11 @@ def run_compile() -> None:
198198
message.string,
199199
)
200200

201+
if locale_errors:
202+
total_errors[locale] = locale_errors
203+
log.info('%d errors encountered in %r locale, skipping', locale_errors, locale)
204+
continue
205+
201206
mo_file = os.path.join(directory, locale, 'LC_MESSAGES', 'sphinx.mo')
202207
log.info('compiling catalog %s to %s', po_file, mo_file)
203208
with open(mo_file, 'wb') as outfile:
@@ -229,17 +234,13 @@ def run_compile() -> None:
229234
# to ensure lines end with ``\n`` rather than ``\r\n``:
230235
outfile.write(f'Documentation.addTranslations({obj});'.encode())
231236

232-
if 'ta' in total_errors:
233-
# Tamil is a known failure.
234-
err_count = total_errors.pop('ta')
235-
log.error('%d errors encountered in %r locale.', err_count, 'ta')
236-
237-
if len(total_errors) > 0:
237+
if total_errors:
238+
_write_pr_body_line('## Babel catalogue errors')
239+
_write_pr_body_line('')
238240
for locale, err_count in total_errors.items():
239-
log.error('%d errors encountered in %r locale.', err_count, locale)
240-
log.error('%d errors encountered.', sum(total_errors.values()))
241-
print('Compiling failed.', file=sys.stderr)
242-
raise SystemExit(2)
241+
log.error('error: %d errors encountered in %r locale.', err_count, locale)
242+
s = 's' if err_count != 1 else ''
243+
_write_pr_body_line(f'* {locale}: {err_count} error{s}')
243244

244245

245246
def _get_logger():
@@ -251,6 +252,13 @@ def _get_logger():
251252
return log
252253

253254

255+
def _write_pr_body_line(message: str) -> None:
256+
if not IS_CI:
257+
return
258+
with open('babel_compile.txt', 'a', encoding='utf-8') as f:
259+
f.write(f'{message}\n')
260+
261+
254262
if __name__ == '__main__':
255263
try:
256264
action = sys.argv[1].lower()

0 commit comments

Comments
 (0)