File: resx2po.py

package info (click to toggle)
python-cron-descriptor 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 756 kB
  • sloc: python: 1,641; makefile: 7; sh: 4
file content (130 lines) | stat: -rw-r--r-- 4,949 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
119
120
121
122
123
124
125
126
127
128
129
130
# 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 logging import getLogger
from pathlib import Path

import polib

log = getLogger(__name__)

class Resx2Po:
    def __init__(self, en_resx: Path, translation_resx: Path, code: str, output_po: Path) -> None:
        if not en_resx.is_file():
            msg = f"EN Resx {en_resx.absolute()} not found"
            raise FileNotFoundError(msg)

        if not translation_resx.is_file():
            msg = f"Translation {code} {translation_resx.absolute()} Resx bound not found"
            raise FileNotFoundError(msg)

        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: Path) -> dict[str, str]:
        tree = ET.parse(resx)
        root = tree.getroot()
        translation_table = {}
        for first in root.findall("./data"):
            found_value = first.find("./value")
            if found_value is not None and found_value.text:
                translation_table[first.attrib["name"]] = found_value.text

        return translation_table

    def generate(self) -> None:
        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:
                log.warning("%s not found in %s resx", message_en_id, self.code)

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


code_list = {
    "cs": "cs_CZ",        # Czech
    "da": "da_DK",        # Danish
    "de": "de_DE",        # German
    "el": "el_GR",        # Greek
    "es": "es_ES",        # Spanish (Spain)
    "es-MX": "es_MX",     # Spanish (Mexico)
    "fa": "fa_IR",        # Persian (Iran)
    "fi": "fi_FI",        # Finnish
    "fr": "fr_FR",        # French (France)
    "he-IL": "he_IL",     # Hebrew (Israel)
    "hu": "hu_HU",        # Hungarian
    "it": "it_IT",        # Italian
    "ja": "ja_JP",        # Japanese
    "kk": "kk_KZ",        # Kazakh
    "ko": "ko_KR",        # Korean
    "nb": "nb_NO",        # Norwegian Bokmål
    "nl": "nl_NL",        # Dutch (Netherlands)
    "pl": "pl_PL",        # Polish
    "pt": "pt_PT",        # Portuguese (Portugal)
    "ro": "ro_RO",        # Romanian
    "ru": "ru_RU",        # Russian
    "sl": "sl_SI",        # Slovenian
    "sv": "sv_SE",        # Swedish
    "tr": "tr_TR",        # Turkish
    "uk": "uk_UA",        # Ukrainian
    "vi": "vi_VN",        # Vietnamese
    "zh-Hans": "zh_CN",   # Simplified Chinese (China)
    "zh-Hant": "zh_TW",   # Traditional Chinese (Taiwan)
}

output_dir = Path("./locale")
input_dir = Path("./source")

for from_code, to_code in code_list.items():
    output_file = output_dir.joinpath(f"{to_code}.po")
    Resx2Po(
        input_dir.joinpath("Resources.resx"),
        input_dir.joinpath(f"Resources.{from_code}.resx"),
        to_code,
        output_file,
    )