Merge branch 'task' into 'master'
[mailman.git] / update_po.py
blob7e4b706a4159a2d5ff732ae10115e813de9626f6
1 #! /usr/bin/env python3
2 # Author: Abhilash Raj
3 # Date: Sept 26 2020
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
8 # when translating.
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
20 try:
21 from babel.messages.pofile import read_po, write_po
22 except ImportError:
23 print('Please install `babel` to run this script.')
24 exit(1)
26 PO_PATH = Path('src/mailman/messages/en/LC_MESSAGES/mailman.po')
27 TEMPLATE_BASE_PATH = Path('src/mailman/templates/en')
30 def get_po(path):
31 "Read the po file path and return a Catalog object."
32 with path.open() as fd:
33 catalog = read_po(fd)
34 return catalog
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():
45 return ''
46 return template_path.read_text().rstrip('\n')
49 def main():
50 catalog = get_po(PO_PATH)
51 for each in catalog:
52 if each.id.endswith('.txt'):
53 each.string = get_template(each.id)
54 else:
55 each.string = each.id
56 put_po(PO_PATH, catalog)
57 print(f'Updated {PO_PATH}')
59 main()