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

Stellarium / stellarium / 17068063291

19 Aug 2025 11:22AM UTC coverage: 11.766%. Remained the same
17068063291

push

github

alex-w
Reformatting

14706 of 124990 relevant lines covered (11.77%)

18303.49 hits per line

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

0.0
/src/core/StelLocaleMgr.cpp
1
/*
2
 * Stellarium
3
 * Copyright (C) 2006 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 "StelLocaleMgr.hpp"
21
#include "StelApp.hpp"
22
#include "StelUtils.hpp"
23
#include "StelSkyCultureMgr.hpp"
24

25
#include <QLocale>
26
#include <QDebug>
27
#include <QSettings>
28
#include <QString>
29
#include <QTextStream>
30
#include <QFile>
31

32
#include <ctime>
33

34
StelLocaleMgr::StelLocaleMgr()
×
35
        : timeFormat()
×
36
        , dateFormat()
×
37
{
38
        core = StelApp::getInstance().getCore();
×
39
        createNameLists();
×
40
}
×
41

42

43
StelLocaleMgr::~StelLocaleMgr()
×
44
{
45
}
×
46

47
void StelLocaleMgr::init()
×
48
{
49
        QSettings* conf = StelApp::getInstance().getSettings();
×
50
        Q_ASSERT(conf);
×
51

52
        setAppLanguage(conf->value("localization/app_locale", "system").toString(), false);
×
53

54
        timeFormat = stringToSTimeFormat(conf->value("localization/time_display_format", "system_default").toString());
×
55
        dateFormat = stringToSDateFormat(conf->value("localization/date_display_format", "system_default").toString());
×
56
}
×
57

58
/*************************************************************************
59
 Set the application locale. This apply to GUI, console messages etc..
60
*************************************************************************/
61
void StelLocaleMgr::setAppLanguage(const QString& newAppLanguageName, bool refreshAll)
×
62
{
63
        // Update the translator with new locale name
64
        Q_ASSERT(StelTranslator::globalTranslator);
×
65
        delete StelTranslator::globalTranslator;
×
66
        StelTranslator::globalTranslator = new StelTranslator("stellarium", newAppLanguageName);
×
67
        qInfo().noquote() << "Application language:" << StelTranslator::globalTranslator->getTrueLocaleName();
×
68

69
        // Update the translator with new locale name
70
        skyTranslator.reset(new StelSkyTranslator(newAppLanguageName));
×
71
        qInfo().noquote() << "Sky language:" << skyTranslator->getTrueLocaleName();
×
72

73
        // Update the translator with new locale name
74
        planetaryFeaturesTranslator.reset(new StelTranslator("stellarium-planetary-features", newAppLanguageName));
×
75
        qInfo().noquote() << "Planetary features language:" << planetaryFeaturesTranslator->getTrueLocaleName();
×
76

77
        // Update the translator with new locale name
78
        scriptsTranslator.reset(new StelTranslator("stellarium-scripts", newAppLanguageName));
×
79
        qInfo().noquote() << "Scripts language:" << scriptsTranslator->getTrueLocaleName();
×
80

81
        // Update the translator with new locale name
82
        skyCultureDescriptionsTranslator.reset(new StelTranslator("stellarium-skycultures-descriptions", newAppLanguageName));
×
83
        qInfo().noquote() << "Sky culture description language:" << skyCultureDescriptionsTranslator->getTrueLocaleName();
×
84

85
        createNameLists();
×
86
        if (refreshAll)
×
87
                StelApp::getInstance().updateI18n();
×
88
}
×
89

90
bool StelLocaleMgr::isAppRTL() const
×
91
{
92
        return QString("ar fa ckb ug ur he yi").contains(getAppLanguage());
×
93
}
94

95
/*************************************************************************
96
 Get the language currently used for sky objects..
97
*************************************************************************/
98
QString StelLocaleMgr::getSkyLanguage() const
×
99
{
100
        return skyTranslator->getTrueLocaleName();
×
101
}
102

