summaryrefslogtreecommitdiff
path: root/l10ntools/source/gConvPo.cxx
blob: f4460da359a415ecedd5080fe8d1c42686746665 (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include <string>
#include <vector>

#include "gL10nMem.hxx"
#include "gConvPo.hxx"
#include <iostream>
#include <fstream>
#include <cstdlib>



convert_po::convert_po(l10nMem& crMemory)
                      : convert_gen(crMemory),
                        mbExpectId(false),
                        mbExpectStr(false),
                        mbFuzzy(false)
{
    // Po files are handled special
    mbLoadMode = true;
}



convert_po::~convert_po()
{
}



void convert_po::startLook()
{
    std::string sFileName, sNewKey;
    int         i;


    if (!msKey.size() || !msId.size())
        return;

    // split key into filename and real key
    i = msKey.find("#");
    sFileName = msKey.substr(0, i);
    sNewKey   = msKey.substr(i+1);

    // load in db
    if (msId.size())
        mcMemory.loadEntryKey(miLineNo, sFileName, sNewKey, msId, msStr, mbFuzzy);

    // and prepare for new entry
    msKey.clear();
    msId.clear();
    msStr.clear();
}



void convert_po::setValue(char *syyText, int iLineCnt)
{
    if (mbExpectId)
        msId = syyText;
    if (mbExpectStr)
        msStr = syyText;
    mbExpectId  =
    mbExpectStr = false;
    miLineNo   += iLineCnt;
}



void convert_po::setFuzzy()
{
    mbFuzzy = true;
}



void convert_po::setKey(char *syyText)
{
    int i;


    // Activate "look for msg" mode.
    startLook();

    // skip "#:" and any blanks
    for (syyText += 2; *syyText == ' ' || *syyText == '\t'; ++syyText) ;
        msKey = syyText;

    // remove trailing blanks
    for (i = msKey.size() -1; msKey[i] == '\r' || msKey[i] == ' ' || msKey[i] == '\t'; --i) ;
        msKey.erase(i+1);
}



void convert_po::setMsgId()
{
    mbExpectId = true;
}



void convert_po::setMsgStr()
{
    mbExpectStr = true;
}



void convert_po::handleNL()
{
    ++miLineNo;
}



extern int polex(void);
void convert_po::execute()
{
    if (mbMergeMode)
        throw l10nMem::showError("Merge not implemented");

    polex();
    startLook();
}



void convert_po::startSave(const std::string& sLanguage,
                           const std::string& sFile)
{
    std::string sFilePath = msTargetPath + sLanguage + sFile;
    outBuffer.open(sFilePath.c_str(), std::ios::out);

    if (!outBuffer.is_open())
        throw l10nMem::showError("Cannot open " + sFilePath + " for writing");

    l10nMem::showDebug("writing file (" + sFilePath + ")");

    std::ostream outFile(&outBuffer);

    // Set license header
    outFile << "#*************************************************************" << std::endl
            << "#*"                                                             << std::endl
            << "#* This file is part of the LibreOffice project.              " << std::endl
            << "#*"                                                             << std::endl
            << "#* This Source Code Form is subject to the terms of the       " << std::endl
            << "#* Mozilla Public License, v. 2.0. If a copy of the MPL was   " << std::endl
            << "#* not distributed with this file, You can obtain one at      " << std::endl
            << "#* http://mozilla.org/MPL/2.0/."                                << std::endl
            << "#*"                                                             << std::endl
            << "#* This file incorporates work covered by the following       " << std::endl
            << "#* license notice :"                                            << std::endl
            << "#*"                                                             << std::endl
            << "#* Licensed to the Apache Software Foundation (ASF) under one " << std::endl
            << "#* or more contributor license agreements. See the NOTICE file" << std::endl
            << "#* distributed with this work for additional information      " << std::endl
            << "#* regarding copyright ownership. The ASF licenses this file  " << std::endl
            << "#* to you under the Apache License, Version 2.0               " << std::endl
            << "#* (the \"License\"); you may not use this file except in     " << std::endl
            << "#* compliance with the License.You may obtain a copy of the   " << std::endl
            << "#* License at http ://www.apache.org/licenses/LICENSE-2.0 .   " << std::endl
            << "#*"                                                             << std::endl
            << "#************************************************************"  << std::endl
            << "msgid \"\""                                                     << std::endl
            << "msgstr \"\""                                                    << std::endl
            << "\"Project-Id-Version: AOO-4-xx\\n\""                            << std::endl
            << "\"POT-Creation-Date: \\n\""                                     << std::endl
            << "\"PO-Revision-Date: \\n\""                                      << std::endl
            << "\"Last-Translator: genLang (build process)\\n\""                << std::endl
            << "\"Language-Team: \\n\""                                         << std::endl
            << "\"MIME-Version: 1.0\\n\""                                       << std::endl
            << "\"Content-Type: text/plain; charset=UTF-8\\n\""                 << std::endl
            << "\"Content-Transfer-Encoding: 8bit\\n\""                         << std::endl
            << "\"X-Generator: genLang\\n\""                                    << std::endl
            << std::endl;
}



void convert_po::save(const std::string& sFileName,
                      const std::string& sKey,
                      const std::string& sENUStext,
                      const std::string& sText,
                      bool               bFuzzy)
{
    std::ostream outFile(&outBuffer);

    outFile << std::endl << "#: " << sFileName << "#" << sKey << std::endl;
    if (bFuzzy)
        outFile << "#, fuzzy" << std::endl;
    outFile << "msgid  \"" << sENUStext << "\"" << std::endl
            << "msgstr \"" << sText     << "\"" << std::endl;
}




void convert_po::endSave()
{
    outBuffer.close();
}