svghmi/i18n.py
branchsvghmi
changeset 3108 079419e7228d
child 3112 bd20f9112014
equal deleted inserted replaced
3107:ee0704cc6dc8 3108:079419e7228d
       
     1 import time
       
     2 
       
     3 locpfx = '#:svghmi.svg:'
       
     4 
       
     5 pot_header = '''\
       
     6 # SOME DESCRIPTIVE TITLE.
       
     7 # Copyright (C) YEAR ORGANIZATION
       
     8 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
       
     9 #
       
    10 msgid ""
       
    11 msgstr ""
       
    12 "Project-Id-Version: PACKAGE VERSION\\n"
       
    13 "POT-Creation-Date: %(time)s\\n"
       
    14 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
       
    15 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
       
    16 "Language-Team: LANGUAGE <LL@li.org>\\n"
       
    17 "MIME-Version: 1.0\\n"
       
    18 "Content-Type: text/plain; charset=CHARSET\\n"
       
    19 "Content-Transfer-Encoding: ENCODING\\n"
       
    20 "Generated-By: SVGHMI 1.0\\n"
       
    21 
       
    22 '''
       
    23 
       
    24 class POTWriter:
       
    25     def __init__(self):
       
    26         self.__messages = {}
       
    27 
       
    28     def ImportMessages(self, msgs):    
       
    29         for msg in msgs:
       
    30             self.addentry("\n".join([line.text for line in msg]), msg.get("label"), msg.get("id"))
       
    31 
       
    32     def addentry(self, msg, label, svgid):
       
    33         entry = (label, svgid)
       
    34         self.__messages.setdefault(msg, set()).add(entry)
       
    35 
       
    36     def write(self, fp):
       
    37         timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
       
    38         print >> fp, pot_header % {'time': timestamp}
       
    39         reverse = {}
       
    40         for k, v in self.__messages.items():
       
    41             keys = list(v)
       
    42             keys.sort()
       
    43             reverse.setdefault(tuple(keys), []).append((k, v))
       
    44         rkeys = reverse.keys()
       
    45         rkeys.sort()
       
    46         for rkey in rkeys:
       
    47             rentries = reverse[rkey]
       
    48             rentries.sort()
       
    49             for k, v in rentries:
       
    50                 v = v.keys()
       
    51                 v.sort()
       
    52                 locline = locpfx
       
    53                 for label, svgid in v:
       
    54                     d = {'label': label, 'svgid': svgid}
       
    55                     s = _(' %(label)s:%(svgid)d') % d
       
    56                     if len(locline) + len(s) <= 78:
       
    57                         locline = locline + s
       
    58                     else:
       
    59                         print >> fp, locline
       
    60                         locline = locpfx + s
       
    61                 if len(locline) > len(locpfx):
       
    62                     print >> fp, locline
       
    63                 print >> fp, 'msgid', normalize(k)
       
    64                 print >> fp, 'msgstr ""\n'
       
    65 
       
    66 
       
    67 class POReader:
       
    68     def __init__(self):
       
    69         self.__messages = {}
       
    70 
       
    71     def add(msgid, msgstr, fuzzy):
       
    72         "Add a non-fuzzy translation to the dictionary."
       
    73         if not fuzzy and msgstr:
       
    74             self.__messages[msgid] = msgstr
       
    75 
       
    76     def read(self, fp):
       
    77         ID = 1
       
    78         STR = 2
       
    79 
       
    80         lines = fp.readlines()
       
    81         section = None
       
    82         fuzzy = 0
       
    83 
       
    84         # Parse the catalog
       
    85         lno = 0
       
    86         for l in lines:
       
    87             lno += 1
       
    88             # If we get a comment line after a msgstr, this is a new entry
       
    89             if l[0] == '#' and section == STR:
       
    90                 self.add(msgid, msgstr, fuzzy)
       
    91                 section = None
       
    92                 fuzzy = 0
       
    93             # Record a fuzzy mark
       
    94             if l[:2] == '#,' and 'fuzzy' in l:
       
    95                 fuzzy = 1
       
    96             # Skip comments
       
    97             if l[0] == '#':
       
    98                 continue
       
    99             # Now we are in a msgid section, output previous section
       
   100             if l.startswith('msgid') and not l.startswith('msgid_plural'):
       
   101                 if section == STR:
       
   102                     self.add(msgid, msgstr, fuzzy)
       
   103                 section = ID
       
   104                 l = l[5:]
       
   105                 msgid = msgstr = ''
       
   106                 is_plural = False
       
   107             # This is a message with plural forms
       
   108             elif l.startswith('msgid_plural'):
       
   109                 if section != ID:
       
   110                     print >> sys.stderr, 'msgid_plural not preceded by msgid on %s:%d' %\
       
   111                         (infile, lno)
       
   112                     sys.exit(1)
       
   113                 l = l[12:]
       
   114                 msgid += '\0' # separator of singular and plural
       
   115                 is_plural = True
       
   116             # Now we are in a msgstr section
       
   117             elif l.startswith('msgstr'):
       
   118                 section = STR
       
   119                 if l.startswith('msgstr['):
       
   120                     if not is_plural:
       
   121                         print >> sys.stderr, 'plural without msgid_plural on %s:%d' %\
       
   122                             (infile, lno)
       
   123                         sys.exit(1)
       
   124                     l = l.split(']', 1)[1]
       
   125                     if msgstr:
       
   126                         msgstr += '\0' # Separator of the various plural forms
       
   127                 else:
       
   128                     if is_plural:
       
   129                         print >> sys.stderr, 'indexed msgstr required for plural on  %s:%d' %\
       
   130                             (infile, lno)
       
   131                         sys.exit(1)
       
   132                     l = l[6:]
       
   133             # Skip empty lines
       
   134             l = l.strip()
       
   135             if not l:
       
   136                 continue
       
   137             l = ast.literal_eval(l)
       
   138             if section == ID:
       
   139                 msgid += l
       
   140             elif section == STR:
       
   141                 msgstr += l
       
   142             else:
       
   143                 print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
       
   144                       'before:'
       
   145                 print >> sys.stderr, l
       
   146                 sys.exit(1)
       
   147         # Add last entry
       
   148         if section == STR:
       
   149             self.add(msgid, msgstr, fuzzy)
       
   150 
       
   151