changeset 322:c32467cd06bb

core/strings: add Strings::Translate function as tr() -> ToUtf8String
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 22:15:53 -0400
parents 8141f409d52c
children 1686fac290c5
files include/core/strings.h src/core/strings.cc src/gui/translate/anime.cc src/gui/translate/config.cc
diffstat 4 files changed, 47 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/include/core/strings.h	Wed Jun 12 20:42:44 2024 -0400
+++ b/include/core/strings.h	Wed Jun 12 22:15:53 2024 -0400
@@ -21,9 +21,6 @@
 
 /* Substring removal functions */
 void ReplaceAll(std::string& string, std::string_view find, std::string_view replace);
-void SanitizeLineEndings(std::string& string);
-void RemoveHtmlTags(std::string& string);
-void ParseHtmlEntities(std::string& string);
 void NormalizeUnicode(std::string& string);
 void NormalizeAnimeTitle(std::string& string);
 
@@ -73,6 +70,8 @@
 
 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub);
 
+std::string Translate(const char* str);
+
 }; // namespace Strings
 
 #endif // MINORI_CORE_STRINGS_H_
--- a/src/core/strings.cc	Wed Jun 12 20:42:44 2024 -0400
+++ b/src/core/strings.cc	Wed Jun 12 22:15:53 2024 -0400
@@ -9,6 +9,7 @@
 #include <QLocale>
 #include <QString>
 #include <QTextDocument>
+#include <QCoreApplication>
 
 #include <algorithm>
 #include <cctype>
@@ -67,15 +68,6 @@
 	}
 }
 
