| 39 | } // namespace Host |
| 40 | |
| 41 | std::pair<const char*, u32> Host::LookupTranslationString(const std::string_view context, const std::string_view msg) |
| 42 | { |
| 43 | // TODO: TranslatableString, compile-time hashing. |
| 44 | |
| 45 | TranslationStringContextMap::iterator ctx_it; |
| 46 | TranslationStringMap::iterator msg_it; |
| 47 | std::pair<const char*, u32> ret; |
| 48 | s32 len; |
| 49 | |
| 50 | // Shouldn't happen, but just in case someone tries to translate an empty string. |
| 51 | if (msg.empty()) [[unlikely]] |
| 52 | { |
| 53 | ret.first = &s_translation_string_cache[0]; |
| 54 | ret.second = 0; |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | s_translation_string_mutex.lock_shared(); |
| 59 | ctx_it = s_translation_string_map.find(context); |
| 60 | |
| 61 | if (ctx_it == s_translation_string_map.end()) [[unlikely]] |
| 62 | goto add_string; |
| 63 | |
| 64 | msg_it = ctx_it->second.find(msg); |
| 65 | if (msg_it == ctx_it->second.end()) [[unlikely]] |
| 66 | goto add_string; |
| 67 | |
| 68 | ret.first = &s_translation_string_cache[msg_it->second.first]; |
| 69 | ret.second = msg_it->second.second; |
| 70 | s_translation_string_mutex.unlock_shared(); |
| 71 | return ret; |
| 72 | |
| 73 | add_string: |
| 74 | s_translation_string_mutex.unlock_shared(); |
| 75 | std::lock_guard lock(s_translation_string_mutex); |
| 76 | |
| 77 | if (s_translation_string_cache.empty()) [[unlikely]] |
| 78 | { |
| 79 | // First element is always an empty string. |
| 80 | s_translation_string_cache.resize(TRANSLATION_STRING_CACHE_SIZE); |
| 81 | s_translation_string_cache[0] = '\0'; |
| 82 | s_translation_string_cache_pos = 0; |
| 83 | } |
| 84 | |
| 85 | if ((len = Internal::GetTranslatedStringImpl(context, msg, |
| 86 | &s_translation_string_cache[s_translation_string_cache_pos], |
| 87 | TRANSLATION_STRING_CACHE_SIZE - 1 - s_translation_string_cache_pos)) < 0) |
| 88 | { |
| 89 | Console.Error("WARNING: Clearing translation string cache, it might need to be larger."); |
| 90 | s_translation_string_cache_pos = 0; |
| 91 | if ((len = Internal::GetTranslatedStringImpl(context, msg, |
| 92 | &s_translation_string_cache[s_translation_string_cache_pos], |
| 93 | TRANSLATION_STRING_CACHE_SIZE - 1 - s_translation_string_cache_pos)) < 0) |
| 94 | { |
| 95 | pxFailRel("Failed to get translated string after clearing cache."); |
| 96 | len = 0; |
| 97 | } |
| 98 | } |