svghmi/i18n.py
author Edouard Tisserant <edouard@beremiz.fr>
Wed, 20 Mar 2024 12:15:23 +0100
changeset 3918 9f0ef23569cb
parent 3915 b5017dd5c049
child 3919 0ce4b50d1182
permissions -rw-r--r--
SVGHMI: i18n: fix .POT generation + other cosmetic fixes.
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     1
#!/usr/bin/env python
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     2
# -*- coding: utf-8 -*-
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     3
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     4
# This file is part of Beremiz
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     5
# Copyright (C) 2021: Edouard TISSERANT
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     6
#
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     7
# See COPYING file for copyrights details.
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
     8
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3573
diff changeset
     9
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
    10
from lxml import etree
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    11
import os
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    12
import sys
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    13
import subprocess
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
import time
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    15
import ast
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    16
import wx
3157
279e08f238ca SVGHMI: Fix launch of POEdit on widows. Parsing oommand in registry based on heuristic was bad, now using regex should be safer. TODO : replicate for Inkscape launch.
Edouard Tisserant
parents: 3144
diff changeset
    17
import re
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    18
from email.parser import HeaderParser
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    19
3133
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
    20
import pycountry
3573
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    21
from dialogs import MessageBoxOnce
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    22
from POULibrary import UserAddressedException
3133
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
    23
3157
279e08f238ca SVGHMI: Fix launch of POEdit on widows. Parsing oommand in registry based on heuristic was bad, now using regex should be safer. TODO : replicate for Inkscape launch.
Edouard Tisserant
parents: 3144
diff changeset
    24
cmd_parser = re.compile(r'(?:"([^"]+)"\s*|([^\s]+)\s*)?')
279e08f238ca SVGHMI: Fix launch of POEdit on widows. Parsing oommand in registry based on heuristic was bad, now using regex should be safer. TODO : replicate for Inkscape launch.
Edouard Tisserant
parents: 3144
diff changeset
    25
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    26
def open_pofile(pofile):
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    27
    """ Opens PO file with POEdit """
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    28
    
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    29
    if sys.platform.startswith('win'):
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    30
        from six.moves import winreg
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    31
        poedit_cmd = None
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    32
        try:
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    33
            poedit_cmd = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE,
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    34
                                           'SOFTWARE\\Classes\\poedit\\shell\\open\\command')
3157
279e08f238ca SVGHMI: Fix launch of POEdit on widows. Parsing oommand in registry based on heuristic was bad, now using regex should be safer. TODO : replicate for Inkscape launch.
Edouard Tisserant
parents: 3144
diff changeset
    35
            cmd = re.findall(cmd_parser, poedit_cmd)
279e08f238ca SVGHMI: Fix launch of POEdit on widows. Parsing oommand in registry based on heuristic was bad, now using regex should be safer. TODO : replicate for Inkscape launch.
Edouard Tisserant
parents: 3144
diff changeset
    36
            dblquote_value,smpl_value = cmd[0]
279e08f238ca SVGHMI: Fix launch of POEdit on widows. Parsing oommand in registry based on heuristic was bad, now using regex should be safer. TODO : replicate for Inkscape launch.
Edouard Tisserant
parents: 3144
diff changeset
    37
            poedit_path = dblquote_value+smpl_value
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    38
        except OSError:
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    39
            poedit_path = None
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    40
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    41
    else:
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3573
diff changeset
    42
        if "SNAP" in os.environ:
3573
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    43
            MessageBoxOnce("Launching POEdit with xdg-open",
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    44
                    "Confined app can't launch POEdit directly.\n"+
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    45
                        "Instead, PO/POT file is passed to xdg-open.\n"+
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    46
                        "Please select POEdit when proposed.\n\n"+
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    47
                    "Notes: \n"+
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    48
                    " - POEdit must be installed on you system.\n"+
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    49
                    " - If no choice is proposed, use file manager to change POT/PO file properties.\n",
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    50
                    "SVGHMII18SnapWarning")
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    51
            poedit_path = "xdg-open"
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    52
        else:
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    53
            try:
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    54
                poedit_path = subprocess.check_output("command -v poedit", shell=True).strip()
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    55
            except subprocess.CalledProcessError:
