devtools/test/unittest_i18n.py
branchstable
changeset 9200 1ba5961b19dd
parent 9153 bc1b8e77d6ce
child 9202 83f73a9746f6
equal deleted inserted replaced
9199:f66e2d331f4b 9200:1ba5961b19dd
    26 from cubicweb.cwconfig import CubicWebNoAppConfiguration
    26 from cubicweb.cwconfig import CubicWebNoAppConfiguration
    27 
    27 
    28 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data')
    28 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data')
    29 
    29 
    30 def load_po(fname):
    30 def load_po(fname):
    31     msgs = []
    31     """load a po file and  return a set of encountered (msgid, msgctx)"""
    32     msgid = None
    32     msgs = set()
    33     msgctxt = None
    33     msgid = msgctxt = None
    34     for line in open(fname):
    34     for line in open(fname):
    35         if line.strip() in ('', '#'):
    35         if line.strip() in ('', '#'):
    36             continue
    36             continue
    37         if line.startswith('msgstr'):
    37         if line.startswith('msgstr'):
    38             msgs.append((msgid, msgctxt))
    38             assert not (msgid, msgctxt) in msgs
    39             msgid = None
    39             msgs.add( (msgid, msgctxt) )
    40             msgctxt = None
    40             msgid = msgctxt = None
    41         elif line.startswith('msgid'):
    41         elif line.startswith('msgid'):
    42             msgid = line.split(' ', 1)[1][1:-1]
    42             msgid = line.split(' ', 1)[1][1:-1]
    43         elif line.startswith('msgctx'):
    43         elif line.startswith('msgctx'):
    44             msgctxt = line.split(' ', 1)[1][1: -1]
    44             msgctxt = line.split(' ', 1)[1][1: -1]
       
    45         elif msgid is not None:
       
    46             msgid += line[1:-1]
       
    47         elif msgctxt is not None:
       
    48             msgctxt += line[1:-1]
       
    49     return msgs
    45 
    50 
    46         else:
       
    47             if msgctxt is not None:
       
    48                 msgctxt += line[1:-1]
       
    49             elif msgid is not None:
       
    50                 msgid += line[1:-1]
       
    51     return set(msgs)
       
    52 
    51 
    53 class cubePotGeneratorTC(TestCase):
    52 class cubePotGeneratorTC(TestCase):
    54     """test case for i18n pot file generator"""
    53     """test case for i18n pot file generator"""
       
    54 
    55     def setUp(self):
    55     def setUp(self):
    56         self._CUBES_PATH = CubicWebNoAppConfiguration.CUBES_PATH[:]
    56         self._CUBES_PATH = CubicWebNoAppConfiguration.CUBES_PATH[:]
    57         CubicWebNoAppConfiguration.CUBES_PATH.append(osp.join(DATADIR, 'cubes'))
    57         CubicWebNoAppConfiguration.CUBES_PATH.append(osp.join(DATADIR, 'cubes'))
    58         CubicWebNoAppConfiguration.cls_adjust_sys_path()
    58         CubicWebNoAppConfiguration.cls_adjust_sys_path()
    59 
    59