diff --git a/src/core/StelTranslator.cpp b/src/core/StelTranslator.cpp index 3bf4348ab12586..fe0340697ede62 100644 --- a/src/core/StelTranslator.cpp +++ b/src/core/StelTranslator.cpp @@ -28,6 +28,37 @@ #include #include +namespace +{ +int parseRomanNumeral(const QStringView& roman) +{ + const auto romanLat = roman.toLatin1(); + int v = 0; + const char* p = romanLat.data(); + + /**/ if (strncmp(p, "XC", 2) == 0) { v += 90; p += 2; } + else if (strncmp(p, "LXXX", 4) == 0) { v += 80; p += 4; } + else if (strncmp(p, "LXX", 3) == 0) { v += 70; p += 3; } + else if (strncmp(p, "LX", 2) == 0) { v += 60; p += 2; } + else if (strncmp(p, "L", 1) == 0) { v += 50; p += 1; } + else if (strncmp(p, "XL", 2) == 0) { v += 40; p += 2; } + else if (strncmp(p, "XXX", 3) == 0) { v += 30; p += 3; } + else if (strncmp(p, "XX", 2) == 0) { v += 20; p += 2; } + else if (strncmp(p, "X", 1) == 0) { v += 10; p += 1; } + + /**/ if (strncmp(p, "IX", 2) == 0) { v += 9; p += 2; } + else if (strncmp(p, "VIII", 4) == 0) { v += 8; p += 4; } + else if (strncmp(p, "VII", 3) == 0) { v += 7; p += 3; } + else if (strncmp(p, "VI", 2) == 0) { v += 6; p += 2; } + else if (strncmp(p, "V", 1) == 0) { v += 5; p += 1; } + else if (strncmp(p, "IV", 2) == 0) { v += 4; p += 2; } + else if (strncmp(p, "III", 3) == 0) { v += 3; p += 3; } + else if (strncmp(p, "II", 2) == 0) { v += 2; p += 2; } + else if (strncmp(p, "I", 1) == 0) { v += 1; p += 1; } + + return v; +} +} // Init static members QMap StelTranslator::iso639codes; @@ -70,8 +101,57 @@ QString StelTranslator::qtranslate(const QString& s, const QString& c) const return res; } +QString StelTranslator::tryTranslateChineseStar(const QString& s, const QString& c) const +{ + static const auto re = []{ QRegularExpression re("(.+) ([IXVLCDM]+)$"); re.optimize(); return re; }(); + const auto match = re.match(s); + if (!match.hasMatch()) return {}; + + const auto translatedConstellation = tryQtranslate(match.captured(1), c); + if (translatedConstellation.isEmpty()) return {}; + + const auto num = parseRomanNumeral(match.captured(2)); + Q_ASSERT(num < 100); + + static const QChar chars[10] = { + u'十', + u'一', + u'二', + u'三', + u'四', + u'五', + u'六', + u'七', + u'八', + u'九', + }; + + QString result = translatedConstellation; + int tens = num / 10; + const int units = num % 10; + if (tens >= 2) + { + result += chars[tens]; + tens = 1; + } + if (tens == 1) + { + result += chars[0]; + } + if (units) + { + result += chars[units]; + } + return result; +} + QString StelTranslator::tryQtranslate(const QString &s, const QString &c) const { + if (getTrueLocaleName().startsWith("zh")) + { + const auto translated = tryTranslateChineseStar(s, c); + if (!translated.isEmpty()) return translated; + } return translator->translate("", s.toUtf8().constData(),c.toUtf8().constData()); } diff --git a/src/core/StelTranslator.hpp b/src/core/StelTranslator.hpp index 8aefaab6b4b950..0ada50439b27d7 100644 --- a/src/core/StelTranslator.hpp +++ b/src/core/StelTranslator.hpp @@ -124,6 +124,8 @@ class StelTranslator private: StelTranslator(const StelTranslator& ); const StelTranslator& operator=(const StelTranslator&); + + QString tryTranslateChineseStar(const QString& s, const QString& c) const; //! Initialize the languages code list from the passed file //! @param fileName file containing the list of language codes