1ee56fb544fc IDE, SVGHMI: Workaround Snap package not launching Inskape, POEdit or Chromium directly, producing lot of output in Incskape CLI, and messing with TMPDIR
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3218
diff changeset
    56
                poedit_path = None
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    57
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    58
    if poedit_path is None:
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    59
        wx.MessageBox("POEdit is not found or installed !")
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    60
    else:
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
    61
        subprocess.Popen([poedit_path,pofile])
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    63
def EtreeToMessages(msgs):
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    64
    """ Converts XML tree from 'extract_i18n' templates into a list of tuples """
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    65
    messages = []
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    66
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    67
    for msg in msgs:
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    68
        messages.append((
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    69
            b"\n".join([line.text.encode() for line in msg]),
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    70
            msg.get("label").encode(), msg.get("id").encode()))
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    71
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    72
    return messages
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    73
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    74
def SaveCatalog(fname, messages):
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    75
    """ Save messages given as list of tupple (msg,label,id) in POT file """
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    76
    w = POTWriter()
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    77
    w.ImportMessages(messages)
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    78
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    79
    with open(fname, 'wb') as POT_file:
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    80
        w.write(POT_file)
3114
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
    81
3214
bb314cdfc656 SVGHMI: force svghmi build if any change in fonts or translation (.PO) files
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3198
diff changeset
    82
def GetPoFiles(dirpath):
bb314cdfc656 SVGHMI: force svghmi build if any change in fonts or translation (.PO) files
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3198
diff changeset
    83
    po_files = [fname for fname in os.listdir(dirpath) if fname.endswith(".po")]
bb314cdfc656 SVGHMI: force svghmi build if any change in fonts or translation (.PO) files
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3198
diff changeset
    84
    po_files.sort()
3218
76f1cd1291f4 SVGHMI: Fixed last i18n changes meant to allow checking changes in PO files. Was making build exception when no PO files available.
Edouard Tisserant
parents: 3214
diff changeset
    85
    return [(po_fname[:-3],os.path.join(dirpath, po_fname)) for po_fname in po_files]
3214
bb314cdfc656 SVGHMI: force svghmi build if any change in fonts or translation (.PO) files
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3198
diff changeset
    86
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    87
def ReadTranslations(dirpath):
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
    88
    """ Read all PO files from a directory and return a list of (langcode, translation_dict) tuples """
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    89
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    90
    translations = []
3218
76f1cd1291f4 SVGHMI: Fixed last i18n changes meant to allow checking changes in PO files. Was making build exception when no PO files available.
Edouard Tisserant
parents: 3214
diff changeset
    91
    for translation_name, po_path in GetPoFiles(dirpath):
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    92
        r = POReader()
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    93
        r.read(po_path)
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
    94
        translations.append((translation_name, r.get_messages()))
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    95
    return translations
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    96
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
    97
def MatchTranslations(translations, messages, errcallback):
3114
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
    98
    """
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
    99
    Matches translations against original message catalog,
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
   100
    warn about inconsistancies,
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
   101
    returns list of langs, and a list of (msgid, [translations]) tuples
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   102
    """
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   103
    translated_messages = []
3114
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
   104
    broken_lang = set()
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   105
    for msgid,label,svgid in messages:
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   106
        translated_message = []
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   107
        for langcode,translation in translations:
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   108
            msg = translation.pop(msgid, None)
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   109
            if msg is None:
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   110
                broken_lang.add(langcode)
3198
d9c8f8789fc8 SVGHMI: Fix typo in error message formating in i18n.
Edouard Tisserant
parents: 3157
diff changeset
   111
                errcallback(_('{}: Missing translation for "{}" (label:{}, id:{})\n').format(langcode,msgid,label,svgid))
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   112
                translated_message.append(msgid)
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   113
            else:
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   114
                translated_message.append(msg)
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   115
        translated_messages.append((msgid,translated_message))
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   116
    langs = []
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   117
    for langcode,translation in translations:
3133
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
   118
        try:
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   119
            l,c = langcode.split("_")
3133
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
   120
            language_name = pycountry.languages.get(alpha_2 = l).name
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
   121
            country_name = pycountry.countries.get(alpha_2 = c).name
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   122
            langname = "{} ({})".format(language_name, country_name)
3133
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
   123
        except:
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
   124
            try:
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   125
                langname = pycountry.languages.get(alpha_2 = langcode).name
