Merge branch 'val' into 'master'
[mailman.git] / update_po.py
blob50ec8c49dfce760f27b01274408cff30e3eaa6e1
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 except ImportError:
21 print('Please install `babel` to run this script.')
22 exit(1)
24 PO_PATH = Path('src/mailman/messages/en/LC_MESSAGES/mailman.po')
25 TEMPLATE_BASE_PATH = Path('src/mailman/templates/en')
28 def get_po(path):
29 "Read the po file path and return a Catalog object."
30 with path.open() as fd:
31 catalog = read_po(fd)
32 return catalog
34 def put_po(path, catalog):
35 "Write the catalog object to the po file path."
36 with path.open('bw') as fd:
37 write_po(fd, catalog, include_previous=True, width=85, )
39 def get_template(name):
40 "Get the template text with the name if it exists."
41 template_path = TEMPLATE_BASE_PATH / name
42 if not template_path.exists():
43 return ''
44 return template_path.read_text().rstrip('\n')
47 def main():
48 catalog = get_po(PO_PATH)
49 for each in catalog:
50 if each.id.endswith('.txt'):
51 each.string = get_template(each.id)
52 else:
53 each.string = each.id
54 put_po(PO_PATH, catalog)
55 print(f'Updated {PO_PATH}')
57 main()