| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 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 |
#ifndef GUI_TRANSLATOR_H |
| 24 |
#define GUI_TRANSLATOR_H |
| 25 |
|
| 26 |
#include <QObject> |
| 27 |
#include <list> |
| 28 |
#include <map> |
| 29 |
#include <memory> |
| 30 |
#include <string> |
| 31 |
#include <FCGlobal.h> |
| 32 |
|
| 33 |
|
| 34 |
class QDir; |
| 35 |
|
| 36 |
namespace Gui |
| 37 |
{ |
| 38 |
|
| 39 |
using TStringList = std::list<std::string>; |
| 40 |
using TStringMap = std::map<std::string, std::string>; |
| 41 |
|
| 42 |
/** |
| 43 |
* The Translator class uses Qt's QTranslator objects to change the language of the application |
| 44 |
* on the fly. |
| 45 |
* For more details see the \link i18n.html Internationalization with FreeCAD \endlink |
| 46 |
* documentation. |
| 47 |
* |
| 48 |
* \author Werner Mayer |
| 49 |
*/ |
| 50 |
class TranslatorP; |
| 51 |
class GuiExport Translator: public QObject |
| 52 |
{ |
| 53 |
Q_OBJECT |
| 54 |
|
| 55 |
public: |
| 56 |
class ParameterObserver; |
| 57 |
static constexpr std::initializer_list<const char*> formattingOptions { |
| 58 |
QT_TR_NOOP("Operating system"), |
| 59 |
QT_TR_NOOP("Selected language"), |
| 60 |
QT_TR_NOOP("C/POSIX") |
| 61 |
}; |
| 62 |
|
| 63 |
/** @name singleton stuff */ |
| 64 |
//@{ |
| 65 |
/// Creates an instance |
| 66 |
static Translator* instance(); |
| 67 |
/// Destroys the instance |
| 68 |
static void destruct(); |
| 69 |
//@} |
| 70 |
|
| 71 |
/** Activates the specified language \a lang if available. */ |
| 72 |
void activateLanguage(const char* lang); |
| 73 |
/* Reloads the translators */ |
| 74 |
void refresh(); |
| 75 |
/** Returns the currently installed language. If no language is installed an empty string is |
| 76 |
* returned. */ |
| 77 |
std::string activeLanguage() const; |
| 78 |
/** Returns the locale (e.g. "de") to the given language name. */ |
| 79 |
std::string locale(const std::string&) const; |
| 80 |
/** Sets default Qt locale based on given language name **/ |
| 81 |
void setLocale(const std::string& = "") const; |
| 82 |
/** Returns a list of supported languages. */ |
| 83 |
TStringList supportedLanguages() const; |
| 84 |
/** Returns a map of supported languages/locales. */ |
| 85 |
TStringMap supportedLocales() const; |
| 86 |
/** Adds a path where localization files can be found */ |
| 87 |
void addPath(const QString& path); |
| 88 |
/** eventFilter used to convert decimal separator **/ |
| 89 |
bool eventFilter(QObject* obj, QEvent* ev) override; |
| 90 |
/** Enables/disables decimal separator conversion **/ |
| 91 |
void enableDecimalPointConversion(bool on); |
| 92 |
/** Returns whether decimal separator conversion is enabled */ |
| 93 |
bool isEnabledDecimalPointConversion() const; |
| 94 |
|
| 95 |
private: |
| 96 |
Translator(); |
| 97 |
~Translator() override; |
| 98 |
void removeTranslators(); |
| 99 |
QStringList directories() const; |
| 100 |
void installQMFiles(const QDir& dir, const char* locale); |
| 101 |
void updateLocaleChange() const; |
| 102 |
|
| 103 |
private: |
| 104 |
std::unique_ptr<ParameterObserver> observer; |
| 105 |
static Translator* _pcSingleton; |
| 106 |
TranslatorP* d; |
| 107 |
std::unique_ptr<Translator, std::function<void(Translator*)>> decimalPointConverter; |
| 108 |
}; |
| 109 |
|
| 110 |
} // namespace Gui |
| 111 |
|
| 112 |
#endif // GUI_TRANSLATOR_H |