103
bool StelLocaleMgr::isSkyRTL() const
×
104
{
105
        return QString("ar fa ckb ug ur he yi").contains(getSkyLanguage());
×
106
}
107

108
// Get the StelTranslator currently used for sky objects.
109
const StelTranslator& StelLocaleMgr::getSkyTranslator() const
×
110
{
111
        return *skyTranslator;
×
112
}
113

114
const StelTranslator& StelLocaleMgr::getPlanetaryFeaturesTranslator() const
×
115
{
116
        return *planetaryFeaturesTranslator;
×
117
}
118

119
const StelTranslator &StelLocaleMgr::getAppStelTranslator() const
×
120
{
121
        return *StelTranslator::globalTranslator;
×
122
}
123

124
const StelTranslator& StelLocaleMgr::getScriptsTranslator() const
×
125
{
126
        return *scriptsTranslator;
×
127
}
128

129
const StelTranslator& StelLocaleMgr::getSkyCultureDescriptionsTranslator() const
×
130
{
131
        return *skyCultureDescriptionsTranslator;
×
132
}
133

134
// Return the time in ISO 8601 format that is : %Y-%m-%d %H:%M:%S
135
QString StelLocaleMgr::getISO8601TimeLocal(double JD, double utcOffsetHrs) const
×
136
{
137
        return StelUtils::julianDayToISO8601String(JD + utcOffsetHrs*StelCore::JD_HOUR);
×
138
}
139

140
//! get the six ints from an ISO8601 date time, understood to be local time, make a jdate out
141
//! of them.
142
double StelLocaleMgr::getJdFromISO8601TimeLocal(const QString& t, bool* ok) const
×
143
{
144
        double jd = StelUtils::getJulianDayFromISO8601String(t, ok);
×
145
        if (!*ok)
×
146
        {
147
                qWarning() << "StelLocaleMgr::getJdFromISO8601TimeLocal: invalid ISO8601 date. Returning JD=0";
×
148
                return 0.0;
×
149
        }
150

151
        jd -= core->getUTCOffset(jd)*StelCore::JD_HOUR;
×
152
        return jd;
×
153
}
154

155

156
// Return a string with the local date formatted according to the dateFormat variable
157
QString StelLocaleMgr::getPrintableDateLocal(double JD, double utcOffsetHrs) const
×
158
{
159
        int year, month, day, dayOfWeek;
160
        const double shift = utcOffsetHrs*StelCore::JD_HOUR;
×
161
        StelUtils::getDateFromJulianDay(JD+shift, &year, &month, &day);
×
162
        dayOfWeek = StelUtils::getDayOfWeek(year, month, day);
×
163
        QString str;
×
164
        switch (dateFormat)
×
165
        {
166
                case SDateMMDDYYYY:
×
167
                        str = QString("%1-%2-%3").arg(month,2,10,QLatin1Char('0')).arg(day,2,10,QLatin1Char('0')).arg(year,4,10);
×
168
                        break;
×
169
                case SDateDDMMYYYY:
×
170
                        str = QString("%1-%2-%3").arg(day,2,10,QLatin1Char('0')).arg(month,2,10,QLatin1Char('0')).arg(year,4,10);
×
171
                        break;
×
172
                case SDateYYYYMMDD:
×
173
                        str = QString("%1-%2-%3").arg(year,4,10).arg(month,2,10,QLatin1Char('0')).arg(day,2,10,QLatin1Char('0'));
×
174
                        break;
×
175
                case SDateWWMMDDYYYY:
×
176
                        str = QString("%1, %2-%3-%4").arg(shortDayName(dayOfWeek)).arg(month,2,10,QLatin1Char('0')).arg(day,2,10,QLatin1Char('0')).arg(year,4,10);
×
177
                        break;
×
178
                case SDateWWDDMMYYYY:
×
179
                        str = QString("%1, %2-%3-%4").arg(shortDayName(dayOfWeek)).arg(day,2,10,QLatin1Char('0')).arg(month,2,10,QLatin1Char('0')).arg(year,4,10);
×
180
                        break;
×
181
                case SDateWWYYYYMMDD:
×
182
                        str = QString("%1, %2-%3-%4").arg(shortDayName(dayOfWeek)).arg(year,4,10).arg(month,2,10,QLatin1Char('0')).arg(day,2,10,QLatin1Char('0'));
×
183
                        break;
×
184
                case SDateSystemDefault:
×
185
                        str = StelUtils::localeDateString(year, month, day, dayOfWeek);
×
186
                        break;
×
187
                default:
×
188
                        qWarning() << "Unknown date format fallback to system default";
×
189
                        str = StelUtils::localeDateString(year, month, day, dayOfWeek);
×
190
        }
191
        return str;
×
192
}
×
193

