Mercurial > minori
diff src/services/kitsu.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 | 5098387a3a46 |
line wrap: on
line diff
--- 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;