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

Stellarium / stellarium / 3996069357

pending completion
3996069357

push

github

Ruslan Kabatsayev
Shorten some lines

5 of 5 new or added lines in 1 file covered. (100.0%)

14663 of 124076 relevant lines covered (11.82%)

22035.13 hits per line

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

0.0
/src/core/StelTranslator.cpp
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
#include "StelTranslator.hpp"
21
#include "StelFileMgr.hpp"
22

23
#include <QFile>
24
#include <QDebug>
25
#include <QStringList>
26
#include <QRegularExpression>
27
#include <QLocale>
28
#include <QDir>
29
#include <QTranslator>
30

31

32
// Init static members
33
QMap<QString, QString> StelTranslator::iso639codes;
34
QString StelTranslator::systemLangName;
35

36
// Use system locale language by default
37
StelTranslator* StelTranslator::globalTranslator = Q_NULLPTR;
38

39
StelTranslator::StelTranslator(const QString& adomain, const QString& alangName)
×
40
        : domain(adomain),
×
41
          langName(alangName)
×
42
{
43
        translator = new QTranslator();
×
44
        bool res = translator->load(StelFileMgr::getLocaleDir()+"/"+adomain+"/"+getTrueLocaleName()+".qm");
×
45
        if (!res)
×
46
                qWarning() << "Couldn't load translations for language " << getTrueLocaleName() << "in section" << adomain;
×
47
        if (translator->isEmpty())
×
48
                qWarning() << "Empty translation file for language " << getTrueLocaleName() << "in section" << adomain;
×
49
}
×
50

51
StelTranslator::~StelTranslator()
×
52
{
53
        delete translator;
×
54
        translator = Q_NULLPTR;
×
55
}
×
56

57
QString StelTranslator::qtranslate(const QString& s, const QString& c) const
×
58
{
59
        if (s.isEmpty())
×
60
                return "";
×
61
        QString res = translator->translate("", s.toUtf8().constData(), c.toUtf8().constData());
×
62
        if (res.isEmpty())
×
63
                return s;
×
64
        return res;
×
65
}
×
66

67
QString StelTranslator::tryQtranslate(const QString &s, const QString &c) const
×
68
{
69
        return translator->translate("", s.toUtf8().constData(),c.toUtf8().constData());
×
70
}
71
        
72
//! Initialize Translation
73
//! @param fileName file containing the list of language codes
74
void StelTranslator::init(const QString& fileName)
×
75
{
76
        StelTranslator::initSystemLanguage();
×
77
        StelTranslator::initIso639_1LanguageCodes(fileName);
×
78
        
79
        Q_ASSERT(StelTranslator::globalTranslator==Q_NULLPTR);
×
80
        StelTranslator::globalTranslator = new StelTranslator("stellarium", "system");
×
81
}
×
82

83
//! Try to determine system language from system configuration
84
void StelTranslator::initSystemLanguage()
×
85
{
86
        systemLangName = QLocale::system().name();
×
87
        if (systemLangName.isEmpty())
×
88
                systemLangName = "en";
×
89

90
        //change systemLangName to ISO 639 / ISO 3166.
91
        int pos = systemLangName.indexOf(':', 0);
×
92
        if (pos != -1) systemLangName.resize(pos);
×
93
        pos = systemLangName.indexOf('.', 0);
×
94
        if (pos != -1) systemLangName.resize(pos);
×
95
}
×
96

97

