svghmi/i18n.py
branchsvghmi
changeset 3112 bd20f9112014
parent 3108 079419e7228d
child 3113 18133b90196e
equal deleted inserted replaced
3111:5d9ae04ee50f 3112:bd20f9112014
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 # This file is part of Beremiz
       
     5 # Copyright (C) 2021: Edouard TISSERANT
       
     6 #
       
     7 # See COPYING file for copyrights details.
       
     8 
       
     9 from __future__ import absolute_import
       
    10 import sys
       
    11 import subprocess
     1 import time
    12 import time
       
    13 import wx
       
    14 
       
    15 def open_pofile(pofile):
       
    16     """ Opens PO file with POEdit """
       
    17     
       
    18     if sys.platform.startswith('win'):
       
    19         from six.moves import winreg
       
    20         poedit_cmd = None
       
    21         try:
       
    22             poedit_cmd = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE,
       
    23                                            'SOFTWARE\\Classes\\poedit\\shell\\open\\command')
       
    24             poedit_path = poedit_cmd.replace('"%1"', '').strip().replace('"', '')
       
    25         except OSError:
       
    26             poedit_path = None
       
    27 
       
    28     else:
       
    29         try:
       
    30             poedit_path = subprocess.check_output("command -v poedit", shell=True).strip()
       
    31         except subprocess.CalledProcessError:
       
    32             poedit_path = None
       
    33 
       
    34     if poedit_path is None:
       
    35         wx.MessageBox("POEdit is not found or installed !")
       
    36     else:
       
    37         subprocess.Popen([poedit_path,pofile])
     2 
    38 
     3 locpfx = '#:svghmi.svg:'
    39 locpfx = '#:svghmi.svg:'
     4 
    40 
     5 pot_header = '''\
    41 pot_header = '''\
     6 # SOME DESCRIPTIVE TITLE.
    42 # SOME DESCRIPTIVE TITLE.
    13 "POT-Creation-Date: %(time)s\\n"
    49 "POT-Creation-Date: %(time)s\\n"
    14 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
    50 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
    15 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
    51 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
    16 "Language-Team: LANGUAGE <LL@li.org>\\n"
    52 "Language-Team: LANGUAGE <LL@li.org>\\n"
    17 "MIME-Version: 1.0\\n"
    53 "MIME-Version: 1.0\\n"
    18 "Content-Type: text/plain; charset=CHARSET\\n"
    54 "Content-Type: text/plain; charset=UTF-8\n"
    19 "Content-Transfer-Encoding: ENCODING\\n"
    55 "Content-Transfer-Encoding: 8bit\n"
    20 "Generated-By: SVGHMI 1.0\\n"
    56 "Generated-By: SVGHMI 1.0\\n"
    21 
    57 
    22 '''
    58 '''
       
    59 escapes = []
       
    60 
       
    61 def make_escapes(pass_iso8859):
       
    62     global escapes
       
    63     escapes = [chr(i) for i in range(256)]
       
    64     if pass_iso8859:
       
    65         # Allow iso-8859 characters to pass through so that e.g. 'msgid
       
    66         # "Höhe"' would result not result in 'msgid "H\366he"'.  Otherwise we
       
    67         # escape any character outside the 32..126 range.
       
    68         mod = 128
       
    69     else:
       
    70         mod = 256
       
    71     for i in range(mod):
       
    72         if not(32 <= i <= 126):
       
    73             escapes[i] = "\\%03o" % i
       
    74     escapes[ord('\\')] = '\\\\'
       
    75     escapes[ord('\t')] = '\\t'
       
    76     escapes[ord('\r')] = '\\r'
       
    77     escapes[ord('\n')] = '\\n'
       
    78     escapes[ord('\"')] = '\\"'
       
    79 
       
    80 make_escapes(pass_iso8859 = True)
       
    81 
       
    82 EMPTYSTRING = ''
       
    83 
       
    84 def escape(s):
       
    85     global escapes
       
    86     s = list(s)
       
    87     for i in range(len(s)):
       
    88         s[i] = escapes[ord(s[i])]
       
    89     return EMPTYSTRING.join(s)
       
    90 
       
    91 def normalize(s):
       
    92     # This converts the various Python string types into a format that is
       
    93     # appropriate for .po files, namely much closer to C style.
       
    94     lines = s.split('\n')
       
    95     if len(lines) == 1:
       
    96         s = '"' + escape(s) + '"'
       
    97     else:
       
    98         if not lines[-1]:
       
    99             del lines[-1]
       
   100             lines[-1] = lines[-1] + '\n'
       
   101         for i in range(len(lines)):
       
   102             lines[i] = escape(lines[i])
       
   103         lineterm = '\\n"\n"'
       
   104         s = '""\n"' + lineterm.join(lines) + '"'
       
   105     return s
       
   106 
    23 
   107 
    24 class POTWriter:
   108 class POTWriter:
    25     def __init__(self):
   109     def __init__(self):
    26         self.__messages = {}
   110         self.__messages = {}
    27 
   111 
    28     def ImportMessages(self, msgs):    
   112     def ImportMessages(self, msgs):
    29         for msg in msgs:
   113         for msg in msgs:
    30             self.addentry("\n".join([line.text for line in msg]), msg.get("label"), msg.get("id"))
   114             self.addentry("\n".join([line.text.encode("utf-8") for line in msg]), msg.get("label"), msg.get("id"))
    31 
   115 
    32     def addentry(self, msg, label, svgid):
   116     def addentry(self, msg, label, svgid):
    33         entry = (label, svgid)
   117         entry = (label, svgid)
       
   118         print(entry)
    34         self.__messages.setdefault(msg, set()).add(entry)
   119         self.__messages.setdefault(msg, set()).add(entry)
    35 
   120 
    36     def write(self, fp):
   121     def write(self, fp):
    37         timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
   122         timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
    38         print >> fp, pot_header % {'time': timestamp}
   123         print >> fp, pot_header % {'time': timestamp}
    45         rkeys.sort()
   130         rkeys.sort()
    46         for rkey in rkeys:
   131         for rkey in rkeys:
    47             rentries = reverse[rkey]
   132             rentries = reverse[rkey]
    48             rentries.sort()
   133             rentries.sort()
    49             for k, v in rentries:
   134             for k, v in rentries:
    50                 v = v.keys()
   135                 v = list(v)
    51                 v.sort()
   136                 v.sort()
    52                 locline = locpfx
   137                 locline = locpfx
    53                 for label, svgid in v:
   138                 for label, svgid in v:
    54                     d = {'label': label, 'svgid': svgid}
   139                     d = {'label': label, 'svgid': svgid}
    55                     s = _(' %(label)s:%(svgid)d') % d
   140                     s = _(' %(label)s:%(svgid)s') % d
    56                     if len(locline) + len(s) <= 78:
   141                     if len(locline) + len(s) <= 78:
    57                         locline = locline + s
   142                         locline = locline + s
    58                     else:
   143                     else:
    59                         print >> fp, locline
   144                         print >> fp, locline
    60                         locline = locpfx + s
   145                         locline = locpfx + s