194
// Return a string with the local time (according to timeZoneMode variable) formatted
195
// according to the timeFormat variable
196
QString StelLocaleMgr::getPrintableTimeLocal(double JD, double utcOffsetHrs) const
×
197
{
198
        int hour, minute, second, millsec;
199
        const double shift = utcOffsetHrs*StelCore::JD_HOUR;
×
200
        StelUtils::getTimeFromJulianDay(JD+shift, &hour, &minute, &second, &millsec);
×
201
        QTime t(hour, minute, second, millsec);
×
202
        switch (timeFormat)
×
203
        {
204
                case STimeSystemDefault:
×
205
                        return t.toString();
×
206
                case STime24h:
×
207
                        return t.toString("hh:mm:ss");
×
208
                case STime12h:
×
209
                        return t.toString("hh:mm:ss AP");
×
210
                default:
×
211
                        qWarning() << "Unknown time format, fallback to system default";
×
212

213
#if (QT_VERSION>=QT_VERSION_CHECK(5,14,0))
214
                        return t.toString(QLocale().dateFormat(QLocale::ShortFormat));
×
215
#else
216
                        return t.toString(Qt::DefaultLocaleShortDate);
217
#endif
218
        }
219
}
220

221
QString StelLocaleMgr::getPrintableTimeZoneLocal(double JD, double utcOffsetHrs) const
×
222
{
223
        QString timeZone = "";
×
224
        QString timeZoneST = "";
×
225
        if (core->getCurrentLocation().planetName=="Earth")
×
226
        {
227
                QString currTZ = core->getCurrentTimeZone();
×
228

229
                if (JD<=StelCore::TZ_ERA_BEGINNING || currTZ.contains("auto") || currTZ.contains("LMST"))
×
230
                {
231
                        // TRANSLATORS: Local Mean Solar Time. Please use abbreviation.
232
                        timeZoneST = qc_("LMST", "solar time");
×
233
                }
234

235
                if (currTZ.contains("LTST"))
×
236
                {
237
                        // TRANSLATORS: Local True Solar Time. Please use abbreviation.
238
                        timeZoneST = qc_("LTST", "solar time");
×
239
                }
240

241
                const double shift = utcOffsetHrs;
×
242
                QTime tz = QTime(0, 0, 0).addSecs(static_cast<int>(3600*qAbs(shift)));
×
243
                if(shift<0.0)
×
244
                        timeZone = QString("UTC-%1").arg(tz.toString("hh:mm"));
×
245
                else
246
                        timeZone = QString("UTC+%1").arg(tz.toString("hh:mm"));
×
247

248
                if (!timeZoneST.isEmpty() && !core->getUseCustomTimeZone())
×
249
                        timeZone = QString("%1 (%2)").arg(timeZone, timeZoneST);
×
250
        }
×
251
        else
252
        {
253
                // TODO: Make sure LMST/LTST would make sense on other planet, or inhibit it?
254
                const double shift = utcOffsetHrs;
×
255
                QTime tz = QTime(0, 0, 0).addSecs(static_cast<int>(3600*qAbs(shift)));
×
256
                if(shift<0.0)
×
257
                        timeZone = QString("UTC-%1").arg(tz.toString("hh:mm"));
×
258
                else
259
                        timeZone = QString("UTC+%1").arg(tz.toString("hh:mm"));
×
260

261
                //if (!timeZoneST.isEmpty() && !core->getUseCustomTimeZone())
262
                //        timeZone = QString("%1 (%2)").arg(timeZone, timeZoneST);
263
        }
264
        return timeZone;
×
265
}
×
266

