1 /*
2  * This file is part of the PySide Tools project.
3  *
4  * Copyright (C) 1992-2006 Trolltech AS. All rights reserved.
5  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
6  *
7  * Contact: PySide team <pyside@openbossa.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #include "metatranslator.h"
26 
27 #include <QMap>
28 #include <stdio.h>
29 
30 typedef QMap<QByteArray, MetaTranslatorMessage> TMM;
31 typedef QList<MetaTranslatorMessage> TML;
32 
33 /*
34   Augments a MetaTranslator with trivially derived translations.
35 
36   For example, if "Enabled:" is consistendly translated as "Eingeschaltet:" no
37   matter the context or the comment, "Eingeschaltet:" is added as the
38   translation of any untranslated "Enabled:" text and is marked Unfinished.
39 
40   Returns the number of additional messages that this heuristic translated.
41 */
42 
applySameTextHeuristic(MetaTranslator * tor)43 int applySameTextHeuristic( MetaTranslator *tor )
44 {
45     TMM translated;
46     TMM avoid;
47     TMM::Iterator t;
48     TML untranslated;
49     TML::Iterator u;
50     TML all = tor->messages();
51     TML::Iterator it;
52     int inserted = 0;
53 
54     for ( it = all.begin(); it != all.end(); ++it ) {
55         if ( (*it).type() == MetaTranslatorMessage::Unfinished ) {
56             if ( !(*it).isTranslated() )
57                 untranslated.append( *it );
58         } else {
59             QByteArray key = (*it).sourceText();
60             t = translated.find( key );
61             if ( t != translated.end() ) {
62                 /*
63                   The same source text is translated at least two
64                   different ways. Do nothing then.
65                 */
66                 if ( (*t).translations() != (*it).translations() ) {
67                     translated.remove( key );
68                     avoid.insert( key, *it );
69                 }
70             } else if ( !avoid.contains(key) && (*it).isTranslated() ) {
71                 translated.insert( key, *it );
72             }
73         }
74     }
75 
76     for ( u = untranslated.begin(); u != untranslated.end(); ++u ) {
77         QByteArray key = (*u).sourceText();
78         t = translated.find( key );
79         if ( t != translated.end() ) {
80             MetaTranslatorMessage m( *u );
81             m.setTranslations( (*t).translations() );
82             tor->insert( m );
83             inserted++;
84         }
85     }
86     return inserted;
87 }
88