changeset 334:948955c3ba81

services: use fmt for setting the status bar this should make localization easier
author Paper <paper@paper.us.eu.org>
date Mon, 17 Jun 2024 20:35:31 -0400
parents 5980a960f3e1
children 5098387a3a46
files src/services/anilist.cc src/services/kitsu.cc src/services/services.cc
diffstat 3 files changed, 42 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/services/anilist.cc	Mon Jun 17 20:03:58 2024 -0400
+++ b/src/services/anilist.cc	Mon Jun 17 20:35:31 2024 -0400
@@ -21,6 +21,8 @@
 #include <exception>
 #include <string_view>
 
+#include <fmt/core.h>
+
 #include <iostream>
 
 namespace Services {
@@ -75,7 +77,7 @@
 
 static std::optional<nlohmann::json> SendJSONRequest(const nlohmann::json& data) {
 	if (!AccountIsValid()) {
-		session.SetStatusBar("AniList: Account isn't valid! (unauthorized?)");
+		session.SetStatusBar(Strings::Translate("AniList: Account isn't valid! (unauthorized?)"));
 		return std::nullopt;
 	}
 
@@ -89,7 +91,7 @@
 
 	const std::string response = Strings::ToUtf8String(HTTP::Request("https://graphql.anilist.co", headers, data.dump(), HTTP::Type::Post));
 	if (response.empty()) {
-		session.SetStatusBar("AniList: JSON request returned an empty result!");
+		session.SetStatusBar(Strings::Translate("AniList: JSON request returned an empty result!"));
 		return std::nullopt;
 	}
 
@@ -98,7 +100,7 @@
 	try {
 		out = nlohmann::json::parse(response);
 	} catch (const std::exception& ex) {
-		session.SetStatusBar("AniList: Failed to parse request JSON with error!");
+		session.SetStatusBar(fmt::format(Strings::Translate("AniList: Failed to parse request JSON with error \"{}\"!"), ex.what()));
 		return std::nullopt;
 	}
 
@@ -107,7 +109,7 @@
 			std::cerr << "AniList: Received an error in response: "
 					  << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl;
 
-		session.SetStatusBar("AniList: Received an error in response!");
+		session.SetStatusBar(Strings::Translate("AniList: Received an error in response!"));
 		return std::nullopt;
 	}
 
@@ -165,7 +167,7 @@
 
 static int ParseMediaJson(const nlohmann::json& json) {
 	if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number()) {
-		session.SetStatusBar("AniList: Failed to parse anime object!");
+		session.SetStatusBar(Strings::Translate("AniList: Failed to parse anime object!"));
 		return 0;
 	}
 
@@ -173,7 +175,7 @@
 
 	int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::AniList, service_id);
 	if (!id) {
-		session.SetStatusBar("AniList: Failed to parse anime object!");
+		session.SetStatusBar(Strings::Translate("AniList: Failed to parse anime object!"));
 		return 0;
 	}
 
@@ -299,7 +301,7 @@
 	};
 	// clang-format on
 
-	session.SetStatusBar("AniList: Parsing anime list...");
+	session.SetStatusBar(Strings::Translate("AniList: Parsing anime list..."));
 
 	const std::optional<nlohmann::json> response = SendJSONRequest(request);
 	if (!response)
@@ -316,7 +318,7 @@
 			success = false;
 
 	if (success)
-		session.SetStatusBar("AniList: Retrieved anime list successfully!");
+		session.SetStatusBar(Strings::Translate("AniList: Retrieved anime list successfully!"));
 
 	return 1;
 }
@@ -443,7 +445,7 @@
 
 	const nlohmann::json& result = res.value();
 
-	session.SetStatusBar("AniList: Anime entry updated successfully!");
+	session.SetStatusBar(Strings::Translate("AniList: Anime entry updated successfully!"));
 
 	return JSON::GetNumber(result, "/data/SaveMediaListEntry/id"_json_pointer, 0);
 }
@@ -471,7 +473,7 @@
 
 	auth.auth_token = Strings::ToUtf8String(token);
 
-	session.SetStatusBar("AniList: Requesting user ID...");
+	session.SetStatusBar(Strings::Translate("AniList: Requesting user ID..."));
 
 	static constexpr std::string_view query =
 		"query {\n"
@@ -492,9 +494,9 @@
 	const nlohmann::json& result = ret.value();
 
 	if (ParseUser(result["data"]["Viewer"]))
-		session.SetStatusBar("AniList: Successfully retrieved user data!");
+		session.SetStatusBar(Strings::Translate("AniList: Successfully retrieved user data!"));
 	else
-		session.SetStatusBar("AniList: Failed to retrieve user ID!");
+		session.SetStatusBar(Strings::Translate("AniList: Failed to retrieve user ID!"));
 	
 	return true;
 }