3133
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
   126
            except:
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   127
                langname = langcode
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   128
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   129
        langs.append((langname,langcode))
3133
450cd01324ad SVGHMI: HMI:DropDown:#langs@lang now automatically shows human readable list of languages. tests/svghmi_i18n updated accordingly.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3116
diff changeset
   130
3114
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
   131
        broken = False
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3573
diff changeset
   132
        for msgid, msg in translation.items():
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   133
            broken = True
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   134
            errcallback(_('{}: Unused translation "{}":"{}"\n').format(langcode,msgid,msg))
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   135
        if broken or langcode in broken_lang:
3198
d9c8f8789fc8 SVGHMI: Fix typo in error message formating in i18n.
Edouard Tisserant
parents: 3157
diff changeset
   136
            errcallback(_('Translation for {} is outdated, please edit {}.po, click "Catalog -> Update from POT File..." and select messages.pot.\n').format(langcode,langcode))
3114
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
   137
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   138
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   139
    return langs,translated_messages
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   140
3114
fb1e320836e8 SVGHMI: i18n: better warning messages, more explicit.
Edouard Tisserant
parents: 3113
diff changeset
   141
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   142
def TranslationToEtree(langs,translated_messages):
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   143
3116
6da94ec04325 SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
Edouard Tisserant
parents: 3115
diff changeset
   144
    result = etree.Element("translations")
6da94ec04325 SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
Edouard Tisserant
parents: 3115
diff changeset
   145
6da94ec04325 SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
Edouard Tisserant
parents: 3115
diff changeset
   146
    langsroot = etree.SubElement(result, "langs")
3144
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   147
    for name, code in langs:
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   148
        langel = etree.SubElement(langsroot, "lang", {"code":code})
2af6afaccaf2 SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3133
diff changeset
   149
        langel.text = name
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   150
3116
6da94ec04325 SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
Edouard Tisserant
parents: 3115
diff changeset
   151
    msgsroot = etree.SubElement(result, "messages")
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   152
    for msgid, msgs in translated_messages:
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   153
        msgidel = etree.SubElement(msgsroot, "msgid")
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   154
        for msg in msgs:
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   155
            msgel = etree.SubElement(msgidel, "msg")
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   156
            for line in msg.split(b"\n"):
3116
6da94ec04325 SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
Edouard Tisserant
parents: 3115
diff changeset
   157
                lineel = etree.SubElement(msgel, "line")
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   158
                lineel.text = escape(line).decode()
3116
6da94ec04325 SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
Edouard Tisserant
parents: 3115
diff changeset
   159
