Removed spurious .mailman.po.swp file.
[mailman.git] / update_po.py
blobea3657b9a1d9bc9852f7bf0f75c4382c8b9fc7ed
1 #! /usr/bin/env python
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
18 try:
19 from babel.messages.pofile import read_po, write_po
20 from babel.messages.catalog import Catalog, Message
21 except ImportError:
22 print('Please install `babel` to run this script.')
23 exit(1)
25 PO_PATH = Path('src/mailman/messages/en/LC_MESSAGES/mailman.po')
26 TEMPLATE_BASE_PATH = Path('src/mailman/templates/en')
29 def get_po(path):
30 "Read the po file path and return a Catalog object."
31 with path.open() as fd:
32 catalog = read_po(fd)
33 return catalog
35 def put_po(path, catalog):
36 "Write the catalog object to the po file path."
37 with path.open('bw') as fd:
38 write_po(fd, catalog, include_previous=True, width=85, )
40 def get_template(name):
41 "Get the template text with the name if it exists."
42 template_path = TEMPLATE_BASE_PATH / name
43 if not template_path.exists():
44 return ''
45 template_path.read_text().rstrip('\n')
48 def main():
49 catalog = get_po(PO_PATH)
50 for each in catalog:
51 if each.id.endswith('.txt'):
52 each.string = get_template(each.id)
53 else:
54 each.string = each.id
55 put_po(PO_PATH, catalog)
56 print(f'Updated {PO_PATH}')
58 main()