1 #! /usr/bin/env python3
5 # The purpose of this script is to generate a multi-lingual en po file for
6 # Mailman Core. Multi-lingual PO files use both a .pot file and a reference po
7 # file. The reference in our case is en and is what is displayed in Weblate
10 # Since there are no utilities to create multi-lingual reference PO file, this
11 # script exists to solve that purpose. Here is how it works:
13 # It will read the default en po file and set all msgstr to be same as
14 # msgid. The only exception to this is email templates, where the msgid is the
15 # filename of the template and msgstr is the content of that file.
17 from pathlib
import Path
21 from babel
.messages
.pofile
import read_po
, write_po
23 print('Please install `babel` to run this script.')
26 PO_PATH
= Path('src/mailman/messages/en/LC_MESSAGES/mailman.po')
27 TEMPLATE_BASE_PATH
= Path('src/mailman/templates/en')
31 "Read the po file path and return a Catalog object."
32 with path
.open() as fd
:
36 def put_po(path
, catalog
):
37 "Write the catalog object to the po file path."
38 with path
.open('bw') as fd
:
39 write_po(fd
, catalog
, include_previous
=True, width
=85, )
41 def get_template(name
):
42 "Get the template text with the name if it exists."
43 template_path
= TEMPLATE_BASE_PATH
/ name
44 if not template_path
.exists():
46 return template_path
.read_text().rstrip('\n')
50 catalog
= get_po(PO_PATH
)
52 if each
.id.endswith('.txt'):
53 each
.string
= get_template(each
.id)
56 put_po(PO_PATH
, catalog
)
57 print(f
'Updated {PO_PATH}')