267
// Convert the time format enum to its associated string and reverse
268
StelLocaleMgr::STimeFormat StelLocaleMgr::stringToSTimeFormat(const QString& tf)
×
269
{
270
        static const QMap<QString, StelLocaleMgr::STimeFormat> map = {
271
                {"system_default", STimeSystemDefault},
×
272
                {"24h", STime24h},
×
273
                {"12h", STime12h}};
×
274
        if (!map.contains(tf))
×
275
                qWarning() << "Unrecognized time_display_format : " << tf << " system_default used.";
×
276
        return map.value(tf, STimeSystemDefault);
×
277
}
×
278

279
QString StelLocaleMgr::sTimeFormatToString(STimeFormat tf)
×
280
{
281
        static const QStringList tfmt={"system_default", "24h", "12h"};
×
282
        return tfmt[tf];
×
283
}
×
284

285
// Convert the date format enum to its associated string and reverse
286
StelLocaleMgr::SDateFormat StelLocaleMgr::stringToSDateFormat(const QString& df)
×
287
{
288
        static const QMap<QString, StelLocaleMgr::SDateFormat> map = {
289
                {"system_default", SDateSystemDefault},
×
290
                {"mmddyyyy",       SDateMMDDYYYY},
×
291
                {"ddmmyyyy",       SDateDDMMYYYY},
×
292
                {"yyyymmdd",       SDateYYYYMMDD}, // iso8601
×
293
                {"wwmmddyyyy",     SDateWWMMDDYYYY},
×
294
                {"wwddmmyyyy",     SDateWWDDMMYYYY},
×
295
                {"wwyyyymmdd",     SDateWWYYYYMMDD}};
×
296
        if (!map.contains(df))
×
297
                qWarning() << "Unrecognized date_display_format : " << df << " system_default used.";
×
298
        return map.value(df, SDateSystemDefault);
×
299
}
×
300

301
QString StelLocaleMgr::sDateFormatToString(SDateFormat df)
×
302
{
303
        QStringList dfmt = {
304
                "system_default",
305
                "mmddyyyy",
306
                "ddmmyyyy",
307
                "yyyymmdd",
308
                "wwmmddyyyy",
309
                "wwddmmyyyy",
310
                "wwyyyymmdd"};
×
311
        return dfmt[df];
×
312
}
×
313

314
QString StelLocaleMgr::getQtDateFormatStr() const
×
315
{
316
        QStringList dfmt = {
317
                "yyyy.MM.dd",
318
                "MM.dd.yyyy",
319
                "dd.MM.yyyy",
320
                "yyyy.MM.dd",
321
                "ddd, MM.dd.yyyy",
322
                "ddd, dd.MM.yyyy",
323
                "ddd, yyyy.MM.dd"};
×
324
        return dfmt[dateFormat];
×
325
}
×
326

327
QString StelLocaleMgr::shortDayName(int weekday)
×
328
{
329
        Q_ASSERT(weekday>=0);
×
330
        Q_ASSERT(shortWeekDays.length()==7);
×
331
        return shortWeekDays[weekday % 7];
×
332
}
333

334
QString StelLocaleMgr::longDayName(int weekday)
×
335
{
336
        Q_ASSERT(weekday>=0);
×
337
        Q_ASSERT(longWeekDays.length()==7);
×
338
        return longWeekDays[weekday % 7];
×
339
}
340

341
QString StelLocaleMgr::shortMonthName(int month)
×
342
{
343
        Q_ASSERT(month >= 0);
×
344
        Q_ASSERT(shortMonthNames.length()==12);
×
345
        return shortMonthNames[month % 12];
×
346
}
347

