~ubuntu-branches/ubuntu/intrepid/kdesdk/intrepid-updates

« back to all changes in this revision

Viewing changes to poxml/transxx.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-28 10:11:43 UTC
  • mto: This revision was merged to the branch mainline in revision 37.
  • Revision ID: james.westby@ubuntu.com-20080528101143-gzc3styjz1b70zxu
Tags: upstream-4.0.80
ImportĀ upstreamĀ versionĀ 4.0.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <iostream>
2
 
using namespace std;
3
 
#include "GettextParser.hpp"
4
 
#include <fstream>
5
 
#include "GettextLexer.hpp"
6
 
 
7
 
#include <qregexp.h>
8
 
#include <qdatetime.h>
9
 
#include <qfileinfo.h>
10
 
 
11
 
int main(int argc, char **argv)
12
 
{
13
 
    if ( argc != 2 && argc != 4 ) {
14
 
        qWarning( "usage: %s [--text translation] potfile", argv[0] );
15
 
        return -1;
16
 
    }
17
 
 
18
 
    QString translation = "xx";
19
 
    QCString filename;
20
 
 
21
 
    if( argc == 4 ) {
22
 
        if( argv[1]!=QString("--text") ) {
23
 
            qWarning( "usage: %s [--text translation] potfile", argv[0] );
24
 
            return -1;
25
 
        }
26
 
        translation = QString::fromLocal8Bit(argv[2]);
27
 
        filename = argv[3];
28
 
    } else {
29
 
        filename = argv[1];
30
 
    }
31
 
 
32
 
    MsgList translated;
33
 
 
34
 
    try {
35
 
        ifstream s(filename);
36
 
        GettextLexer lexer(s);
37
 
        GettextParser parser(lexer);
38
 
        translated = parser.file();
39
 
 
40
 
    } catch(exception& e) {
41
 
        cerr << "exception: " << e.what() << endl;
42
 
        return 1;
43
 
    }
44
 
 
45
 
    const bool is_desktop = filename.find( "desktop_") >= 0;
46
 
 
47
 
    // The header is the last item (due too the sorting)
48
 
    MsgList::const_iterator header = --translated.end();
49
 
    if ( ( header == translated.end() ) || ( ! ( *header ).msgid.isEmpty() ) )
50
 
    {
51
 
        cerr << "Cannot find correct header msgid\n";
52
 
        cout << "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
53
 
        cout << "\"Plural-Forms: nplurals=1; plural=0;\\n\"\n";
54
 
    }
55
 
    else
56
 
    {
57
 
        QStringList headerLines = QStringList::split( "\\n", ( *header ).msgstr, false );
58
 
        QFileInfo fi( QString::fromLocal8Bit( filename ) );
59
 
        QString projectId( "Project-Id-Version: " ); 
60
 
        projectId += fi.baseName( false );
61
 
        headerLines.gres( QRegExp( "^Project-Id-Version:.*" ), projectId );
62
 
        headerLines.gres( QRegExp( "^Last-Translator:.*" ), "Last-Translator: transxx program <null@kde.org>" );
63
 
        headerLines.gres( QRegExp( "^Language-Team:.*" ), "Language-Team: Test Language <kde-i18n-doc@kde.org>" );
64
 
        QString revisionDate ( "PO-Revision-Date: " );
65
 
        const QDateTime dt = QDateTime::currentDateTime( Qt::UTC );
66
 
        revisionDate += dt.toString( "yyyy-MM-dd hh:mm+0000" );
67
 
        headerLines.gres( QRegExp( "^PO-Revision-Date:.*" ), revisionDate );
68
 
        headerLines << "Plural-Forms: nplurals=1; plural=0;";
69
 
        outputMsg ( "msgid", "" );
70
 
        outputMsg ( "msgstr", escapePO( headerLines.join("\\n") + "\\n" ) );
71
 
    }
72
 
    cout << "\n";
73
 
 
74
 
    for (MsgList::ConstIterator it = translated.begin();
75
 
         it != translated.end(); ++it)
76
 
    {
77
 
        QString msgid = ( *it ).msgid;
78
 
        QString msgid_plural = ( *it ).msgid_plural;
79
 
        if ( !msgid.isEmpty() ) {
80
 
            outputMsg("msgid", escapePO( msgid) );
81
 
 
82
 
            if ( ! msgid_plural.isEmpty() ) {
83
 
                outputMsg("msgid_plural", escapePO( msgid_plural ) );
84
 
            }
85
 
 
86
 
            QString msgstr;
87
 
 
88
 
            if ( msgid.find( "Definition of PluralForm" ) != -1 ) {
89
 
                outputMsg("msgstr", "NoPlural");
90
 
                cout << "\n";
91
 
                continue;
92
 
            }
93
 
 
94
 
            if ( is_desktop ) {
95
 
                msgstr = msgid.left( msgid.find( '=' ) + 1);
96
 
                msgstr += translation + msgid.mid( msgid.find( '=' ) + 1) + translation;
97
 
                outputMsg( "msgstr", escapePO(msgstr) );
98
 
                cout << "\n";
99
 
                continue;
100
 
            }
101
 
 
102
 
            if (msgid.startsWith("_n: ") || msgid.startsWith("_: ") ) { // KDE extentions
103
 
                msgid = msgid.mid(msgid.find("\\n") + 2, msgid.length());
104
 
            }
105
 
 
106
 
            if (msgid.endsWith("%"))
107
 
                msgstr = translation + msgid + " " + translation;
108
 
            else
109
 
                msgstr = translation + msgid + translation;
110
 
 
111
 
            // Note: msgid has been modified, so we need to go back to the original version by the help of the iterator
112
 
            // (Gettext is not aware of the KDE-specific handling, so it really wants a \n at start and at end in the msgstr if they were in the msgid )
113
 
            if ( ( *it ).msgid.endsWith( "\\n" ) && ! ( *it ).msgid.endsWith( "\\\\n" ))
114
 
                msgstr += "\n";
115
 
            if ( ( *it ).msgid.startsWith( "\\n" ) )
116
 
                msgstr.prepend( "\n" );
117
 
 
118
 
            if ( msgid_plural.isEmpty() ) {
119
 
                outputMsg("msgstr", escapePO( msgstr) );
120
 
            }
121
 
            else
122
 
            {
123
 
                outputMsg("msgstr[0]", escapePO( msgstr) );
124
 
            }
125
 
            cout << "\n";
126
 
        }
127
 
    }
128
 
 
129
 
}
130