Skip to content

Commit

Permalink
Add support for Chinese translations of Chinese cultures
Browse files Browse the repository at this point in the history
This leaves out " Added" suffixes, which makes corresponding names
untranslated yet.
Also, this doesn't handle Chinese cultures in non-Chinese locales.
  • Loading branch information
10110111 committed Mar 11, 2024
1 parent e5d9a75 commit e671b66
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/core/StelTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@
#include <QDir>
#include <QTranslator>

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<QString, QString> StelTranslator::iso639codes;
Expand Down Expand Up @@ -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());
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/StelTranslator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e671b66

Please sign in to comment.