svghmi/i18n.py
branchpython3
changeset 3750 f62625418bff
parent 3573 1ee56fb544fc
child 3915 b5017dd5c049
equal deleted inserted replaced
3749:fda6c1a37662 3750:f62625418bff
     4 # This file is part of Beremiz
     4 # This file is part of Beremiz
     5 # Copyright (C) 2021: Edouard TISSERANT
     5 # Copyright (C) 2021: Edouard TISSERANT
     6 #
     6 #
     7 # See COPYING file for copyrights details.
     7 # See COPYING file for copyrights details.
     8 
     8 
     9 from __future__ import absolute_import
     9 
    10 from lxml import etree
    10 from lxml import etree
    11 import os
    11 import os
    12 import sys
    12 import sys
    13 import subprocess
    13 import subprocess
    14 import time
    14 import time
    38             poedit_path = dblquote_value+smpl_value
    38             poedit_path = dblquote_value+smpl_value
    39         except OSError:
    39         except OSError:
    40             poedit_path = None
    40             poedit_path = None
    41 
    41 
    42     else:
    42     else:
    43         if os.environ.has_key("SNAP"):
    43         if "SNAP" in os.environ:
    44             MessageBoxOnce("Launching POEdit with xdg-open",
    44             MessageBoxOnce("Launching POEdit with xdg-open",
    45                     "Confined app can't launch POEdit directly.\n"+
    45                     "Confined app can't launch POEdit directly.\n"+
    46                         "Instead, PO/POT file is passed to xdg-open.\n"+
    46                         "Instead, PO/POT file is passed to xdg-open.\n"+
    47                         "Please select POEdit when proposed.\n\n"+
    47                         "Please select POEdit when proposed.\n\n"+
    48                     "Notes: \n"+
    48                     "Notes: \n"+
   129                 langname = langcode
   129                 langname = langcode
   130 
   130 
   131         langs.append((langname,langcode))
   131         langs.append((langname,langcode))
   132 
   132 
   133         broken = False
   133         broken = False
   134         for msgid, msg in translation.iteritems():
   134         for msgid, msg in translation.items():
   135             broken = True
   135             broken = True
   136             errcallback(_('{}: Unused translation "{}":"{}"\n').format(langcode,msgid,msg))
   136             errcallback(_('{}: Unused translation "{}":"{}"\n').format(langcode,msgid,msg))
   137         if broken or langcode in broken_lang:
   137         if broken or langcode in broken_lang:
   138             errcallback(_('Translation for {} is outdated, please edit {}.po, click "Catalog -> Update from POT File..." and select messages.pot.\n').format(langcode,langcode))
   138             errcallback(_('Translation for {} is outdated, please edit {}.po, click "Catalog -> Update from POT File..." and select messages.pot.\n').format(langcode,langcode))
   139 
   139 
   244         entry = (label, svgid)
   244         entry = (label, svgid)
   245         self.__messages.setdefault(msg, set()).add(entry)
   245         self.__messages.setdefault(msg, set()).add(entry)
   246 
   246 
   247     def write(self, fp):
   247     def write(self, fp):
   248         timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
   248         timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
   249         print >> fp, pot_header % {'time': timestamp}
   249         print(pot_header % {'time': timestamp}, file=fp)
   250         reverse = {}
   250         reverse = {}
   251         for k, v in self.__messages.items():
   251         for k, v in list(self.__messages.items()):
   252             keys = list(v)
   252             keys = list(v)
   253             keys.sort()
   253             keys.sort()
   254             reverse.setdefault(tuple(keys), []).append((k, v))
   254             reverse.setdefault(tuple(keys), []).append((k, v))
   255         rkeys = reverse.keys()
   255         rkeys = list(reverse.keys())
   256         rkeys.sort()
   256         rkeys.sort()
   257         for rkey in rkeys:
   257         for rkey in rkeys:
   258             rentries = reverse[rkey]
   258             rentries = reverse[rkey]
   259             rentries.sort()
   259             rentries.sort()
   260             for k, v in rentries:
   260             for k, v in rentries:
   265                     d = {'label': label, 'svgid': svgid}
   265                     d = {'label': label, 'svgid': svgid}
   266                     s = _(' %(label)s:%(svgid)s') % d
   266                     s = _(' %(label)s:%(svgid)s') % d
   267                     if len(locline) + len(s) <= 78:
   267                     if len(locline) + len(s) <= 78:
   268                         locline = locline + s
   268                         locline = locline + s
   269                     else:
   269                     else:
   270                         print >> fp, locline
   270                         print(locline, file=fp)
   271                         locline = locpfx + s
   271                         locline = locpfx + s
   272                 if len(locline) > len(locpfx):
   272                 if len(locline) > len(locpfx):
   273                     print >> fp, locline
   273                     print(locline, file=fp)
   274                 print >> fp, 'msgid', normalize(k)
   274                 print('msgid', normalize(k), file=fp)
   275                 print >> fp, 'msgstr ""\n'
   275                 print('msgstr ""\n', file=fp)
   276 
   276 
   277 
   277 
   278 class POReader:
   278 class POReader:
   279     def __init__(self):
   279     def __init__(self):
   280         self.__messages = {}
   280         self.__messages = {}
   319                 msgid = msgstr = ''
   319                 msgid = msgstr = ''
   320                 is_plural = False
   320                 is_plural = False
   321             # This is a message with plural forms
   321             # This is a message with plural forms
   322             elif l.startswith('msgid_plural'):
   322             elif l.startswith('msgid_plural'):
   323                 if section != ID:
   323                 if section != ID:
   324                     print >> sys.stderr, 'msgid_plural not preceded by msgid on %s:%d' %\
   324                     print('msgid_plural not preceded by msgid on %s:%d' %\
   325                         (infile, lno)
   325                         (infile, lno), file=sys.stderr)
   326                     sys.exit(1)
   326                     sys.exit(1)
   327                 l = l[12:]
   327                 l = l[12:]
   328                 msgid += '\0' # separator of singular and plural
   328                 msgid += '\0' # separator of singular and plural
   329                 is_plural = True
   329                 is_plural = True
   330             # Now we are in a msgstr section
   330             # Now we are in a msgstr section
   331             elif l.startswith('msgstr'):
   331             elif l.startswith('msgstr'):
   332                 section = STR
   332                 section = STR
   333                 if l.startswith('msgstr['):
   333                 if l.startswith('msgstr['):
   334                     if not is_plural:
   334                     if not is_plural:
   335                         print >> sys.stderr, 'plural without msgid_plural on %s:%d' %\
   335                         print('plural without msgid_plural on %s:%d' %\
   336                             (infile, lno)
   336                             (infile, lno), file=sys.stderr)
   337                         sys.exit(1)
   337                         sys.exit(1)
   338                     l = l.split(']', 1)[1]
   338                     l = l.split(']', 1)[1]
   339                     if msgstr:
   339                     if msgstr:
   340                         msgstr += '\0' # Separator of the various plural forms
   340                         msgstr += '\0' # Separator of the various plural forms
   341                 else:
   341                 else:
   342                     if is_plural:
   342                     if is_plural:
   343                         print >> sys.stderr, 'indexed msgstr required for plural on  %s:%d' %\
   343                         print('indexed msgstr required for plural on  %s:%d' %\
   344                             (infile, lno)
   344                             (infile, lno), file=sys.stderr)
   345                         sys.exit(1)
   345                         sys.exit(1)
   346                     l = l[6:]
   346                     l = l[6:]
   347             # Skip empty lines
   347             # Skip empty lines
   348             l = l.strip()
   348             l = l.strip()
   349             if not l:
   349             if not l:
   352             if section == ID:
   352             if section == ID:
   353                 msgid += l
   353                 msgid += l
   354             elif section == STR:
   354             elif section == STR:
   355                 msgstr += l
   355                 msgstr += l
   356             else:
   356             else:
   357                 print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
   357                 print('Syntax error on %s:%d' % (infile, lno), \
   358                       'before:'
   358                       'before:', file=sys.stderr)
   359                 print >> sys.stderr, l
   359                 print(l, file=sys.stderr)
   360                 sys.exit(1)
   360                 sys.exit(1)
   361         # Add last entry
   361         # Add last entry
   362         if section == STR:
   362         if section == STR:
   363             self.add(msgid, msgstr, fuzzy)
   363             self.add(msgid, msgstr, fuzzy)
   364 
   364