Merge branch 'digest' into 'master'
[mailman.git] / update_lang_templates.py
blob5a655f2e71fa8a37686102796a17af460c11bdea
1 #! /usr/bin/env python
2 # Author: Abhilash Raj
3 # Date: Aug 21, 2021
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
19 try:
20 from babel.messages.pofile import read_po, write_po
21 except ImportError:
22 print('Please install `babel` to run this script.')
23 exit(1)
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}'
29 IGNORE_PATH_NAMES = [
30 '__init__.py',
31 '__pycache__',
32 'en',
33 'mailman.pot',
37 def get_po(lang):
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.')
42 return None
44 with po_path.open() as fd:
45 catalog = read_po(fd)
46 return catalog
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'):
54 content += '\n'
55 template_path.write_text(content)
58 def main():
59 for subdir in PO_BASE_PATH.iterdir():
60 if subdir.name in IGNORE_PATH_NAMES:
61 continue
63 lang = subdir.name
64 catalog = get_po(lang)
66 if catalog is None:
67 print(f'Failed to get catalog for {lang}')
68 continue
70 for each in catalog:
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}')
77 main()