diff src/services/anilist.cc @ 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 b5d6c27c308f
children f81bed4e04ac
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;
 }