Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TranslationDataIS.py « mpcresources « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 74ab1b07e07d3ff9a9997dfeb2c6688abc0e01b4 (plain)
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
# (C) 2013, 2016 see Authors.txt
#
# This file is part of MPC-HC.
#
# MPC-HC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# MPC-HC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from TranslationData import *


class TranslationDataIS(TranslationData):
    sectionEntry = re.compile(ur'^\[(.*)\]\r\n', re.UNICODE)
    stringEntry = re.compile(ur'^en\.([^=]+)=(.+)\r\n', re.UNICODE)

    def loadFromIS(self, filename):
        self.empty()

        with codecs.open(filename, 'r', detectEncoding(filename)) as f:
            section = None
            for line in f:
                match = TranslationDataIS.stringEntry.match(line)
                if section and match:
                    if match.group(1) != u'langid':
                        self.strings[(section + '_' + match.group(1), match.group(2).replace(u'%n', ur'\n'))] = ''
                else:
                    match = TranslationDataIS.sectionEntry.match(line)
                    if match:
                        section = match.group(1)

    @staticmethod
    def translateIS(translationsConfigAndData, filenameBase, filenameRC):
        encoding = detectEncoding(filenameBase)
        with codecs.open(filenameBase, 'r', encoding) as fBase, \
                codecs.open(filenameRC, 'w', encoding) as fOut:
            section = None
            sectionData = []

            for line in fBase:
                match = TranslationDataIS.sectionEntry.match(line)
                if match or line == u'#if localize == "true"\r\n':
                    if section:
                        # Remove empty line at the end of a section
                        end = len(sectionData)
                        while end > 0 and sectionData[end - 1] == u'\r\n':
                            end -= 1
                        sectionData = sectionData[:end]

                        fOut.write(u'[' + section + u']')
                        for config, translationData in translationsConfigAndData:
                            fOut.write(u'\r\n')
                            translationData.translateISSection(config, section, sectionData, fOut)

                    if match:
                        if section:
                            fOut.write(u'\r\n\r\n')
                        section = match.group(1)
                        sectionData = []
                    else:
                        break
                elif section:
                    sectionData.append(line)
                else:
                    fOut.write(line)

    def translateISSection(self, config, section, sectionData, fOut):
        for line in sectionData:
            match = TranslationDataIS.stringEntry.match(line)
            if match:
                if match.group(1) != u'langid':
                    s = self.strings.get((section + '_' + match.group(1), match.group(2).replace(u'%n', ur'\n')))
                    if not s:
                        s = match.group(2)
                    line = '%s.%s=%s\r\n' % (
                        config.get('Info', 'langShortName'), match.group(1), s.replace(ur'\n', u'%n'))
                else:
                    line = '%s.langid=%0.8d\r\n' % (
                        config.get('Info', 'langShortName'), config.getint('Info', 'langId'))

                fOut.write(line)
            elif line == u'; English\r\n':
                fOut.write(u'; ' + config.get('Info', 'langName') + u'\r\n')
            elif line != u'WelcomeLabel1=[name/ver]\r\n':
                fOut.write(line)