File: resx2po.py

package info (click to toggle)
python-cron-descriptor 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 520 kB
  • sloc: python: 1,222; sh: 14; makefile: 7
file content (118 lines) | stat: -rw-r--r-- 4,030 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# The MIT License (MIT)
#
# Copyright (c) 2016 Adam Schubert
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import datetime

import xml.etree.ElementTree as ET
from pathlib import Path
import polib


class Resx2Po:
    def __init__(self, en_resx: Path, translation_resx: Path, code: str, output_po: Path):
        if not en_resx.is_file():
            raise Exception("EN Resx {} not found".format(en_resx.absolute()))

        if not translation_resx.is_file():
            raise Exception("Translation {} {} Resx bound not found".format(code, translation_resx.absolute()))

        self.en_resx = self.resx2dict(en_resx)
        self.translation_resx = self.resx2dict(translation_resx)
        self.code = code
        self.output_po = output_po

        self.generate()

    def resx2dict(self, resx):
        tree = ET.parse(resx)
        root = tree.getroot()
        translation_table = {}
        for first in root.findall('./data'):
            translation_table[first.attrib['name']] = first.find('./value').text

        return translation_table

    def generate(self):
        po = polib.POFile()
        now = datetime.datetime.now(datetime.timezone.utc)
        po.metadata = {
            'Project-Id-Version': '1.0',
            'Report-Msgid-Bugs-To': 'adam.schubert@sg1-game.net',
            'POT-Creation-Date': now.strftime("%Y-%m-%d %H:%M%z"),
            'PO-Revision-Date': now.strftime("%Y-%m-%d %H:%M%z"),
            'Last-Translator': 'Adam Schubert <adam.schubert@sg1-game.net>',
            'Language-Team': '',
            'MIME-Version': '1.0',
            'Content-Type': 'text/plain; charset=utf-8',
            'Content-Transfer-Encoding': '8bit',
            'Language': self.code
        }

        for message_en_id, message_en in self.en_resx.items():
            if message_en_id in self.translation_resx:
                entry = polib.POEntry(
                    msgid=message_en,
                    msgstr=self.translation_resx[message_en_id],
                    comment=message_en_id
                )
                po.append(entry)
            else:
                print('WARNING: {} not found in {} resx'.format(message_en_id, self.code))

        po.save(str(self.output_po.absolute()))


code_list = {
    'da': 'da_DK',

    # 'de': 'de_DE',
    # 'es': 'es_ES',
    # 'es-MX': 'es_MX',
    # 'fa': 'fa_IR',
    # 'fi': 'fi_FI',
    # 'fr': 'fr_FR',
    # 'it': 'it_IT',
    # 'ja': 'ja_IP',
    # 'ko': 'ko_KR',
    # 'nb': 'nb_NO',
    # 'nl': 'nl_NL',
    # 'pl': 'pl_PL',
    # 'pt': 'pt_PT',
    # 'ro': 'ro_RO',
    # 'ru': 'ru_RU',
    # 'sl': 'sl_SI',
    # 'sv': 'sv_SE',
    # 'tr': 'tr_TR',
    # 'uk': 'uk_UA',
    # 'zh-Hans': 'zh_Hans_CN',
    # 'zh-Hant': 'zh_Hant',
}

output_dir = Path('../locale')

for from_code, to_code in code_list.items():
    output_file = output_dir.joinpath('{}.po'.format(to_code))
    Resx2Po(
        Path('Resources.resx'),
        Path('Resources.{}.resx'.format(from_code)),
        to_code,
        output_file
    )