6da94ec04325 SVGHMI: i18n: finished passing back translated messaged to xslt proc, finished template to generate corresponding javascript literals.
Edouard Tisserant
parents: 3115
diff changeset
   160
    return result
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   161
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   162
# Code below is based on :
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   163
#  cpython/Tools/i18n/pygettext.py
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   164
#  cpython/Tools/i18n/msgfmt.py
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   165
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   166
locpfx = b'#:svghmi.svg:'
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   167
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   168
pot_header = '''\
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   169
# SOME DESCRIPTIVE TITLE.
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   170
# Copyright (C) YEAR ORGANIZATION
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   171
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   172
#
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   173
msgid ""
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   174
msgstr ""
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   175
"Project-Id-Version: PACKAGE VERSION\\n"
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   176
"POT-Creation-Date: %(time)s\\n"
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   177
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   178
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   179
"Language-Team: LANGUAGE <LL@li.org>\\n"
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   180
"MIME-Version: 1.0\\n"
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   181
"Content-Type: text/plain; charset=UTF-8\\n"
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   182
"Content-Transfer-Encoding: 8bit\\n"
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
"Generated-By: SVGHMI 1.0\\n"
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
3918
9f0ef23569cb SVGHMI: i18n: fix .POT generation + other cosmetic fixes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3915
diff changeset
   185
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   186
'''
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   187
escapes = []
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   188
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   189
def make_escapes():
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   190
    global escapes
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   191
    escapes = [b"\%03o" % i for i in range(128)]
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   192
    for i in range(32, 127):
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   193
        escapes[i] = bytes([i])
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   194
    escapes[ord('\\')] = b'\\\\'
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   195
    escapes[ord('\t')] = b'\\t'
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   196
    escapes[ord('\r')] = b'\\r'
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   197
    escapes[ord('\n')] = b'\\n'
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   198
    escapes[ord('\"')] = b'\\"'
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   199
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   200
make_escapes()
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   201
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   202
def escape(s):
3918
9f0ef23569cb SVGHMI: i18n: fix .POT generation + other cosmetic fixes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3915
diff changeset
   203
    return b''.join([escapes[c] if c < 128 else bytes([c]) for c in s])
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   204
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   205
def normalize(s):
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   206
    # This converts the various Python string types into a format that is
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   207
    # appropriate for .po files, namely much closer to C style.
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   208
    lines = s.split(b'\n')
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   209
    if len(lines) == 1:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   210
        s = b'"' + escape(s) + b'"'
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   211
    else:
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   212
        if not lines[-1]:
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   213
            del lines[-1]
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   214
            lines[-1] = lines[-1] + b'\n'
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   215
        for i in range(len(lines)):
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   216
            lines[i] = escape(lines[i])
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   217
        lineterm = b'\\n"\n"'
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   218
        s = b'""\n"' + lineterm.join(lines) + b'"'
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   219
    return s
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   220
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   221
class POTWriter:
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   222
    def __init__(self):
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   223
        self.__messages = {}
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   224
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   225
    def ImportMessages(self, msgs):
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   226
        for  msg, label, svgid in msgs:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   227
            self.addentry(msg, label, svgid)
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   228
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   229
    def addentry(self, msg, label, svgid):
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   230
        entry = (label, svgid)
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   231
        self.__messages.setdefault(msg, set()).add(entry)
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   232
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   233
    def write(self, fp):
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   234
        timestamp = time.strftime('%Y-%m-%d %H:%M%z')
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   235
        header = pot_header % {'time': timestamp}
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   236
        fp.write(header.encode())
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   237
        reverse = {}
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   238
        for k, v in self.__messages.items():
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   239
            keys = list(v)
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   240
            keys.sort()
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   241
            reverse.setdefault(tuple(keys), []).append((k, v))
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   242
        rkeys = sorted(reverse.keys())
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   243
        for rkey in rkeys:
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   244
            rentries = reverse[rkey]
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   245
            rentries.sort()
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   246
            for k, v in rentries:
3112
bd20f9112014 SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button.
Edouard Tisserant
parents: 3108
diff changeset
   247
                v = list(v)
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   248
                v.sort()
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   249
                locline = locpfx
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   250
                for label, svgid in v:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   251
                    d = {b'label': label, b'svgid': svgid}
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   252
                    s = b' %(label)s:%(svgid)s' % d
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   253
                    if len(locline) + len(s) <= 78:
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   254
                        locline = locline + s
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   255
                    else:
3918
9f0ef23569cb SVGHMI: i18n: fix .POT generation + other cosmetic fixes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3915
diff changeset
   256
                        fp.write(locline + b'\n')
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   257
                        locline = locpfx + s
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   258
                if len(locline) > len(locpfx):
3918
9f0ef23569cb SVGHMI: i18n: fix .POT generation + other cosmetic fixes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3915
diff changeset
   259
                    fp.write(locline + b'\n')
9f0ef23569cb SVGHMI: i18n: fix .POT generation + other cosmetic fixes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3915
diff changeset
   260
                fp.write(b'msgid ' + normalize(k) + b'\n')
9f0ef23569cb SVGHMI: i18n: fix .POT generation + other cosmetic fixes.
Edouard Tisserant <edouard@beremiz.fr>
parents: 3915
diff changeset
   261
                fp.write(b'msgstr ""\n\n')
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   262
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   263
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   264
class POReader:
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   265
    def __init__(self):
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   266
        self.__messages = {}
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   267
3113
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   268
    def get_messages(self):
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   269
        return self.__messages
18133b90196e SVGHMI: i18n: now loads PO filesand match translation against catalog. Refactored a bit to move i18n related code in i18n.py
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3112
diff changeset
   270
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   271
    def add(self, ctxt, msgid, msgstr, fuzzy):
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   272
        "Add a non-fuzzy translation to the dictionary."
