5 # The purpose of this script is to generate various templates supported by
6 # Mailman from the .po files that are translated by translators. The workflow
7 # for the entire translation looks like this:
8 # english template -> en mailman.po -> weblate -> language.po -> language templates.
10 # English templates -> mailman.pot is handled by update_po.py script, which
11 # will copy the contents of the english templates into the .po file for en
12 # language so it can serve as the reference for translators in weblate. After
13 # we get translated .po files back from weblate, we copy the template text
14 # back into individual files of each language using *this script*.
16 from pathlib
import Path
20 from babel
.messages
.pofile
import read_po
, write_po
22 print('Please install `babel` to run this script.')
25 PO_BASE_PATH
= Path('src/mailman/messages')
26 PO_PATH_TEMPLATE
= 'src/mailman/messages/{}/LC_MESSAGES/mailman.po'
27 TEMPLATE_PATH_TEMPLATE
= 'src/mailman/templates/{lang}/{name}'
38 "Read the po file path and return a Catalog object."
39 po_path
= Path(PO_PATH_TEMPLATE
.format(lang
))
40 if not po_path
.exists():
41 print(f
'{po_path} does not exist.')
44 with po_path
.open() as fd
:
49 def write_template(lang
, name
, content
):
50 "Get the template text with the name if it exists."
51 template_path
= Path(TEMPLATE_PATH_TEMPLATE
.format(lang
=lang
, name
=name
))
52 template_path
.parent
.mkdir(exist_ok
=True, parents
=True)
53 if not content
.endswith('\n'):
55 template_path
.write_text(content
)
59 for subdir
in PO_BASE_PATH
.iterdir():
60 if subdir
.name
in IGNORE_PATH_NAMES
:
64 catalog
= get_po(lang
)
67 print(f
'Failed to get catalog for {lang}')
71 if each
.id.endswith('.txt'):
72 if each
.string
.strip() != '':
73 write_template(lang
, each
.id, each
.string
)
75 print(f
'Finished writing templates for {lang}')