--- a/src/services/kitsu.cc	Mon Jun 17 20:03:58 2024 -0400
+++ b/src/services/kitsu.cc	Mon Jun 17 20:35:31 2024 -0400
@@ -22,6 +22,8 @@
 #include <exception>
 #include <string_view>
 
+#include <fmt/core.h>
+
 #include <iostream>
 
 using namespace nlohmann::literals::json_literals;
@@ -43,7 +45,7 @@
 
 	const std::string ret = Strings::ToUtf8String(HTTP::Request(std::string(OAUTH_PATH), headers, data.dump(), HTTP::Type::Post));
 	if (ret.empty()) {
-		session.SetStatusBar("Kitsu: Request returned empty data!");
+		session.SetStatusBar(Strings::Translate("Kitsu: Request returned empty data!"));
 		return false;
 	}
 
@@ -51,22 +53,12 @@
 	try {
 		result = nlohmann::json::parse(ret, nullptr, false);
 	} catch (const std::exception& ex) {
-		session.SetStatusBar(std::string("Kitsu: Failed to parse authorization data with error \"") + ex.what() + "\"!");
+		session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to parse authorization data with error \"{}\""), ex.what()));
 		return false;
 	}
 
 	if (result.contains("/error"_json_pointer)) {
-		std::string status = "Kitsu: Failed with error \"";
-		status += result["/error"_json_pointer].get<std::string>();
-
-		if (result.contains("/error_description"_json_pointer)) {
-			status += "\" and description \"";
-			status += result["/error_description"_json_pointer].get<std::string>();
-		}
-
-		status += "\"!";
-
-		session.SetStatusBar(status);
+		session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed with error \"{}\"!"), result["/error"_json_pointer].get<std::string>()));
 		return false;
 	}
 
@@ -81,7 +73,7 @@
 
 	for (const auto& ptr : required) {
 		if (!result.contains(ptr)) {
-			session.SetStatusBar("Kitsu: Authorization request returned bad data!");
+			session.SetStatusBar(Strings::Translate("Kitsu: Authorization request returned bad data!"));
 			return false;
 		}
 	}
@@ -187,7 +179,7 @@
 	try {
 		json = nlohmann::json::parse(response);
 	} catch (const std::exception& ex) {
-		session.SetStatusBar(std::string("Kitsu: Failed to parse response with error \"") + ex.what() + "\"!");
+		session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to parse response with error \"{}\""), ex.what()));
 		return std::nullopt;
 	}
 
@@ -195,7 +187,7 @@
 		for (const auto& item : json["/errors"])
 			std::cerr << "Kitsu: API returned error \"" << json["/errors/title"_json_pointer] << "\" with detail \"" << json["/errors/detail"] << std::endl;
 
-		session.SetStatusBar("Kitsu: Request failed with errors!");
+		session.SetStatusBar(Strings::Translate("Kitsu: Request failed with errors!"));
 		return std::nullopt;
 	}
 
@@ -261,16 +253,16 @@
 }
 
 static int ParseAnimeJson(const nlohmann::json& json) {
-	static const std::string FAILED_TO_PARSE = "Kitsu: Failed to parse anime object!";
+	const std::string FAILED_TO_PARSE = Strings::Translate("Kitsu: Failed to parse anime object! {}");
 
 	const std::string service_id = json["/id"_json_pointer].get<std::string>();
 	if (service_id.empty()) {
-		session.SetStatusBar(FAILED_TO_PARSE + " (/id)");
+		session.SetStatusBar(fmt::format(FAILED_TO_PARSE, "(/id)"));
 		return 0;
 	}
 
 	if (!json.contains("/attributes"_json_pointer)) {
-		session.SetStatusBar(FAILED_TO_PARSE + " (/attributes)");
+		session.SetStatusBar(fmt::format(FAILED_TO_PARSE, "(/attributes)"));
 		return 0;
 	}
 
@@ -330,14 +322,14 @@
 
 	for (const auto& ptr : required) {
 		if (!json.contains(ptr)) {
-			session.SetStatusBar(std::string("Kitsu: Failed to parse library object! (missing ") + ptr.to_string() + ")");
+			session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to parse library object! (missing {})"), ptr.to_string()));
 			return 0;
 		}
 	}
 
 	std::string service_id = json["/relationships/anime/data/id"_json_pointer].get<std::string>();
 	if (service_id.empty()) {
-		session.SetStatusBar("Kitsu: Failed to parse library object!");
+		session.SetStatusBar(Strings::Translate("Kitsu: Failed to parse library object (missing service ID)!"));
 		return 0;
 	}
 
@@ -429,7 +421,7 @@
 static bool ParseAnyJson(const nlohmann::json& json) {
 	static const nlohmann::json::json_pointer required = "/type"_json_pointer;
 	if (!json.contains(required) && !json[required].is_string()) {
-		session.SetStatusBar(std::string("Kitsu: Failed to parse generic object! (missing ") + required.to_string() + ")");
+		session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to generic object! (missing {})"), required.to_string()));
 		return 0;
 	}
 
@@ -454,7 +446,7 @@
 	const auto& auth = session.config.auth.kitsu;
 
 	if (auth.user_id.empty()) {
-		session.SetStatusBar("Kitsu: User ID is unavailable!");
+		session.SetStatusBar(Strings::Translate("Kitsu: User ID is unavailable!"));
 		return 0;
 	}
 
@@ -500,7 +492,7 @@
 	}
 
 	if (success)
-		session.SetStatusBar("Kitsu: Successfully received library data!");
+		session.SetStatusBar(Strings::Translate("Kitsu: Successfully received library data!"));
 
 	return 1;
 }
