1010
1111import filecmp
1212import json
13- import os
1413import shutil
1514import sys
1615import unittest
2120from test .support .script_helper import assert_python_failure , assert_python_ok
2221from test .test_tools import skip_if_missing , toolsdir
2322
24-
2523skip_if_missing ('i18n' )
2624
2725data_dir = (Path (__file__ ).parent / 'msgfmt_data' ).resolve ()
@@ -62,16 +60,10 @@ def test_translations(self):
6260 self .assertEqual (t .gettext ('Multilinestring' ), 'Multilinetranslation' )
6361 self .assertEqual (t .gettext ('"escapes"' ), '"translated"' )
6462 self .assertEqual (t .gettext ('\n newlines \n ' ), '\n translated \n ' )
65- self .assertEqual (t .ngettext ('One email sent.' , '%d emails sent.' , 1 ),
66- 'One email sent.' )
67- self .assertEqual (t .ngettext ('One email sent.' , '%d emails sent.' , 2 ),
68- '%d emails sent.' )
69- self .assertEqual (t .npgettext ('abc' , 'One email sent.' ,
70- '%d emails sent.' , 1 ),
71- 'One email sent.' )
72- self .assertEqual (t .npgettext ('abc' , 'One email sent.' ,
73- '%d emails sent.' , 2 ),
74- '%d emails sent.' )
63+ self .assertEqual (t .ngettext ('One email sent.' , '%d emails sent.' , 1 ), 'One email sent.' )
64+ self .assertEqual (t .ngettext ('One email sent.' , '%d emails sent.' , 2 ), '%d emails sent.' )
65+ self .assertEqual (t .npgettext ('abc' , 'One email sent.' , '%d emails sent.' , 1 ), 'One email sent.' )
66+ self .assertEqual (t .npgettext ('abc' , 'One email sent.' , '%d emails sent.' , 2 ), '%d emails sent.' )
7567
7668 def test_po_with_bom (self ):
7769 with temp_cwd ():
@@ -165,9 +157,7 @@ def test_no_outputfile(self):
165157 with temp_cwd (None ):
166158 shutil .copy (data_dir / 'file2_fr_lf.po' , '.' )
167159 assert_python_ok (msgfmt , 'file2_fr_lf.po' )
168- self .assertTrue (
169- filecmp .cmp (data_dir / 'file2_fr_lf.mo' , 'file2_fr_lf.mo' ),
170- 'Wrong compiled file2_fr_lf.mo' )
160+ self .assertTrue (filecmp .cmp (data_dir / 'file2_fr_lf.mo' , 'file2_fr_lf.mo' ), 'Wrong compiled file2_fr_lf.mo' )
171161
172162 def test_both_with_outputfile (self ):
173163 """Test script with -o option and 2 input files
@@ -179,11 +169,8 @@ def test_both_with_outputfile(self):
179169 Unix endings (lf)
180170 """
181171 with temp_cwd (None ):
182- assert_python_ok (msgfmt , '-o' , 'file12.mo' ,
183- data_dir / 'file1_fr_crlf.po' ,
184- data_dir / 'file2_fr_lf.po' )
185- self .assertTrue (
186- filecmp .cmp (data_dir / 'file12_fr.mo' , 'file12.mo' ))
172+ assert_python_ok (msgfmt , '-o' , 'file12.mo' , data_dir / 'file1_fr_crlf.po' , data_dir / 'file2_fr_lf.po' )
173+ self .assertTrue (filecmp .cmp (data_dir / 'file12_fr.mo' , 'file12.mo' ))
187174
188175 def test_both_without_outputfile (self ):
189176 """Test script without -o option and 2 input files"""
@@ -192,30 +179,30 @@ def test_both_without_outputfile(self):
192179 shutil .copy (data_dir / 'file1_fr_crlf.po' , '.' )
193180 shutil .copy (data_dir / 'file2_fr_lf.po' , '.' )
194181 assert_python_ok (msgfmt , 'file1_fr_crlf.po' , 'file2_fr_lf.po' )
195- self .assertTrue (
196- filecmp .cmp (data_dir / 'file1_fr_crlf.mo' , 'file1_fr_crlf.mo' ))
197- self .assertTrue (
198- filecmp .cmp (data_dir / 'file2_fr_lf.mo' , 'file2_fr_lf.mo' ))
182+ self .assertTrue (filecmp .cmp (data_dir / 'file1_fr_crlf.mo' , 'file1_fr_crlf.mo' ))
183+ self .assertTrue (filecmp .cmp (data_dir / 'file2_fr_lf.mo' , 'file2_fr_lf.mo' ))
184+
185+
186+ def make_message_files (mo_file , * po_files ):
187+ compile_messages (mo_file , * po_files )
188+ # Create a human-readable JSON file which is
189+ # easier to review than the binary .mo file.
190+ with open (mo_file , 'rb' ) as f :
191+ translations = GNUTranslations (f )
192+ catalog_file = mo_file .with_suffix ('.json' )
193+ with open (catalog_file , 'w' ) as f :
194+ data = translations ._catalog .items ()
195+ data = sorted (data , key = lambda x : (isinstance (x [0 ], tuple ), x [0 ]))
196+ json .dump (data , f , indent = 4 )
197+ f .write ('\n ' )
199198
200199
201200def update_catalog_snapshots ():
202201 for po_file in data_dir .glob ('*.po' ):
203202 mo_file = po_file .with_suffix ('.mo' )
204- compile_messages (mo_file , po_file )
205- # Create a human-readable JSON file which is
206- # easier to review than the binary .mo file.
207- with open (mo_file , 'rb' ) as f :
208- translations = GNUTranslations (f )
209- catalog_file = po_file .with_suffix ('.json' )
210- with open (catalog_file , 'w' ) as f :
211- data = translations ._catalog .items ()
212- data = sorted (data , key = lambda x : (isinstance (x [0 ], tuple ), x [0 ]))
213- json .dump (data , f , indent = 4 )
214- f .write ('\n ' )
203+ make_message_files (mo_file , po_file )
215204 # special processing for file12_fr.mo which results from 2 input files
216- compile_messages (data_dir / 'file12_fr.mo' ,
217- data_dir / 'file1_fr_crlf.po' ,
218- data_dir / 'file2_fr_lf.po' )
205+ make_message_files (data_dir / 'file12_fr.mo' , data_dir / 'file1_fr_crlf.po' , data_dir / 'file2_fr_lf.po' )
219206
220207
221208if __name__ == '__main__' :
0 commit comments