Host: Add TranslatePluralToSmallString()

This commit is contained in:
Stenzek 2024-08-26 14:06:57 +10:00
parent 8a3dbbbea5
commit 2f7a700c15
No known key found for this signature in database
3 changed files with 31 additions and 1 deletions

View file

@ -178,6 +178,24 @@ std::string Host::TranslatePluralToString(const char* context, const char* msg,
return qApp->translate(context, msg, disambiguation, count).toStdString(); 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<const wchar_t*>(qstr.utf16()), qstr.length()));
#else
const QByteArray utf8 = qstr.toUtf8();
ret.assign(utf8.constData(), utf8.length());
#endif
return ret;
}
std::span<const std::pair<const char*, const char*>> Host::GetAvailableLanguageList() std::span<const std::pair<const char*, const char*>> Host::GetAvailableLanguageList()
{ {
static constexpr const std::pair<const char*, const char*> languages[] = {{"English", "en"}, static constexpr const std::pair<const char*, const char*> languages[] = {{"English", "en"},

View file

@ -193,6 +193,14 @@ std::string Host::TranslatePluralToString(const char* context, const char* msg,
return ret; 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<std::mutex>& lock) void Host::LoadSettings(SettingsInterface& si, std::unique_lock<std::mutex>& lock)
{ {
} }

View file

@ -4,6 +4,7 @@
#pragma once #pragma once
#include "common/heap_array.h" #include "common/heap_array.h"
#include "common/small_string.h"
#include "common/types.h" #include "common/types.h"
#include <ctime> #include <ctime>
@ -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. /// 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); 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. /// Clears the translation cache. All previously used strings should be considered invalid.
void ClearTranslationCache(); 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_FS(context, msg) fmt::runtime(Host::TranslateToStringView(context, msg))
#define TRANSLATE_PLURAL_STR(context, msg, disambiguation, count) \ #define TRANSLATE_PLURAL_STR(context, msg, disambiguation, count) \
Host::TranslatePluralToString(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) \ #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. // Does not translate the string at runtime, but allows the UI to in its own way.
#define TRANSLATE_NOOP(context, msg) msg #define TRANSLATE_NOOP(context, msg) msg