@@ -515,7 +507,7 @@
 	if (!service_id)
 		return false;
 
-	session.SetStatusBar("Kitsu: Retrieving anime metadata...");
+	session.SetStatusBar(Strings::Translate("Kitsu: Retrieving anime metadata..."));
 
 	static const std::map<std::string, std::string> params = {
 		{"include", Strings::Implode({
@@ -532,13 +524,13 @@
 	const auto& json = response.value();
 
 	if (!json.contains("/included"_json_pointer) || !json["/included"_json_pointer].is_array()) {
-		session.SetStatusBar("Kitsu: Server returned bad data when trying to retrieve anime metadata!");
+		session.SetStatusBar(Strings::Translate("Kitsu: Server returned bad data when trying to retrieve anime metadata!"));
 		return false;
 	}
 
 	ParseMetadataJson(anime, json["/included"_json_pointer]);
 
-	session.SetStatusBar("Kitsu: Successfully retrieved anime metadata!");
+	session.SetStatusBar(Strings::Translate("Kitsu: Successfully retrieved anime metadata!"));
 
 	return true;
 }
@@ -577,11 +569,11 @@
 	const nlohmann::json& json = response.value();
 
 	if (!json.contains("/data/0/id"_json_pointer)) {
-		session.SetStatusBar("Kitsu: Failed to retrieve user ID!");
+		session.SetStatusBar(Strings::Translate("Kitsu: Failed to retrieve user ID!"));
 		return false;
 	}
 
-	session.SetStatusBar("Kitsu: Successfully authorized user!");
+	session.SetStatusBar(Strings::Translate("Kitsu: Successfully authorized user!"));
 	session.config.auth.kitsu.user_id = json["/data/0/id"_json_pointer].get<std::string>();
 
 	return true;
--- a/src/services/services.cc	Mon Jun 17 20:03:58 2024 -0400
+++ b/src/services/services.cc	Mon Jun 17 20:35:31 2024 -0400
@@ -1,13 +1,16 @@
 #include "services/services.h"
 #include "core/session.h"
+#include "core/strings.h"
 #include "gui/translate/anime.h"
 #include "services/anilist.h"
 #include "services/kitsu.h"
 
+#include <fmt/core.h>
+
 namespace Services {
 
 void Synchronize() {
-	session.SetStatusBar(Translate::ToString(session.config.service) + ": Retrieving anime list...");
+	session.SetStatusBar(fmt::format(Strings::Translate("{}: Retrieving anime list..."), Translate::ToString(session.config.service)));
 
 	switch (session.config.service) {
 		case Anime::Service::AniList: AniList::GetAnimeList(); break;
@@ -24,7 +27,7 @@
 }
 
 std::vector<int> Search(const std::string& search) {
-	session.SetStatusBar(Translate::ToString(session.config.service) + ": Requesting search query...");
+	session.SetStatusBar(fmt::format(Strings::Translate("{}: Requesting search query..."), Translate::ToString(session.config.service)));
 
 	switch (session.config.service) {
 		case Anime::Service::AniList: return AniList::Search(search);
@@ -34,7 +37,7 @@
 }
 
 bool GetSeason(Anime::Season season) {
-	session.SetStatusBar(Translate::ToString(session.config.service) + ": Retrieving anime season data...");
+	session.SetStatusBar(fmt::format(Strings::Translate("{}: Retrieving anime season data..."), Translate::ToString(session.config.service)));
 
 	switch (session.config.service) {
 		case Anime::Service::AniList: return AniList::GetSeason(season);
@@ -44,7 +47,7 @@
 }
 
 void UpdateAnimeEntry(int id) {
-	session.SetStatusBar(Translate::ToString(session.config.service) + ": Updating remote anime entry...");
+	session.SetStatusBar(fmt::format(Strings::Translate("{}: Updating remote anime entry..."), Translate::ToString(session.config.service)));
 
 	switch (session.config.service) {
 		case Anime::Service::AniList: AniList::UpdateAnimeEntry(id); break;