-void SanitizeLineEndings(std::string& string) {
-	/* LOL */
-	ReplaceAll(string, "\r\n",   "\n");
-	ReplaceAll(string, "</p>",   "\n");
-	ReplaceAll(string, "<br>",   "\n");
-	ReplaceAll(string, "<br />", "\n");
-	ReplaceAll(string, "\n\n\n", "\n\n");
-}
-
 void ConvertRomanNumerals(std::string& string) {
 	static const std::vector<std::pair<std::string_view, std::string_view>> vec = {
 		{"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"},
@@ -119,55 +111,11 @@
 	RemoveTrailingChars(string, ' ');
 }
 
-/* removes dumb HTML tags because anilist is aids and
- * gives us HTML for synopses :/
- */
-void RemoveHtmlTags(std::string& string) {
-	while (string.find("<") != std::string::npos) {
-		auto startpos = string.find("<");
-		auto endpos = string.find(">") + 1;
-
-		if (endpos != std::string::npos)
-			string.erase(startpos, endpos - startpos);
-	}
-}
-
-/* e.g. "&lt;" for "<" */
-void ParseHtmlEntities(std::string& string) {
-	const std::unordered_map<std::string, std::string> map = {
-	    {"&lt;",    "<"},
-        {"&rt;",    ">"},
-        {"&nbsp;",  "\xA0"},
-        {"&amp;",   "&"},
-        {"&quot;",  "\""},
-	    {"&apos;",  "'"},
-        {"&cent;",  "¢"},
-        {"&pound;", "£"},
-        {"&euro;",  "€"},
-        {"&yen;",   "¥"},
-	    {"&copy;",  "©"},
-        {"&reg;",   "®"},
-        {"&times;", "×"},
-        {"&rsquo;", "’" }  // Haibane Renmei, AniList
-	};
-
-	for (const auto& item : map)
-		ReplaceAll(string, item.first, item.second);
-}
-
-/* removes stupid HTML stuff */
 void TextifySynopsis(std::string& string) {
-#if 1
 	/* Just let Qt deal with it. */
 	QTextDocument text;
 	text.setHtml(Strings::ToQString(string));
 	string = Strings::ToUtf8String(text.toPlainText());
-#else
-	/* old implementation here... beware of jankiness */
-	SanitizeLineEndings(string);
-	RemoveHtmlTags(string);
-	ParseHtmlEntities(string);
-#endif
 }
 
 /* let Qt handle the heavy lifting of locale shit
@@ -305,4 +253,8 @@
 	return true;
 }
 
+std::string Translate(const char* str) {
+	return Strings::ToUtf8String(QCoreApplication::tr(str));
+}
+
 } // namespace Strings
--- a/src/gui/translate/anime.cc	Wed Jun 12 20:42:44 2024 -0400
+++ b/src/gui/translate/anime.cc	Wed Jun 12 22:15:53 2024 -0400
@@ -192,81 +192,81 @@
 
 std::string ToLocalString(const Anime::ListStatus status) {
 	switch (status) {
-		case Anime::ListStatus::Current: return Strings::ToUtf8String(QCoreApplication::tr("Currently watching"));
-		case Anime::ListStatus::Planning: return Strings::ToUtf8String(QCoreApplication::tr("Plan to watch"));
-		case Anime::ListStatus::Completed: return Strings::ToUtf8String(QCoreApplication::tr("Completed"));
-		case Anime::ListStatus::Dropped: return Strings::ToUtf8String(QCoreApplication::tr("Dropped"));
-		case Anime::ListStatus::Paused: return Strings::ToUtf8String(QCoreApplication::tr("On hold"));
+		case Anime::ListStatus::Current: return Strings::Translate("Currently watching");
+		case Anime::ListStatus::Planning: return Strings::Translate("Plan to watch");
+		case Anime::ListStatus::Completed: return Strings::Translate("Completed");
+		case Anime::ListStatus::Dropped: return Strings::Translate("Dropped");
+		case Anime::ListStatus::Paused: return Strings::Translate("On hold");
 		default:
-		case Anime::ListStatus::NotInList: return Strings::ToUtf8String(QCoreApplication::tr("Not in list"));
+		case Anime::ListStatus::NotInList: return Strings::Translate("Not in list");
 	}
 }
 
 std::string ToLocalString(const Anime::SeriesFormat format) {
 	switch (format) {
-		case Anime::SeriesFormat::Tv: return Strings::ToUtf8String(QCoreApplication::tr("TV"));
-		case Anime::SeriesFormat::TvShort: return Strings::ToUtf8String(QCoreApplication::tr("TV short"));
-		case Anime::SeriesFormat::Ova: return Strings::ToUtf8String(QCoreApplication::tr("OVA"));
-		case Anime::SeriesFormat::Movie: return Strings::ToUtf8String(QCoreApplication::tr("Movie"));
-		case Anime::SeriesFormat::Special: return Strings::ToUtf8String(QCoreApplication::tr("Special"));
-		case Anime::SeriesFormat::Ona: return Strings::ToUtf8String(QCoreApplication::tr("ONA"));
-		case Anime::SeriesFormat::Music: return Strings::ToUtf8String(QCoreApplication::tr("Music"));
+		case Anime::SeriesFormat::Tv: return Strings::Translate("TV");
+		case Anime::SeriesFormat::TvShort: return Strings::Translate("TV short");
+		case Anime::SeriesFormat::Ova: return Strings::Translate("OVA");
+		case Anime::SeriesFormat::Movie: return Strings::Translate("Movie");
+		case Anime::SeriesFormat::Special: return Strings::Translate("Special");
+		case Anime::SeriesFormat::Ona: return Strings::Translate("ONA");
+		case Anime::SeriesFormat::Music: return Strings::Translate("Music");
 		default:
-		case Anime::SeriesFormat::Unknown: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
+		case Anime::SeriesFormat::Unknown: return Strings::Translate("Unknown");
 	}
 }
 
 std::string ToLocalString(const Anime::SeriesSeason season) {
 	switch (season) {
-		case Anime::SeriesSeason::Winter: return Strings::ToUtf8String(QCoreApplication::tr("Winter"));
-		case Anime::SeriesSeason::Summer: return Strings::ToUtf8String(QCoreApplication::tr("Summer"));
-		case Anime::SeriesSeason::Fall: return Strings::ToUtf8String(QCoreApplication::tr("Fall"));
-		case Anime::SeriesSeason::Spring: return Strings::ToUtf8String(QCoreApplication::tr("Spring"));
+		case Anime::SeriesSeason::Winter: return Strings::Translate("Winter");
+		case Anime::SeriesSeason::Summer: return Strings::Translate("Summer");
+		case Anime::SeriesSeason::Fall: return Strings::Translate("Fall");
+		case Anime::SeriesSeason::Spring: return Strings::Translate("Spring");
 		default:
-		case Anime::SeriesSeason::Unknown: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
+		case Anime::SeriesSeason::Unknown: return Strings::Translate("Unknown");
 	}
 }
 
 std::string ToLocalString(const Anime::SeriesStatus status) {
 	switch (status) {
-		case Anime::SeriesStatus::Releasing: return Strings::ToUtf8String(QCoreApplication::tr("Currently airing"));
-		case Anime::SeriesStatus::Finished: return Strings::ToUtf8String(QCoreApplication::tr("Finished airing"));
-		case Anime::SeriesStatus::NotYetReleased: return Strings::ToUtf8String(QCoreApplication::tr("Not yet aired"));
-		case Anime::SeriesStatus::Cancelled: return Strings::ToUtf8String(QCoreApplication::tr("Cancelled"));
-		case Anime::SeriesStatus::Hiatus: return Strings::ToUtf8String(QCoreApplication::tr("On hiatus"));
+		case Anime::SeriesStatus::Releasing: return Strings::Translate("Currently airing");
+		case Anime::SeriesStatus::Finished: return Strings::Translate("Finished airing");
+		case Anime::SeriesStatus::NotYetReleased: return Strings::Translate("Not yet aired");
+		case Anime::SeriesStatus::Cancelled: return Strings::Translate("Cancelled");
+		case Anime::SeriesStatus::Hiatus: return Strings::Translate("On hiatus");
 		default:
-		case Anime::SeriesStatus::Unknown: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
+		case Anime::SeriesStatus::Unknown: return Strings::Translate("Unknown");
 	}
 }
 
 std::string ToLocalString(const Anime::Service service) {
 	switch (service) {
-		case Anime::Service::AniList: return Strings::ToUtf8String(QCoreApplication::tr("AniList"));
-		case Anime::Service::MyAnimeList: return Strings::ToUtf8String(QCoreApplication::tr("MyAnimeList"));
-		case Anime::Service::Kitsu: return Strings::ToUtf8String(QCoreApplication::tr("Kitsu"));
+		case Anime::Service::AniList: return Strings::Translate("AniList");
+		case Anime::Service::MyAnimeList: return Strings::Translate("MyAnimeList");
+		case Anime::Service::Kitsu: return Strings::Translate("Kitsu");
 		default:
-		case Anime::Service::None: return Strings::ToUtf8String(QCoreApplication::tr("None"));
+		case Anime::Service::None: return Strings::Translate("None");
 	}
 }
 
 std::string ToLocalString(const Anime::TitleLanguage language) {
 	switch (language) {
-		case Anime::TitleLanguage::Native: return Strings::ToUtf8String(QCoreApplication::tr("Native"));
-		case Anime::TitleLanguage::English: return Strings::ToUtf8String(QCoreApplication::tr("English"));
+		case Anime::TitleLanguage::Native: return Strings::Translate("Native");
+		case Anime::TitleLanguage::English: return Strings::Translate("English");
 		default:
-		case Anime::TitleLanguage::Romaji: return Strings::ToUtf8String(QCoreApplication::tr("Romaji"));
+		case Anime::TitleLanguage::Romaji: return Strings::Translate("Romaji");
 	}
 }
 
 std::string ToLocalString(const Anime::ScoreFormat format) {
 	switch (format) {
-		case Anime::ScoreFormat::Point3: return Strings::ToUtf8String(QCoreApplication::tr("3-point"));
-		case Anime::ScoreFormat::Point5: return Strings::ToUtf8String(QCoreApplication::tr("5-point"));
-		case Anime::ScoreFormat::Point10: return Strings::ToUtf8String(QCoreApplication::tr("10-point"));
+		case Anime::ScoreFormat::Point3: return Strings::Translate("3-point");
+		case Anime::ScoreFormat::Point5: return Strings::Translate("5-point");
+		case Anime::ScoreFormat::Point10: return Strings::Translate("10-point");
 		case Anime::ScoreFormat::Point10Decimal:
-			return Strings::ToUtf8String(QCoreApplication::tr("10-point Decimal"));
+			return Strings::Translate("10-point Decimal");
 		default:
-		case Anime::ScoreFormat::Point100: return Strings::ToUtf8String(QCoreApplication::tr("100-point"));
+		case Anime::ScoreFormat::Point100: return Strings::Translate("100-point");
 	}
 }
 
--- a/src/gui/translate/config.cc	Wed Jun 12 20:42:44 2024 -0400
+++ b/src/gui/translate/config.cc	Wed Jun 12 22:15:53 2024 -0400
@@ -32,9 +32,9 @@
 std::string ToLocalString(const Theme::Theme& theme) {
 	switch (theme) {
 		default:
-		case Theme::Theme::Default: return Strings::ToUtf8String(QCoreApplication::tr("Default"));
-		case Theme::Theme::Light: return Strings::ToUtf8String(QCoreApplication::tr("Light"));
-		case Theme::Theme::Dark: return Strings::ToUtf8String(QCoreApplication::tr("Dark"));
+		case Theme::Theme::Default: return Strings::Translate("Default");
+		case Theme::Theme::Light: return Strings::Translate("Light");
+		case Theme::Theme::Dark: return Strings::Translate("Dark");
 	}
 }