• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Stellarium / stellarium / 13260145531

11 Feb 2025 09:41AM UTC coverage: 12.127% (+0.03%) from 12.101%
13260145531

Pull #3751

github

10110111
Restore deleted additional SC files
Pull Request #3751: Switch skycultures to the new format

14613 of 120497 relevant lines covered (12.13%)

18620.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/core/StelTranslator.hpp
1
/*
2
* Stellarium
3
* Copyright (C) 2005 Fabien Chereau
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 program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
18
*/
19

20
#ifndef STELTRANSLATOR_HPP
21
#define STELTRANSLATOR_HPP
22

23
//! @file StelTranslator.hpp
24
//! Define some translation macros.
25

26
#include <QMap>
27
#include <QString>
28

29
//! @def q_(str)
30
//! Return the gettext translated english text @a str using the current global translator.
31
//! The returned value is a localized QString.
32
#define q_(str) StelTranslator::globalTranslator->qtranslate(str)
33

34
//! @def qc_(str, ctxt)
35
//! Return the gettext translated english text @a str in context @b ctxt using the current global translator.
36
//! The returned value is a localized QString.
37
#define qc_(str, ctxt) StelTranslator::globalTranslator->qtranslate(str, ctxt)
38

39
//! @def N_(str)
40
//! A pseudo function call that serves as a marker for the automated extraction of messages.
41
//! A call to N_() doesn't translate.
42
#define N_(str) str
43

44
//! @def NC_(str, ctxt)
45
//! A pseudo function call that serves as a marker for the automated extraction of messages in some context.
46
//! A call to NC_() doesn't translate.
47
#define NC_(str, ctxt) str
48

49
//! Class used to translate strings to any language.
50
//! Implements a nice interface to gettext which is UTF-8 compliant and is somewhat multiplateform
51
//! All its operations do not modify the global locale.
52
//! The purpose of this class is to remove all non-OO C locale functions from stellarium.
53
//! @author Fabien Chereau
54
class StelTranslator
55
{
56
public:
57
        //! Create a translator from a language name.
58
        //! If the passed locale name cannot be handled by the system, default value will be used.
59
        //! The passed language name is a language code string like "fr" or "fr_FR".
60
        //! This class wrap gettext to simulate an object oriented multiplateform gettext UTF8 translator
61
        //! @param adomain The name of the domain to use for translation
62
        //! @param amoDirectory The directory where to look for the domain.mo translation files.
63
        //! @param alangName The C locale name or language name like "fr" or "fr_FR". If string is "" or "system" it will use the system locale.
64
        StelTranslator(const QString& adomain, const QString& alangName);
65
        
66
        virtual ~StelTranslator();
67

68
        //! Checks whether any translations have been loaded
69
        virtual bool isEmpty() const;
70
        
71
        //! Translate input message and return it as a QString.
72
        //! If the string is not translated in the current locale, the input string is returned unchanged.
73
        //! @param s input string in english.
74
        //! @param c disambiguation string (gettext "context" string).
75
        //! @return The translated QString
76
        QString qtranslate(const QString& s, const QString& c = QString()) const;
77

78
        //! Same as #qtranslate, but with additional code to handle Chinese names of stars
79
        QString qTranslateStar(const QString& s, const QString& c = QString()) const;
80

81
        //! Try to translate input message and return it as a QString. If no translation
82
        //! exist for the current StelTranslator language, a null string is returned.
83
        //! @param s input string in english.
84
        //! @param c disambiguation string (gettext "context" string).
85
        //! @return The translated QString
86
        virtual QString tryQtranslate(const QString& s, const QString& c = QString()) const;
87

88
        //! Same as #tryQtranslate, but with additional code to handle Chinese names of stars
89
        QString tryQtranslateStar(const QString& s, const QString& c = QString()) const;
90
        
91
        //! Get true translator locale name. Actual locale, never "system".
92
        //! @return Locale name e.g "fr_FR"
93
        const QString& getTrueLocaleName() const
×
94
        {
95
                if (langName=="system" || langName=="system_default")
×
96
                        return StelTranslator::systemLangName;
×
97
                else
98
                        return langName;
×
99
        }
100

101
        //! Used as a global translator by the whole app
102
        static StelTranslator* globalTranslator;
103

104
        //! Get available language name in native language from passed locales directory
105
        QStringList getAvailableLanguagesNamesNative(const QString& localeDir="", const QString &section="") const;
106

107
        //! Convert from ISO639-1 language code to native language name
108
        //! @param languageCode the code to look up
109
        static QString iso639_1CodeToNativeName(const QString& languageCode);
110
        
111
        //! Convert from native language name to ISO639-1 2 letters language code 
112
        static QString nativeNameToIso639_1Code(const QString& languageName);
113
        
114
        //! Initialize Translation
115
        //! @param fileName file containing the list of language codes
116
        static void init(const QString& fileName);
117
        
118
protected:
119
        StelTranslator() = default;
120

121
        //! The domain name
122
        QString domain;
123

124
        //! The two letter string defining the current language name
125
        QString langName;
126

127
        //! QTranslator instance
128
        class QTranslator* translator = nullptr;
129

130
private:
131
        StelTranslator(const StelTranslator& );
132
        const StelTranslator& operator=(const StelTranslator&);
133

134
        QString tryTranslateChineseStar(const QString& s, const QString& c) const;
135
        
136
        //! Initialize the languages code list from the passed file
137
        //! @param fileName file containing the list of language codes
138
        static void initIso639_1LanguageCodes(const QString& fileName);
139
        
140
        //! Get available language codes from passed locales directory
141
        static QStringList getAvailableIso639_1Codes(const QString& localeDir="");
142

143
        //! Try to determine system language from system configuration
144
        static void initSystemLanguage(void);
145
        
146
        //! Store the system default language name as taken from LANGUAGE environment variable
147
        static QString systemLangName;
148
        
149
        //! Contains the list of all iso639 languages codes
150
        static QMap<QString, QString> iso639codes;
151
};
152

153
class StelSkyTranslator : public StelTranslator
154
{
155
public:
156
        StelSkyTranslator(const QString& langName);
157
        QString tryQtranslate(const QString& s, const QString& c = QString()) const override;
158
        bool isEmpty() const override;
159
private:
160
        //! Used as a skyculture-independent fallback when current sky culture doesn't have a translation
161
        StelTranslator commonSkyTranslator;
162
};
163

164
#endif // STELTRANSLATOR_HPP
165

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc