svghmi/i18n.py
branchsvghmi
changeset 3113 18133b90196e
parent 3112 bd20f9112014
child 3114 fb1e320836e8
equal deleted inserted replaced
3112:bd20f9112014 3113:18133b90196e
     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 from __future__ import absolute_import
       
    10 import os
    10 import sys
    11 import sys
    11 import subprocess
    12 import subprocess
    12 import time
    13 import time
       
    14 import ast
    13 import wx
    15 import wx
    14 
    16 
    15 def open_pofile(pofile):
    17 def open_pofile(pofile):
    16     """ Opens PO file with POEdit """
    18     """ Opens PO file with POEdit """
    17     
    19     
    33 
    35 
    34     if poedit_path is None:
    36     if poedit_path is None:
    35         wx.MessageBox("POEdit is not found or installed !")
    37         wx.MessageBox("POEdit is not found or installed !")
    36     else:
    38     else:
    37         subprocess.Popen([poedit_path,pofile])
    39         subprocess.Popen([poedit_path,pofile])
       
    40 
       
    41 def EtreeToMessages(msgs):
       
    42     """ Converts XML tree from 'extract_i18n' templates into a list of tuples """
       
    43     messages = []
       
    44 
       
    45     for msg in msgs:
       
    46         messages.append((
       
    47             "\n".join([line.text.encode("utf-8") for line in msg]),
       
    48             msg.get("label"), msg.get("id")))
       
    49 
       
    50     return messages
       
    51 
       
    52 def SaveCatalog(fname, messages):
       
    53     """ Save messages given as list of tupple (msg,label,id) in POT file """
       
    54     w = POTWriter()
       
    55     w.ImportMessages(messages)
       
    56 
       
    57     with open(fname, 'w') as POT_file:
       
    58         w.write(POT_file)
       
    59     
       
    60 def ReadTranslations(dirpath):
       
    61     """ Read all PO files from a directory and return a list of (lang, translation_dict) tuples """
       
    62 
       
    63     po_files = [fname for fname in os.listdir(dirpath) if fname.endswith(".po")]
       
    64 
       
    65     translations = []
       
    66     for po_fname in po_files:
       
    67         r = POReader()
       
    68         with open(os.path.join(dirpath, po_fname), 'r') as PO_file:
       
    69             r.read(PO_file)
       
    70             translations.append((po_fname[:-3], r.get_messages()))
       
    71     return translations
       
    72 
       
    73 def MatchTranslations(translations, messages, errcallback):
       
    74     """ 
       
    75     Matches translations against original message catalog, 
       
    76     warn about inconsistancies, 
       
    77     returns list of langs, and a list of (msgid, [translations]) tuples 
       
    78     """
       
    79     translated_messages = []
       
    80     for msgid,label,svgid in messages:
       
    81         translated_message = []
       
    82         for lang,translation in translations:
       
    83             msg = translation.pop(msgid, None)
       
    84             if msg is None:
       
    85                 errcallback(_('{}: Missing translation for "{}" (label:{}, id:{})').format(lang,msgid,label,svgid))
       
    86             translated_message.append(msg)
       
    87         translated_messages.append((msgid,translated_message))
       
    88     langs = []
       
    89     for lang,translation in translations:
       
    90         langs.append(lang)
       
    91         for msgid, msg in translation.iteritems():
       
    92             errcallback(_('{}: Unused translation "{}":"{}"').format(lang,msgid,msg))
       
    93 
       
    94     return langs,translated_messages
       
    95 
       
    96         
       
    97 def TranslationToEtree(langs,translated_messages):
       
    98     pass
       
    99     
    38 
   100 
    39 locpfx = '#:svghmi.svg:'
   101 locpfx = '#:svghmi.svg:'
    40 
   102 
    41 pot_header = '''\
   103 pot_header = '''\
    42 # SOME DESCRIPTIVE TITLE.
   104 # SOME DESCRIPTIVE TITLE.
    49 "POT-Creation-Date: %(time)s\\n"
   111 "POT-Creation-Date: %(time)s\\n"
    50 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
   112 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
    51 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
   113 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
    52 "Language-Team: LANGUAGE <LL@li.org>\\n"
   114 "Language-Team: LANGUAGE <LL@li.org>\\n"
    53 "MIME-Version: 1.0\\n"
   115 "MIME-Version: 1.0\\n"
    54 "Content-Type: text/plain; charset=UTF-8\n"
   116 "Content-Type: text/plain; charset=UTF-8\\n"
    55 "Content-Transfer-Encoding: 8bit\n"
   117 "Content-Transfer-Encoding: 8bit\\n"
    56 "Generated-By: SVGHMI 1.0\\n"
   118 "Generated-By: SVGHMI 1.0\\n"
    57 
   119 
    58 '''
   120 '''
    59 escapes = []
   121 escapes = []
    60 
   122 
   109     def __init__(self):
   171     def __init__(self):
   110         self.__messages = {}
   172         self.__messages = {}
   111 
   173 
   112     def ImportMessages(self, msgs):
   174     def ImportMessages(self, msgs):
   113         for msg in msgs:
   175         for msg in msgs:
   114             self.addentry("\n".join([line.text.encode("utf-8") for line in msg]), msg.get("label"), msg.get("id"))
   176             self.addentry(*msg)
   115 
   177 
   116     def addentry(self, msg, label, svgid):
   178     def addentry(self, msg, label, svgid):
   117         entry = (label, svgid)
   179         entry = (label, svgid)
   118         print(entry)
       
   119         self.__messages.setdefault(msg, set()).add(entry)
   180         self.__messages.setdefault(msg, set()).add(entry)
   120 
   181 
   121     def write(self, fp):
   182     def write(self, fp):
   122         timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
   183         timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
   123         print >> fp, pot_header % {'time': timestamp}
   184         print >> fp, pot_header % {'time': timestamp}
   151 
   212 
   152 class POReader:
   213 class POReader:
   153     def __init__(self):
   214     def __init__(self):
   154         self.__messages = {}
   215         self.__messages = {}
   155 
   216 
   156     def add(msgid, msgstr, fuzzy):
   217     def get_messages(self):
       
   218         return self.__messages
       
   219 
       
   220     def add(self, msgid, msgstr, fuzzy):
   157         "Add a non-fuzzy translation to the dictionary."
   221         "Add a non-fuzzy translation to the dictionary."
   158         if not fuzzy and msgstr:
   222         if not fuzzy and msgstr:
   159             self.__messages[msgid] = msgstr
   223             self.__messages[msgid] = msgstr
   160 
   224 
   161     def read(self, fp):
   225     def read(self, fp):