|
1 | 1 | """Tests for the Tools/i18n/msgfmt.py tool.""" |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import sys |
4 | 5 | import unittest |
5 | 6 | from gettext import GNUTranslations |
@@ -39,6 +40,28 @@ def test_compilation(self): |
39 | 40 |
|
40 | 41 | self.assertDictEqual(actual._catalog, expected._catalog) |
41 | 42 |
|
| 43 | + def test_translations(self): |
| 44 | + with open(data_dir / 'general.mo', 'rb') as f: |
| 45 | + t = GNUTranslations(f) |
| 46 | + |
| 47 | + self.assertEqual(t.gettext('foo'), 'foo') |
| 48 | + self.assertEqual(t.gettext('bar'), 'baz') |
| 49 | + self.assertEqual(t.pgettext('abc', 'foo'), 'bar') |
| 50 | + self.assertEqual(t.pgettext('xyz', 'foo'), 'bar') |
| 51 | + self.assertEqual(t.gettext('Multilinestring'), 'Multilinetranslation') |
| 52 | + self.assertEqual(t.gettext('"escapes"'), '"translated"') |
| 53 | + self.assertEqual(t.gettext('\n newlines \n'), '\n translated \n') |
| 54 | + self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 1), |
| 55 | + 'One email sent.') |
| 56 | + self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 2), |
| 57 | + '%d emails sent.') |
| 58 | + self.assertEqual(t.npgettext('abc', 'One email sent.', |
| 59 | + '%d emails sent.', 1), |
| 60 | + 'One email sent.') |
| 61 | + self.assertEqual(t.npgettext('abc', 'One email sent.', |
| 62 | + '%d emails sent.', 2), |
| 63 | + '%d emails sent.') |
| 64 | + |
42 | 65 | def test_invalid_msgid_plural(self): |
43 | 66 | with temp_cwd(): |
44 | 67 | Path('invalid.po').write_text('''\ |
@@ -117,6 +140,16 @@ def update_catalog_snapshots(): |
117 | 140 | for po_file in data_dir.glob('*.po'): |
118 | 141 | mo_file = po_file.with_suffix('.mo') |
119 | 142 | compile_messages(po_file, mo_file) |
| 143 | + # Create a human-readable JSON file which is |
| 144 | + # easier to review than the binary .mo file. |
| 145 | + with open(mo_file, 'rb') as f: |
| 146 | + translations = GNUTranslations(f) |
| 147 | + catalog_file = po_file.with_suffix('.json') |
| 148 | + with open(catalog_file, 'w') as f: |
| 149 | + data = translations._catalog.items() |
| 150 | + data = sorted(data, key=lambda x: (isinstance(x[0], tuple), x[0])) |
| 151 | + json.dump(data, f, indent=4) |
| 152 | + f.write('\n') |
120 | 153 |
|
121 | 154 |
|
122 | 155 | if __name__ == '__main__': |
|
0 commit comments