~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Gui/Language/Translator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2004 Werner Mayer <werner.wm.mayer@gmx.de>              *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This library is free software; you can redistribute it and/or         *
 
7
 *   modify it under the terms of the GNU Library General Public           *
 
8
 *   License as published by the Free Software Foundation; either          *
 
9
 *   version 2 of the License, or (at your option) any later version.      *
 
10
 *                                                                         *
 
11
 *   This library  is distributed in the hope that it will be useful,      *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU Library General Public License for more details.                  *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU Library General Public     *
 
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
 
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
 
 
26
#include "Translator.h"
 
27
 
 
28
#include <Base/Console.h>
 
29
#include <Base/Console.h>
 
30
#include <Base/Parameter.h>
 
31
#include <App/Application.h>
 
32
 
 
33
using namespace Gui;
 
34
 
 
35
/** \defgroup i18n Internationalization with FreeCAD
 
36
 *
 
37
 * The internationalization of FreeCAD makes heavy use of the internationalization
 
38
 * support of Qt. For more details refer to your Qt documentation.
 
39
 * 
 
40
 * \section stepbystep Step by step
 
41
 * To integrate a new language into FreeCAD or one of its application modules
 
42
 * you have to perform the following steps:
 
43
 *
 
44
 * \subsection tsfile Creation of a .ts file
 
45
 * First you have to generate a .ts file for the language to be translated. You can do this
 
46
 * by running the \a lupdate tool in the \a bin path of your Qt installation. As argument
 
47
 * you can specify either all related source files and the .ts output file or a Qt project 
 
48
 * file (.pro) which contains all relevant source files.
 
49
 *
 
50
 * \subsection translate Translation into your language
 
51
 * To translate the english string literals into the language you want to support you can open your
 
52
 * .ts file with \a QtLinguist and translate all literals by hand. Another way
 
53
 * for translation is to use the tool \a tsauto from Sebastien Fricker.This tool uses the 
 
54
 * engine from Google web page (www.google.com). tsauto supports the languages
 
55
 * \li english
 
56
 * \li french
 
57
 * \li german
 
58
 * \li italian
 
59
 * \li portuguese and
 
60
 * \li spanish
 
61
 *
 
62
 * \remark To get most of the literals translated you should have removed all
 
63
 * special characters (like &, !, ?, ...). Otherwise the translation could fail.
 
64
 * After having translated all literals you can load the .ts file into QtLinguist and 
 
65
 * invoke the menu item \a Release which generates the binary .qm file.
 
66
 *
 
67
 * \subsection usets Integration of the .qm file
 
68
 * The .qm file should now be integrated into the GUI library (either of FreeCAD
 
69
 * itself or its application module). The .qm file will be embedded into the
 
70
 * resulting binary file. So, at runtime you don't need any .qm files any
 
71
 * more. Indeed you will have a bigger binary file but you haven't any troubles
 
72
 * concerning missing .qm files.
 
73
 *
 
74
 * To integrate the .qm file into the executable you have to create a resource file (.qrc), first.
 
75
 * This is an XML file where you can append the .qm file. For the .qrc file you have to define the following
 
76
 * curstom build step inside the Visual Studio project file:
 
77
 *
 
78
 * Command Line: rcc.exe -name $(InputName) $(InputPath) -o "$(InputDir)qrc_$(InputName).cpp"
 
79
 * Outputs:      $(InputDir)qrc_$(InputName).cpp
 
80
 * 
 
81
 * For the gcc build system you just have to add the line <resourcefile>.qrc to the BUILT_SOURCES
 
82
 * sources section of the Makefile.am, run automake and configure (or ./confog.status) afterwards.
 
83
 *
 
84
 * Finally, you have to add a the line
 
85
 * \code
 
86
 * 
 
87
 * Q_INIT_RESOURCE(resource);
 
88
 *
 
89
 * \endcode
 
90
 * 
 
91
 * where \a resource is the name of the .qrc file. That's all!
 
92
 */
 
93
 
 
94
/* TRANSLATOR Gui::Translator */
 
95
 
 
96
Translator* Translator::_pcSingleton = 0;
 
97
 
 
98
namespace Gui {
 
99
class TranslatorP
 
100
{
 
101
public:
 
102
    std::string activatedLanguage; /**< Active language */
 
103
    std::map<std::string, std::string> mapLanguageTopLevelDomain;
 
104
    std::list<QTranslator*> translators; /**< A list of all created translators */
 
105
    QStringList paths;
 
106
};
 
107
}
 
108
 
 
109
Translator* Translator::instance(void)
 
110
{
 
111
    if (!_pcSingleton)
 
112
        _pcSingleton = new Translator;
 
113
    return _pcSingleton;
 
114
}
 
115
 
 
116
void Translator::destruct (void)
 
117
{
 
118
    if (_pcSingleton)
 
119
        delete _pcSingleton;
 
120
    _pcSingleton=0;
 
121
}
 
122
 
 
123
Translator::Translator()
 
124
{
 
125
    // This is needed for Qt's lupdate
 
126
    d = new TranslatorP;
 
127
    d->mapLanguageTopLevelDomain[QT_TR_NOOP("English" )] = "en";
 
128
    d->mapLanguageTopLevelDomain[QT_TR_NOOP("German"  )] = "de";
 
129
    d->mapLanguageTopLevelDomain[QT_TR_NOOP("French"  )] = "fr";
 
130
    d->mapLanguageTopLevelDomain[QT_TR_NOOP("Italian" )] = "it";
 
131
    d->mapLanguageTopLevelDomain[QT_TR_NOOP("Japanese")] = "jp";
 
132
    d->mapLanguageTopLevelDomain[QT_TR_NOOP("Chinese" )] = "cn";
 
133
    d->activatedLanguage = "English";
 
134
 
 
135
    d->paths = directories();
 
136
}
 
137
 
 
138
Translator::~Translator()
 
139
{
 
140
    removeTranslators();
 
141
    delete d;
 
142
}
 
143
 
 
144
TStringList Translator::supportedLanguages() const
 
145
{
 
146
    // List all .qm files
 
147
    TStringList languages;
 
148
    QDir dir(QLatin1String(":/translations"));
 
149
    for (std::map<std::string,std::string>::const_iterator it = d->mapLanguageTopLevelDomain.begin();
 
150
        it != d->mapLanguageTopLevelDomain.end(); ++it) {
 
151
        QString filter = QString::fromAscii("*_%1.qm").arg(QLatin1String(it->second.c_str()));
 
152
        QStringList fileNames = dir.entryList(QStringList(filter), QDir::Files, QDir::Name);
 
153
        if (!fileNames.isEmpty())
 
154
            languages.push_back(it->first);
 
155
    }
 
156
 
 
157
    return languages;
 
158
}
 
159
 
 
160
void Translator::activateLanguage (const char* lang)
 
161
{
 
162
    removeTranslators(); // remove the currently installed translators
 
163
    d->activatedLanguage = lang;
 
164
    TStringList languages = supportedLanguages();
 
165
    if (std::find(languages.begin(), languages.end(), lang) != languages.end()) {
 
166
        refresh();
 
167
    }
 
168
}
 
169
 
 
170
std::string Translator::activeLanguage() const
 
171
{
 
172
    return d->activatedLanguage;
 
173
}
 
174
 
 
175
QStringList Translator::directories() const
 
176
{
 
177
    //std::string mods = App::Application::Config()["AppHomePath"]+"Mod";
 
178
    QStringList list;
 
179
    //QStringList dirs;
 
180
    //QDir dir(QLatin1String(mods.c_str()));
 
181
    //dirs = dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot, QDir::Name);
 
182
    //for (QStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it)
 
183
    //    list.push_back(dir.filePath(*it));
 
184
    list.push_back(QLatin1String(":/translations"));
 
185
    return list;
 
186
}
 
187
 
 
188
void Translator::addPath(const QString& path)
 
189
{
 
190
    d->paths.push_back(path);
 
191
}
 
192
 
 
193
void Translator::installQMFiles(const QDir& dir, const char* locale)
 
194
{
 
195
    QString filter = QString::fromAscii("*_%1.qm").arg(QLatin1String(locale));
 
196
    QStringList fileNames = dir.entryList(QStringList(filter), QDir::Files, QDir::Name);
 
197
    for (QStringList::Iterator it = fileNames.begin(); it != fileNames.end(); ++it){
 
198
        bool ok=false;
 
199
        for (std::list<QTranslator*>::const_iterator tt = d->translators.begin();
 
200
            tt != d->translators.end(); ++tt) {
 
201
            if ((*tt)->objectName() == *it) {
 
202
                ok = true; // this file is already installed
 
203
                break;
 
204
            }
 
205
        }
 
206
 
 
207
        // okay, we need to install this file
 
208
        if (!ok) {
 
209
            QTranslator* translator = new QTranslator;
 
210
            translator->setObjectName(*it);
 
211
            if (translator->load(dir.filePath(*it))) {
 
212
                qApp->installTranslator(translator);
 
213
                d->translators.push_back(translator);
 
214
            }
 
215
            else {
 
216
                delete translator;
 
217
            }
 
218
        }
 
219
    }
 
220
}
 
221
 
 
222
/**
 
223
 * This method checks for newly added (internal) .qm files which might be added at runtime. This e.g. happens if a plugin
 
224
 * gets loaded at runtime. For each newly added files that supports the currently set language a new translator object is created 
 
225
 * to load the file.
 
226
 */
 
227
void Translator::refresh()
 
228
{
 
229
    std::map<std::string, std::string>::iterator tld = d->mapLanguageTopLevelDomain.find(d->activatedLanguage);
 
230
    if (tld == d->mapLanguageTopLevelDomain.end())
 
231
        return; // no language activated
 
232
    for (QStringList::iterator it = d->paths.begin(); it != d->paths.end(); ++it) {
 
233
        QDir dir(*it);
 
234
        installQMFiles(dir, tld->second.c_str());
 
235
    }
 
236
}
 
237
 
 
238
/**
 
239
 * Uninstalls all translators.
 
240
 */
 
241
void Translator::removeTranslators()
 
242
{
 
243
    for (std::list<QTranslator*>::iterator it = d->translators.begin(); it != d->translators.end(); ++it) {
 
244
        qApp->removeTranslator(*it);
 
245
        delete *it;
 
246
    }
 
247
 
 
248
    d->translators.clear();
 
249
}
 
250
 
 
251
#include "moc_Translator.cpp"