From 2f7a700c157c42b68b7aac82fd05156f707672ac Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 26 Aug 2024 14:06:57 +1000 Subject: [PATCH] Host: Add TranslatePluralToSmallString() --- src/duckstation-qt/qttranslations.cpp | 18 ++++++++++++++++++ src/duckstation-regtest/regtest_host.cpp | 8 ++++++++ src/util/host.h | 6 +++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/duckstation-qt/qttranslations.cpp b/src/duckstation-qt/qttranslations.cpp index 54e10ccf8..d2232fdac 100644 --- a/src/duckstation-qt/qttranslations.cpp +++ b/src/duckstation-qt/qttranslations.cpp @@ -178,6 +178,24 @@ std::string Host::TranslatePluralToString(const char* context, const char* msg, return qApp->translate(context, msg, disambiguation, count).toStdString(); } +SmallString Host::TranslatePluralToSmallString(const char* context, const char* msg, const char* disambiguation, + int count) +{ + const QString qstr = qApp->translate(context, msg, disambiguation, count); + SmallString ret; + +#ifdef _WIN32 + // Cheeky way to avoid heap allocations. + static_assert(sizeof(*qstr.utf16()) == sizeof(wchar_t)); + ret.assign(std::wstring_view(reinterpret_cast(qstr.utf16()), qstr.length())); +#else + const QByteArray utf8 = qstr.toUtf8(); + ret.assign(utf8.constData(), utf8.length()); +#endif + + return ret; +} + std::span> Host::GetAvailableLanguageList() { static constexpr const std::pair languages[] = {{"English", "en"}, diff --git a/src/duckstation-regtest/regtest_host.cpp b/src/duckstation-regtest/regtest_host.cpp index 78de0df27..fb64d24d2 100644 --- a/src/duckstation-regtest/regtest_host.cpp +++ b/src/duckstation-regtest/regtest_host.cpp @@ -193,6 +193,14 @@ std::string Host::TranslatePluralToString(const char* context, const char* msg, return ret; } +SmallString Host::TranslatePluralToSmallString(const char* context, const char* msg, const char* disambiguation, + int count) +{ + SmallString ret(msg); + ret.replace("%n", TinyString::from_format("{}", count)); + return ret; +} + void Host::LoadSettings(SettingsInterface& si, std::unique_lock& lock) { } diff --git a/src/util/host.h b/src/util/host.h index 26567a8c2..3af97fe8d 100644 --- a/src/util/host.h +++ b/src/util/host.h @@ -4,6 +4,7 @@ #pragma once #include "common/heap_array.h" +#include "common/small_string.h" #include "common/types.h" #include @@ -62,6 +63,7 @@ std::string TranslateToString(std::string_view context, std::string_view msg); /// Returns a localized version of the specified string within the specified context, adjusting for plurals using %n. std::string TranslatePluralToString(const char* context, const char* msg, const char* disambiguation, int count); +SmallString TranslatePluralToSmallString(const char* context, const char* msg, const char* disambiguation, int count); /// Clears the translation cache. All previously used strings should be considered invalid. void ClearTranslationCache(); @@ -79,8 +81,10 @@ s32 GetTranslatedStringImpl(std::string_view context, std::string_view msg, char #define TRANSLATE_FS(context, msg) fmt::runtime(Host::TranslateToStringView(context, msg)) #define TRANSLATE_PLURAL_STR(context, msg, disambiguation, count) \ Host::TranslatePluralToString(context, msg, disambiguation, count) +#define TRANSLATE_PLURAL_SSTR(context, msg, disambiguation, count) \ + Host::TranslatePluralToSmallString(context, msg, disambiguation, count) #define TRANSLATE_PLURAL_FS(context, msg, disambiguation, count) \ - fmt::runtime(Host::TranslatePluralToString(context, msg, disambiguation, count)) + fmt::runtime(Host::TranslatePluralToSmallString(context, msg, disambiguation, count).view()) // Does not translate the string at runtime, but allows the UI to in its own way. #define TRANSLATE_NOOP(context, msg) msg