File size: 4,472 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/

#ifndef GUI_TRANSLATOR_H
#define GUI_TRANSLATOR_H

#include <QObject>
#include <list>
#include <map>
#include <memory>
#include <string>
#include <FCGlobal.h>


class QDir;

namespace Gui
{

using TStringList = std::list<std::string>;
using TStringMap = std::map<std::string, std::string>;

/**

 * The Translator class uses Qt's QTranslator objects to change the language of the application

 * on the fly.

 * For more details see the \link i18n.html Internationalization with FreeCAD \endlink

 * documentation.

 *

 * \author Werner Mayer

 */
class TranslatorP;
class GuiExport Translator: public QObject
{
    Q_OBJECT

public:
    class ParameterObserver;
    static constexpr std::initializer_list<const char*> formattingOptions {
        QT_TR_NOOP("Operating system"),
        QT_TR_NOOP("Selected language"),
        QT_TR_NOOP("C/POSIX")
    };

    /** @name singleton stuff */
    //@{
    /// Creates an instance
    static Translator* instance();
    /// Destroys the instance
    static void destruct();
    //@}

    /** Activates the specified language \a lang if available. */
    void activateLanguage(const char* lang);
    /* Reloads the translators */
    void refresh();
    /** Returns the currently installed language. If no language is installed an empty string is

     * returned. */
    std::string activeLanguage() const;
    /** Returns the locale (e.g. "de") to the given language name. */
    std::string locale(const std::string&) const;
    /** Sets default Qt locale based on given language name **/
    void setLocale(const std::string& = "") const;
    /** Returns a list of supported languages. */
    TStringList supportedLanguages() const;
    /** Returns a map of supported languages/locales. */
    TStringMap supportedLocales() const;
    /** Adds a path where localization files can be found */
    void addPath(const QString& path);
    /** eventFilter used to convert decimal separator **/
    bool eventFilter(QObject* obj, QEvent* ev) override;
    /** Enables/disables decimal separator conversion **/
    void enableDecimalPointConversion(bool on);
    /** Returns whether decimal separator conversion is enabled */
    bool isEnabledDecimalPointConversion() const;

private:
    Translator();
    ~Translator() override;
    void removeTranslators();
    QStringList directories() const;
    void installQMFiles(const QDir& dir, const char* locale);
    void updateLocaleChange() const;

private:
    std::unique_ptr<ParameterObserver> observer;
    static Translator* _pcSingleton;
    TranslatorP* d;
    std::unique_ptr<Translator, std::function<void(Translator*)>> decimalPointConverter;
};

}  // namespace Gui

#endif  // GUI_TRANSLATOR_H