~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/translationmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * translationmanager.cpp
 
3
 * Copyright (C) 2006  Remko Troncon, Justin Karneges
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include <QTranslator>
 
22
#include <QCoreApplication>
 
23
#include <QFile>
 
24
#include <QDir>
 
25
 
 
26
#include "translationmanager.h"
 
27
#include "applicationinfo.h"
 
28
#include "varlist.h"
 
29
 
 
30
TranslationManager::TranslationManager()
 
31
{
 
32
        // Initialize
 
33
        currentLanguage_ = "en";
 
34
        QString currentLanguageName = QT_TR_NOOP("language_name");
 
35
 
 
36
        // The application translator
 
37
        translator_ = new QTranslator(0);
 
38
 
 
39
        // The qt translator
 
40
        qt_translator_ = new QTranslator(0);
 
41
 
 
42
        // Self-destruct
 
43
        connect(QCoreApplication::instance(),SIGNAL(aboutToQuit()),SLOT(deleteLater()));
 
44
}
 
45
 
 
46
TranslationManager::~TranslationManager()
 
47
{
 
48
        QCoreApplication::instance()->removeTranslator(translator_);
 
49
        delete translator_;
 
50
        translator_ = 0;
 
51
        
 
52
        QCoreApplication::instance()->removeTranslator(qt_translator_);
 
53
        delete qt_translator_;
 
54
        qt_translator_ = 0;
 
55
}
 
56
 
 
57
TranslationManager* TranslationManager::instance() 
 
58
{
 
59
        if (!instance_) {
 
60
                instance_ = new TranslationManager();
 
61
        }
 
62
        return instance_;
 
63
}
 
64
 
 
65
const QString& TranslationManager::currentLanguage() const
 
66
{
 
67
        return currentLanguage_;
 
68
}
 
69
 
 
70
QString TranslationManager::currentXMLLanguage() const
 
71
{
 
72
        QString xmllang = currentLanguage_;
 
73
        xmllang.replace('_',"-");
 
74
        int at_index = xmllang.find('@');
 
75
        if (at_index > 0)
 
76
                xmllang = xmllang.left(at_index);
 
77
        return xmllang;
 
78
}
 
79
 
 
80
void TranslationManager::loadTranslation(const QString& language)
 
81
{
 
82
        // The default translation
 
83
        if(language == "en") {
 
84
                currentLanguage_ = language;
 
85
                //currentLanguageName_ = "English";
 
86
                QCoreApplication::instance()->removeTranslator(translator_);
 
87
                QCoreApplication::instance()->removeTranslator(qt_translator_);
 
88
                return;
 
89
        }
 
90
        
 
91
        // Try loading the translation file
 
92
        QStringList dirs = translationDirs();
 
93
        foreach(QString dir, dirs) {
 
94
                if(!QFile::exists(dir))
 
95
                        continue;
 
96
                if (translator_->load("psi_" + language, dir)) {
 
97
                        // try to load qt library translation
 
98
                        qt_translator_->load("qt_" + language, dir);
 
99
                        if (currentLanguage_ == "en") {
 
100
                                QCoreApplication::instance()->installTranslator(translator_);
 
101
                                QCoreApplication::instance()->installTranslator(qt_translator_);
 
102
                        }
 
103
                        currentLanguage_ = language;
 
104
                        break;
 
105
                }
 
106
        }
 
107
}
 
108
 
 
109
VarList TranslationManager::availableTranslations()
 
110
{
 
111
        VarList langs;
 
112
 
 
113
        // We always support english
 
114
        langs.set("en", "English");
 
115
        
 
116
        // Search the paths
 
117
        QStringList dirs = TranslationManager::translationDirs();
 
118
        for(QStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it) {
 
119
                if(!QFile::exists(*it))
 
120
                        continue;
 
121
                QDir d(*it);
 
122
                QStringList entries = d.entryList();
 
123
                for(QStringList::Iterator it2 = entries.begin(); it2 != entries.end(); ++it2) {
 
124
                        if(*it2 == "." || *it2 == "..")
 
125
                                continue;
 
126
 
 
127
                        QString str = *it2;
 
128
                        // verify that it is a language file
 
129
                        if(str.left(4) != "psi_")
 
130
                                continue;
 
131
                        int n = str.find('.', 4);
 
132
                        if(n == -1)
 
133
                                continue;
 
134
                        if(str.mid(n) != ".qm")
 
135
                                continue;
 
136
                        QString lang = str.mid(4, n-4);
 
137
 
 
138
                        //printf("found [%s], lang=[%s]\n", str.latin1(), lang.latin1());
 
139
 
 
140
                        // get the language_name
 
141
                        QString name = QString("[") + str + "]";
 
142
                        QTranslator t(0);
 
143
                        if(!t.load(str, *it))
 
144
                                continue;
 
145
 
 
146
                        //Is translate equivalent to the old findMessage? I hope so
 
147
                        //Qt4 conversion
 
148
                        QString s = t.translate("@default", "language_name");
 
149
                        if(!s.isEmpty())
 
150
                                name = s;
 
151
 
 
152
                        langs.set(lang, name);
 
153
                }
 
154
        }
 
155
        
 
156
        return langs;
 
157
}
 
158
 
 
159
QStringList TranslationManager::translationDirs() const
 
160
{
 
161
        QStringList dirs;
 
162
        QString subdir = "";
 
163
        dirs += "." + subdir;
 
164
        dirs += ApplicationInfo::homeDir() + subdir;
 
165
        dirs += ApplicationInfo::resourcesDir() + subdir;
 
166
        return dirs;
 
167
}
 
168
 
 
169
 
 
170
TranslationManager* TranslationManager::instance_ = NULL;