98
//! Convert from ISO639-1 2(+3) letters language code to native language name
99
QString StelTranslator::iso639_1CodeToNativeName(const QString& languageCode)
×
100
{
101
        if (languageCode=="C")
×
102
                return "English";
×
103

104
        //QLocale loc(languageCode);
105
        //QString l = loc.name();
106
        // There is a QLocale for this code.  This should be the case for most
107
        // language codes, but there are a few without QLocales, e.g. Interlingua
108
        //if (l.contains('_'))
109
        //        l.truncate(l.indexOf('_'));
110
        //if (iso639codes.find(l)!=iso639codes.end())
111
        //        return iso639codes[l]+ (languageCode.size()==2 ? "" : QString(" (")+QLocale::countryToString(loc.country())+")");
112

113
        // For codes which return the locale C, use the language code to do the lookup
114
        if (iso639codes.contains(languageCode))
×
115
                return iso639codes[languageCode];
×
116

117
        // qWarning() << "WARNING: Cannot determine name of language for code" << languageCode;
118
        return languageCode;
×
119
}
120

121
//! Convert from native language name to ISO639-1 2(+3) letters language code
122
QString StelTranslator::nativeNameToIso639_1Code(const QString& languageName)
×
123
{
124
        QMap<QString, QString>::ConstIterator iter;
×
125
        for (iter=iso639codes.constBegin();iter!=iso639codes.constEnd();++iter)
×
126
                if (iter.value() == languageName)
×
127
                        return iter.key();
×
128

129
        return languageName;
×
130
}
131

132
//! Get available native language names from directory tree
133
QStringList StelTranslator::getAvailableLanguagesNamesNative(const QString& localeDir, const QString& section) const
×
134
{
135
        QString tmpDir = localeDir;
×
136
        if (section.isEmpty() || section=="stellarium")
×
137
                tmpDir.append("/stellarium/");
×
138
        else
139
                tmpDir.append("/stellarium-" + section + "/");
×
140
        const QStringList codeList = getAvailableIso639_1Codes(tmpDir);
×
141
        QStringList output;
×
142
        for (const auto& lang : codeList)
×
143
        {
144
                output += iso639_1CodeToNativeName(lang);
×
145
        }
146
        return output;
×
147
}
×
148

149
//! Get available language codes from directory tree
150
QStringList StelTranslator::getAvailableIso639_1Codes(const QString& localeDir)
×
151
{
152
        QDir dir(localeDir);
×
153

154
        if (!dir.exists())
×
155
        {
156
                qWarning() << "Unable to find locale directory containing translations:" << QDir::toNativeSeparators(localeDir);
×
157
                return QStringList();
×
158
        }
159

160
        const QStringList entries=dir.entryList(QDir::Files, QDir::Name);
×
161
        QStringList result;
×
162
        for (auto path : entries)
×
163
        {
164
                if (!path.endsWith(".qm"))
×
165
                        continue;
×
166
                path.chop(3);
×
167
                result.append(path);
×
168
        }
×
169
        return result;
×
170
}
×
171

172
//! Initialize the languages code list from the passed file
173
//! @param fileName file containing the list of language codes
174
void StelTranslator::initIso639_1LanguageCodes(const QString& fileName)
×
175
{
176
        QFile inf(fileName);
×
177
        if (!inf.open(QIODevice::ReadOnly))
×
178
        {
179
                qWarning() << "Can't open ISO639 codes file " << QDir::toNativeSeparators(fileName);
×
180
                Q_ASSERT(0);
×
181
        }
182

183
        if (!iso639codes.empty())
×
184
                iso639codes.clear();
×
185

186
        while (!inf.atEnd())
×
187
        {
188
                QString record = QString::fromUtf8(inf.readLine());
×
189

190
                if (record.startsWith("//") || record.startsWith("#") || record.isEmpty()) // skip comments and empty lines
×
191
                        continue;
×
192

193
                static const QRegularExpression nlExp("[\\n\\r]*$");
×
194
                record.remove(nlExp); // chomp new lines
×
195
                #if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0))
196
                const QStringList& fields = record.split("\t", Qt::SkipEmptyParts);
×
197
                #else
198
                const QStringList& fields = record.split("\t", QString::SkipEmptyParts);
199
                #endif
200
                iso639codes.insert(fields.at(0), fields.at(2));
×
201
        }
×
202
}
×
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