1
/***************************************************************************
2
* Copyright (c) 2004 Werner Mayer <werner.wm.mayer@gmx.de> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
24
#include "PreCompiled.h"
26
#include "Translator.h"
28
#include <Base/Console.h>
29
#include <Base/Console.h>
30
#include <Base/Parameter.h>
31
#include <App/Application.h>
35
/** \defgroup i18n Internationalization with FreeCAD
37
* The internationalization of FreeCAD makes heavy use of the internationalization
38
* support of Qt. For more details refer to your Qt documentation.
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:
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.
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
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.
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.
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:
78
* Command Line: rcc.exe -name $(InputName) $(InputPath) -o "$(InputDir)qrc_$(InputName).cpp"
79
* Outputs: $(InputDir)qrc_$(InputName).cpp
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.
84
* Finally, you have to add a the line
87
* Q_INIT_RESOURCE(resource);
91
* where \a resource is the name of the .qrc file. That's all!
94
/* TRANSLATOR Gui::Translator */
96
Translator* Translator::_pcSingleton = 0;
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 */
109
Translator* Translator::instance(void)
112
_pcSingleton = new Translator;
116
void Translator::destruct (void)
123
Translator::Translator()
125
// This is needed for Qt's lupdate
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";
135
d->paths = directories();
138
Translator::~Translator()
144
TStringList Translator::supportedLanguages() const
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);
160
void Translator::activateLanguage (const char* lang)
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()) {
170
std::string Translator::activeLanguage() const
172
return d->activatedLanguage;
175
QStringList Translator::directories() const
177
//std::string mods = App::Application::Config()["AppHomePath"]+"Mod";
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"));
188
void Translator::addPath(const QString& path)
190
d->paths.push_back(path);
193
void Translator::installQMFiles(const QDir& dir, const char* locale)
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){
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
207
// okay, we need to install this file
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);
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
227
void Translator::refresh()
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) {
234
installQMFiles(dir, tld->second.c_str());
239
* Uninstalls all translators.
241
void Translator::removeTranslators()
243
for (std::list<QTranslator*>::iterator it = d->translators.begin(); it != d->translators.end(); ++it) {
244
qApp->removeTranslator(*it);
248
d->translators.clear();
251
#include "moc_Translator.cpp"