svghmi/i18n.py
branchsvghmi
changeset 3115 77cfbf1aacf0
parent 3114 fb1e320836e8
child 3116 6da94ec04325
equal deleted inserted replaced
3114:fb1e320836e8 3115:77cfbf1aacf0
     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 from lxml import etree
    10 import os
    11 import os
    11 import sys
    12 import sys
    12 import subprocess
    13 import subprocess
    13 import time
    14 import time
    14 import ast
    15 import ast
    42     """ Converts XML tree from 'extract_i18n' templates into a list of tuples """
    43     """ Converts XML tree from 'extract_i18n' templates into a list of tuples """
    43     messages = []
    44     messages = []
    44 
    45 
    45     for msg in msgs:
    46     for msg in msgs:
    46         messages.append((
    47         messages.append((
    47             "\n".join([line.text.encode("utf-8") for line in msg]),
    48             "\n".join([line.text for line in msg]),
    48             msg.get("label"), msg.get("id")))
    49             msg.get("label"), msg.get("id")))
    49 
    50 
    50     return messages
    51     return messages
    51 
    52 
    52 def SaveCatalog(fname, messages):
    53 def SaveCatalog(fname, messages):
    83         for lang,translation in translations:
    84         for lang,translation in translations:
    84             msg = translation.pop(msgid, None)
    85             msg = translation.pop(msgid, None)
    85             if msg is None:
    86             if msg is None:
    86                 broken_lang.add(lang)
    87                 broken_lang.add(lang)
    87                 errcallback(_('{}: Missing translation for "{}" (label:{}, id:{})\n').format(lang,msgid,label,svgid))
    88                 errcallback(_('{}: Missing translation for "{}" (label:{}, id:{})\n').format(lang,msgid,label,svgid))
    88             translated_message.append(msg)
    89                 translated_message.append(msgid)
       
    90             else:
       
    91                 translated_message.append(msg)
    89         translated_messages.append((msgid,translated_message))
    92         translated_messages.append((msgid,translated_message))
    90     langs = []
    93     langs = []
    91     for lang,translation in translations:
    94     for lang,translation in translations:
    92         langs.append(lang)
    95         langs.append(lang)
    93         broken = False
    96         broken = False
    94         for msgid, msg in translation.iteritems():
    97         for msgid, msg in translation.iteritems():
    95             if len(msgid):
    98             broken = True
    96                 broken = True
    99             errcallback(_('{}: Unused translation "{}":"{}"\n').format(lang,msgid,msg))
    97                 errcallback(_('{}: Unused translation "{}":"{}"\n').format(lang,msgid,msg))
       
    98         if broken or lang in broken_lang:
   100         if broken or lang in broken_lang:
    99             errcallback(_('Translation for {} is outdated, please edit {}.po, click "Catalog -> Update from POT File..." and select messages.pot.\n').format(lang,lang))
   101             errcallback(_('Translation for {} is outdated, please edit {}.po, click "Catalog -> Update from POT File..." and select messages.pot.\n').format(lang,lang))
   100 
   102 
   101 
   103 
   102     return langs,translated_messages
   104     return langs,translated_messages
   103 
   105 
   104 
   106 
   105 def TranslationToEtree(langs,translated_messages):
   107 def TranslationToEtree(langs,translated_messages):
   106     pass
   108 
       
   109     langsroot = etree.Element("langs")
       
   110     for lang in langs:
       
   111         langel = etree.SubElement(langsroot, "lang")
       
   112         langel.text = lang
       
   113 
       
   114     msgsroot = etree.Element("translations")
       
   115     for msgid, msgs in translated_messages:
       
   116         msgidel = etree.SubElement(msgsroot, "msgid")
       
   117         msgidel.text = msgid 
       
   118         for msg in msgs:
       
   119             msgel = etree.SubElement(msgidel, "msg")
       
   120             msgel.text = msg 
       
   121    
       
   122     return [langsroot,msgsroot]
       
   123 
   107 
   124 
   108 
   125 
   109 locpfx = '#:svghmi.svg:'
   126 locpfx = '#:svghmi.svg:'
   110 
   127 
   111 pot_header = '''\
   128 pot_header = '''\
   178 class POTWriter:
   195 class POTWriter:
   179     def __init__(self):
   196     def __init__(self):
   180         self.__messages = {}
   197         self.__messages = {}
   181 
   198 
   182     def ImportMessages(self, msgs):
   199     def ImportMessages(self, msgs):
   183         for msg in msgs:
   200         for  msg, label, svgid in msgs:
   184             self.addentry(*msg)
   201             self.addentry(msg.encode("utf-8"), label, svgid)
   185 
   202 
   186     def addentry(self, msg, label, svgid):
   203     def addentry(self, msg, label, svgid):
   187         entry = (label, svgid)
   204         entry = (label, svgid)
   188         self.__messages.setdefault(msg, set()).add(entry)
   205         self.__messages.setdefault(msg, set()).add(entry)
   189 
   206 
   225     def get_messages(self):
   242     def get_messages(self):
   226         return self.__messages
   243         return self.__messages
   227 
   244 
   228     def add(self, msgid, msgstr, fuzzy):
   245     def add(self, msgid, msgstr, fuzzy):
   229         "Add a non-fuzzy translation to the dictionary."
   246         "Add a non-fuzzy translation to the dictionary."
   230         if not fuzzy and msgstr:
   247         if not fuzzy and msgstr and msgid:
   231             self.__messages[msgid] = msgstr
   248             self.__messages[msgid.decode('utf-8')] = msgstr.decode('utf-8')
   232 
   249 
   233     def read(self, fp):
   250     def read(self, fp):
   234         ID = 1
   251         ID = 1
   235         STR = 2
   252         STR = 2
   236 
   253