Skip to content

Commit

Permalink
Read translatable sky culture name from description.md
Browse files Browse the repository at this point in the history
  • Loading branch information
10110111 committed Feb 2, 2024
1 parent 9624b0d commit cd8bb72
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
36 changes: 35 additions & 1 deletion src/core/StelSkyCultureMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ QString convertReferenceLinks(QString text)

}

QString StelSkyCultureMgr::getSkyCultureEnglishName(const QString& idFromJSON) const
{
const auto skyCultureId = idFromJSON;
const QString descPath = StelFileMgr::findFile("skycultures/" + skyCultureId + "/description.md");
if (descPath.isEmpty())
{
qWarning() << "WARNING: can't find description for skyculture" << skyCultureId;
return idFromJSON;
}

QFile f(descPath);
if (!f.open(QIODevice::ReadOnly))
{
qWarning().nospace() << "Failed to open sky culture description file " << descPath << ": " << f.errorString();
return idFromJSON;
}

for (int lineNum = 1;; ++lineNum)
{
const auto line = QString::fromUtf8(f.readLine()).trimmed();
if (line.isEmpty()) continue;
if (!line.startsWith("#"))
{
qWarning().nospace() << "Sky culture description file " << descPath << " at line "
<< lineNum << " has wrong format (expected a top-level header, got " << line;
return idFromJSON;
}
return line.mid(1).trimmed();
}

qWarning() << "Failed to find sky culture name in" << descPath;
return idFromJSON;
}

StelSkyCultureMgr::StelSkyCultureMgr()
{
setObjectName("StelSkyCultureMgr");
Expand Down Expand Up @@ -114,7 +148,7 @@ StelSkyCultureMgr::StelSkyCultureMgr()
const auto data = jsonDoc.object();

auto& culture = dirToNameEnglish[dir];
culture.englishName = data["id"].toString();
culture.englishName = getSkyCultureEnglishName(data["id"].toString());
culture.region = data["region"].toString();
if (data["constellations"].isArray())
{
Expand Down
3 changes: 3 additions & 0 deletions src/core/StelSkyCultureMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ public slots:
void currentSkyCultureChanged(const StelSkyCulture& culture);

private:
//! Read the English name of the sky culture from description file.
//! @param idFromJSON the id from \p index.json that will be used as a default name if an error occurs.
QString getSkyCultureEnglishName(const QString& idFromJSON) const;
//! Get the culture name in English associated with a specified directory.
//! @param directory The directory name.
//! @return The English name for the culture associated with directory.
Expand Down
2 changes: 1 addition & 1 deletion src/tests/testStelSkyCultureMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void TestStelSkyCultureMgr::testStelSkyCultureMgr()
QVERIFY(scMgr.getCurrentSkyCultureID()=="western");
QVERIFY(scMgr.setCurrentSkyCultureID("blackfoot"));
QVERIFY(scMgr.getCurrentSkyCultureID()=="blackfoot");
QVERIFY(scMgr.getCurrentSkyCultureEnglishName()=="blackfoot");
QVERIFY(scMgr.getCurrentSkyCultureEnglishName()=="Blackfoot");
QVERIFY(scMgr.getCurrentSkyCultureBoundariesType()==StelSkyCulture::BoundariesType::None);
QVERIFY(scMgr.getCurrentSkyCultureClassificationIdx()==StelSkyCulture::TRADITIONAL);
QVERIFY(scMgr.getSkyCultureListEnglish().contains("western", Qt::CaseInsensitive));
Expand Down

0 comments on commit cd8bb72

Please sign in to comment.