3115
77cfbf1aacf0 SVGHMI: i18n: various fixes about unicode encoding and converting translation result back to XML to feed xslt processor
Edouard Tisserant
parents: 3114
diff changeset
   273
        if not fuzzy and msgstr and msgid:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   274
            if ctxt is None:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   275
                self.__messages[msgid] = msgstr
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   276
            else:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   277
                self.__messages[b"%b\x04%b" % (ctxt, id)] = str
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   278
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   279
    def read(self, infile):
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   280
        ID = 1
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   281
        STR = 2
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   282
        CTXT = 3
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   283
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   284
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   285
        with open(infile, 'rb') as f:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   286
            lines = f.readlines()
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   287
            
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   288
        section = msgctxt = None
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   289
        fuzzy = 0
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   290
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   291
        # Start off assuming Latin-1, so everything decodes without failure,
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   292
        # until we know the exact encoding
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   293
        encoding = 'latin-1'
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   294
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   295
        # Parse the catalog
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   296
        lno = 0
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   297
        for l in lines:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   298
            l = l.decode(encoding)
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   299
            lno += 1
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   300
            # If we get a comment line after a msgstr, this is a new entry
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   301
            if l[0] == '#' and section == STR:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   302
                self.add(msgctxt, msgid, msgstr, fuzzy)
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   303
                section = msgctxt = None
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   304
                fuzzy = 0
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   305
            # Record a fuzzy mark
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   306
            if l[:2] == '#,' and 'fuzzy' in l:
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   307
                fuzzy = 1
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   308
            # Skip comments
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   309
            if l[0] == '#':
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   310
                continue
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   311
            # Now we are in a msgid or msgctxt section, output previous section
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   312
            if l.startswith('msgctxt'):
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   313
                if section == STR:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   314
                    self.add(msgctxt, msgid, msgstr, fuzzy)
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   315
                section = CTXT
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   316
                l = l[7:]
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   317
                msgctxt = b''
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   318
            elif l.startswith('msgid') and not l.startswith('msgid_plural'):
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   319
                if section == STR:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   320
                    self.add(msgctxt, msgid, msgstr, fuzzy)
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   321
                    if not msgid:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   322
                        # See whether there is an encoding declaration
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   323
                        p = HeaderParser()
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   324
                        charset = p.parsestr(msgstr.decode(encoding)).get_content_charset()
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   325
                        if charset:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   326
                            encoding = charset
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   327
                section = ID
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   328
                l = l[5:]
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   329
                msgid = msgstr = b''
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   330
                is_plural = False
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   331
            # This is a message with plural forms
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   332
            elif l.startswith('msgid_plural'):
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   333
                if section != ID:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   334
                    raise UserAddressedException(
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   335
                        'msgid_plural not preceded by msgid on %s:%d' % (infile, lno))
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   336
                l = l[12:]
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   337
                msgid += b'\0' # separator of singular and plural
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   338
                is_plural = True
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   339
            # Now we are in a msgstr section
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   340
            elif l.startswith('msgstr'):
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   341
                section = STR
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   342
                if l.startswith('msgstr['):
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   343
                    if not is_plural:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   344
                        raise UserAddressedException(
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   345
                            'plural without msgid_plural on %s:%d' % (infile, lno))
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   346
                    l = l.split(']', 1)[1]
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   347
                    if msgstr:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   348
                        msgstr += b'\0' # Separator of the various plural forms
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   349
                else:
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   350
                    if is_plural:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   351
                        raise UserAddressedException(
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   352
                            'indexed msgstr required for plural on  %s:%d' % (infile, lno))
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   353
                    l = l[6:]
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   354
            # Skip empty lines
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   355
            l = l.strip()
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   356
            if not l:
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   357
                continue
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   358
            l = ast.literal_eval(l)
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   359
            if section == CTXT:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   360
                msgctxt += l.encode(encoding)
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   361
            elif section == ID:
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   362
                msgid += l.encode(encoding)
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   363
            elif section == STR:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   364
                msgstr += l.encode(encoding)
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   365
            else:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   366
                raise UserAddressedException(
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   367
                    'Syntax error on %s:%d' % (infile, lno) + 'before:\n %s'%l)
3108
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   368
        # Add last entry
079419e7228d SVGHMI: Intermediate commit while implementing i18n. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   369
        if section == STR:
3915
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   370
            self.add(msgctxt, msgid, msgstr, fuzzy)
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   371
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   372
b5017dd5c049 SVGHMI: fix py3 consequences on i18n
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   373