348
QString StelLocaleMgr::longMonthName(int month)
×
349
{
350
        Q_ASSERT(month >= 0);
×
351
        Q_ASSERT(longMonthNames.length()==12);
×
352
        return longMonthNames[month % 12];
×
353
}
354

355
QString StelLocaleMgr::longGenitiveMonthName(int month)
×
356
{
357
        Q_ASSERT(month >= 0);
×
358
        Q_ASSERT(longGenitiveMonthNames.length()==12);
×
359
        return longGenitiveMonthNames[month % 12];
×
360
}
361

362
QString StelLocaleMgr::romanMonthName(int month)
×
363
{
364
        Q_ASSERT(month >= 0);
×
365
        static const QStringList romanMonths = { "XII", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI"};
×
366
        return romanMonths[month % 12];
×
367
}
×
368

369

370
void StelLocaleMgr::createNameLists()
×
371
{
372
        shortWeekDays.clear();
×
373
        shortWeekDays
374
                << qc_("Sun", "short day name")
×
375
                << qc_("Mon", "short day name")
×
376
                << qc_("Tue", "short day name")
×
377
                << qc_("Wed", "short day name")
×
378
                << qc_("Thu", "short day name")
×
379
                << qc_("Fri", "short day name")
×
380
                << qc_("Sat", "short day name");
×
381
        longWeekDays.clear();
×
382
        longWeekDays
383
                << qc_("Sunday",    "long day name")
×
384
                << qc_("Monday",    "long day name")
×
385
                << qc_("Tuesday",   "long day name")
×
386
                << qc_("Wednesday", "long day name")
×
387
                << qc_("Thursday",  "long day name")
×
388
                << qc_("Friday",    "long day name")
×
389
                << qc_("Saturday",  "long day name");
×
390

391
        shortMonthNames.clear();
×
392
        shortMonthNames
393
                << qc_("Dec", "short month name")
×
394
                << qc_("Jan", "short month name")
×
395
                << qc_("Feb", "short month name")
×
396
                << qc_("Mar", "short month name")
×
397
                << qc_("Apr", "short month name")
×
398
                << qc_("May", "short month name")
×
399
                << qc_("Jun", "short month name")
×
400
                << qc_("Jul", "short month name")
×
401
                << qc_("Aug", "short month name")
×
402
                << qc_("Sep", "short month name")
×
403
                << qc_("Oct", "short month name")
×
404
                << qc_("Nov", "short month name");
×
405

406
        longMonthNames.clear();
×
407
        longMonthNames
408
                << qc_("December",  "long month name")
×
409
                << qc_("January",   "long month name")
×
410
                << qc_("February",  "long month name")
×
411
                << qc_("March",     "long month name")
×
412
                << qc_("April",     "long month name")
×
413
                << qc_("May",       "long month name")
×
414
                << qc_("June",      "long month name")
×
415
                << qc_("July",      "long month name")
×
416
                << qc_("August",    "long month name")
×
417
                << qc_("September", "long month name")
×
418
                << qc_("October",   "long month name")
×
419
                << qc_("November",  "long month name");
×
420
        longGenitiveMonthNames.clear();
×
421
        longGenitiveMonthNames
422
                << qc_("December",  "genitive")
×
423
                << qc_("January",   "genitive")
×
424
                << qc_("February",  "genitive")
×
425
                << qc_("March",     "genitive")
×
426
                << qc_("April",     "genitive")
×
427
                << qc_("May",       "genitive")
×
428
                << qc_("June",      "genitive")
×
429
                << qc_("July",      "genitive")
×
430
                << qc_("August",    "genitive")
×
431
                << qc_("September", "genitive")
×
432
                << qc_("October",   "genitive")
×
433
                << qc_("November",  "genitive");
×
434
}
×
435

436
QStringList StelLocaleMgr::shortWeekDays;
437
QStringList StelLocaleMgr::longWeekDays;
438
QStringList StelLocaleMgr::shortMonthNames;
439
QStringList StelLocaleMgr::longMonthNames;
440
QStringList StelLocaleMgr::longGenitiveMonthNames;
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