From d95226f8c0296c17e75d5810d03455c5f24e66c3 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Sat, 3 May 2025 12:59:04 +0100 Subject: [PATCH] i18n::get should accept a string_view rather than char*, simplifies calling. --- sphaira/include/i18n.hpp | 3 ++- sphaira/source/app.cpp | 2 +- sphaira/source/i18n.cpp | 12 ++++++------ sphaira/source/ui/menus/game_menu.cpp | 2 +- sphaira/source/ui/sidebar.cpp | 4 ++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/sphaira/include/i18n.hpp b/sphaira/include/i18n.hpp index 5b03658..d47a1e2 100644 --- a/sphaira/include/i18n.hpp +++ b/sphaira/include/i18n.hpp @@ -1,13 +1,14 @@ #pragma once #include +#include namespace sphaira::i18n { bool init(long index); void exit(); -std::string get(const char* str); +std::string get(std::string_view str); } // namespace sphaira::i18n diff --git a/sphaira/source/app.cpp b/sphaira/source/app.cpp index bfa8b09..3cfa94c 100644 --- a/sphaira/source/app.cpp +++ b/sphaira/source/app.cpp @@ -1588,7 +1588,7 @@ void App::DisplayAdvancedOptions(bool left_side) { } )); } - }, i18n::get(g_app->m_right_side_menu.Get().c_str()))); + }, i18n::get(g_app->m_right_side_menu.Get()))); options->Add(std::make_shared("Install options"_i18n, [left_side](){ App::DisplayInstallOptions(left_side); diff --git a/sphaira/source/i18n.cpp b/sphaira/source/i18n.cpp index 24f4a69..7b44f1a 100644 --- a/sphaira/source/i18n.cpp +++ b/sphaira/source/i18n.cpp @@ -13,8 +13,8 @@ yyjson_doc* json; yyjson_val* root; std::unordered_map g_tr_cache; -std::string get_internal(const char* str, size_t len) { - const std::string kkey = {str, len}; +std::string get_internal(std::string_view str) { + const std::string kkey = {str.data(), str.length()}; if (auto it = g_tr_cache.find(kkey); it != g_tr_cache.end()) { return it->second; @@ -28,7 +28,7 @@ std::string get_internal(const char* str, size_t len) { return kkey; } - auto key = yyjson_obj_getn(root, str, len); + auto key = yyjson_obj_getn(root, str.data(), str.length()); if (!key) { log_write("\tfailed to find key: [%s]\n", kkey.c_str()); return kkey; @@ -134,8 +134,8 @@ void exit() { g_i18n_data.clear(); } -std::string get(const char* str) { - return get_internal(str, std::strlen(str)); +std::string get(std::string_view str) { + return get_internal(str); } } // namespace sphaira::i18n @@ -143,7 +143,7 @@ std::string get(const char* str) { namespace literals { std::string operator"" _i18n(const char* str, size_t len) { - return sphaira::i18n::get_internal(str, len); + return sphaira::i18n::get_internal({str, len}); } } // namespace literals diff --git a/sphaira/source/ui/menus/game_menu.cpp b/sphaira/source/ui/menus/game_menu.cpp index 80bd986..02ae0c7 100644 --- a/sphaira/source/ui/menus/game_menu.cpp +++ b/sphaira/source/ui/menus/game_menu.cpp @@ -23,7 +23,7 @@ using MetaEntries = std::vector; Result Notify(Result rc, const std::string& error_message) { if (R_FAILED(rc)) { App::Push(std::make_shared(rc, - i18n::get(error_message.c_str()) + i18n::get(error_message) )); } else { App::Notify("Success"); diff --git a/sphaira/source/ui/sidebar.cpp b/sphaira/source/ui/sidebar.cpp index d255c09..08bd410 100644 --- a/sphaira/source/ui/sidebar.cpp +++ b/sphaira/source/ui/sidebar.cpp @@ -44,10 +44,10 @@ SidebarEntryBool::SidebarEntryBool(std::string title, bool option, Callback cb, , m_false_str{std::move(false_str)} { if (m_true_str == "On") { - m_true_str = i18n::get(m_true_str.c_str()); + m_true_str = i18n::get(m_true_str); } if (m_false_str == "Off") { - m_false_str = i18n::get(m_false_str.c_str()); + m_false_str = i18n::get(m_false_str); } SetAction(Button::A, Action{"OK"_i18n, [this](){