refactor: let client pass in translated string of None when calling tr_strratio() (#7805)

This commit is contained in:
Charles Kerr
2025-11-14 10:04:04 -06:00
committed by GitHub
parent cfd5647c58
commit 73e6892a74
7 changed files with 18 additions and 15 deletions

View File

@@ -111,7 +111,7 @@ Glib::ustring gtr_get_unicode_string(GtrUnicode uni)
Glib::ustring tr_strlratio(double ratio) Glib::ustring tr_strlratio(double ratio)
{ {
return tr_strratio(ratio, gtr_get_unicode_string(GtrUnicode::Inf).c_str()); return tr_strratio(ratio, Q_("None"), gtr_get_unicode_string(GtrUnicode::Inf).c_str());
} }
Glib::ustring tr_strlsize(libtransmission::Values::Storage const& storage) Glib::ustring tr_strlsize(libtransmission::Values::Storage const& storage)

View File

@@ -561,11 +561,11 @@ std::string tr_strpercent(double x)
return fmt::format("{:.0Lf}", x); return fmt::format("{:.0Lf}", x);
} }
std::string tr_strratio(double ratio, std::string_view infinity) std::string tr_strratio(double ratio, std::string_view const none, std::string_view const infinity)
{ {
if ((int)ratio == TR_RATIO_NA) if ((int)ratio == TR_RATIO_NA)
{ {
return _("None"); return std::string{ none };
} }
if ((int)ratio == TR_RATIO_INF) if ((int)ratio == TR_RATIO_INF)

View File

@@ -247,7 +247,7 @@ template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
/** @param ratio the ratio to convert to a string /** @param ratio the ratio to convert to a string
@param infinity the string representation of "infinity" */ @param infinity the string representation of "infinity" */
[[nodiscard]] std::string tr_strratio(double ratio, std::string_view infinity); [[nodiscard]] std::string tr_strratio(double ratio, std::string_view none, std::string_view infinity);
// --- // ---

View File

@@ -47,6 +47,14 @@ QString Formatter::storage_to_string(int64_t const bytes)
return storage_to_string(static_cast<uint64_t>(bytes)); return storage_to_string(static_cast<uint64_t>(bytes));
} }
QString Formatter::ratio_to_string(double ratio)
{
static auto constexpr Infinity = "\xE2\x88\x9E"sv;
static auto const none = tr("None").toStdString();
return QString::fromStdString(tr_strratio(ratio, none, Infinity));
}
QString Formatter::time_to_string(int seconds) QString Formatter::time_to_string(int seconds)
{ {
seconds = std::max(seconds, 0); seconds = std::max(seconds, 0);

View File

@@ -29,12 +29,7 @@ public:
return QString::fromStdString(tr_strpercent(x)); return QString::fromStdString(tr_strpercent(x));
} }
[[nodiscard]] static auto ratio_to_string(double ratio) [[nodiscard]] static QString ratio_to_string(double ratio);
{
static auto constexpr InfinitySymbol = "\xE2\x88\x9E";
return QString::fromStdString(tr_strratio(ratio, InfinitySymbol));
}
[[nodiscard]] static QString storage_to_string(int64_t bytes); [[nodiscard]] static QString storage_to_string(int64_t bytes);
[[nodiscard]] static QString storage_to_string(uint64_t bytes); [[nodiscard]] static QString storage_to_string(uint64_t bytes);

View File

@@ -308,13 +308,13 @@ TEST_F(UtilsTest, ratioToString)
{ TR_RATIO_INF, "inf" } } }; { TR_RATIO_INF, "inf" } } };
char const nullchar = '\0'; char const nullchar = '\0';
ASSERT_EQ(tr_strratio(TR_RATIO_NA, "Ratio is NaN"), "None"); ASSERT_EQ(tr_strratio(TR_RATIO_NA, "None", "Ratio is NaN"), "None");
ASSERT_EQ(tr_strratio(TR_RATIO_INF, "A bit longer text for infinity"), "A bit longer text for infinity"); ASSERT_EQ(tr_strratio(TR_RATIO_INF, "None", "A bit longer text for infinity"), "A bit longer text for infinity");
// Inf contains only null character // Inf contains only null character
ASSERT_EQ(tr_strratio(TR_RATIO_INF, &nullchar), ""); ASSERT_EQ(tr_strratio(TR_RATIO_INF, "None", &nullchar), "");
for (auto const& [input, expected] : Tests) for (auto const& [input, expected] : Tests)
{ {
ASSERT_EQ(tr_strratio(input, "inf"), expected); ASSERT_EQ(tr_strratio(input, "None", "inf"), expected);
} }
} }

View File

@@ -168,7 +168,7 @@ struct RemoteConfig
[[nodiscard]] auto strlratio2(double ratio) [[nodiscard]] auto strlratio2(double ratio)
{ {
return tr_strratio(ratio, "Inf"); return tr_strratio(ratio, "None", "Inf");
} }
[[nodiscard]] auto strlratio(int64_t numerator, int64_t denominator) [[nodiscard]] auto strlratio(int64_t numerator, int64_t denominator)