From 24b66dede00c8a5336adc6c1fafc837ee688c9ad Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 19 Aug 2021 19:13:25 +0100 Subject: [PATCH] Add fwgettext util function --- src/client/client.cpp | 2 +- src/client/game.cpp | 43 ++++++++++++------------------------------- src/client/gameui.cpp | 9 +++------ src/gettext.h | 18 ++++++++++++++++++ util/updatepo.sh | 1 + 5 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/client/client.cpp b/src/client/client.cpp index 923369afe..3c5559fca 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1735,7 +1735,7 @@ void Client::afterContentReceived() tu_args.guienv = guienv; tu_args.last_time_ms = porting::getTimeMs(); tu_args.last_percent = 0; - tu_args.text_base = wgettext("Initializing nodes"); + tu_args.text_base = wgettext("Initializing nodes"); tu_args.tsrc = m_tsrc; m_nodedef->updateTextures(this, &tu_args); delete[] tu_args.text_base; diff --git a/src/client/game.cpp b/src/client/game.cpp index 73ad73e0e..90bfab2a3 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -1927,24 +1927,18 @@ void Game::processKeyInput() } else if (wasKeyDown(KeyType::INC_VOLUME)) { if (g_settings->getBool("enable_sound")) { float new_volume = rangelim(g_settings->getFloat("sound_volume") + 0.1f, 0.0f, 1.0f); - wchar_t buf[100]; g_settings->setFloat("sound_volume", new_volume); - const wchar_t *str = wgettext("Volume changed to %d%%"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100)); - delete[] str; - m_game_ui->showStatusText(buf); + std::wstring msg = fwgettext("Volume changed to %d%%", myround(new_volume * 100)); + m_game_ui->showStatusText(msg); } else { m_game_ui->showTranslatedStatusText("Sound system is disabled"); } } else if (wasKeyDown(KeyType::DEC_VOLUME)) { if (g_settings->getBool("enable_sound")) { float new_volume = rangelim(g_settings->getFloat("sound_volume") - 0.1f, 0.0f, 1.0f); - wchar_t buf[100]; g_settings->setFloat("sound_volume", new_volume); - const wchar_t *str = wgettext("Volume changed to %d%%"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100)); - delete[] str; - m_game_ui->showStatusText(buf); + std::wstring msg = fwgettext("Volume changed to %d%%", myround(new_volume * 100)); + m_game_ui->showStatusText(msg); } else { m_game_ui->showTranslatedStatusText("Sound system is disabled"); } @@ -2329,20 +2323,13 @@ void Game::increaseViewRange() s16 range = g_settings->getS16("viewing_range"); s16 range_new = range + 10; - wchar_t buf[255]; - const wchar_t *str; if (range_new > 4000) { range_new = 4000; - str = wgettext("Viewing range is at maximum: %d"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); - delete[] str; - m_game_ui->showStatusText(buf); - + std::wstring msg = fwgettext("Viewing range is at maximum: %d", range_new); + m_game_ui->showStatusText(msg); } else { - str = wgettext("Viewing range changed to %d"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); - delete[] str; - m_game_ui->showStatusText(buf); + std::wstring msg = fwgettext("Viewing range changed to %d", range_new); + m_game_ui->showStatusText(msg); } g_settings->set("viewing_range", itos(range_new)); } @@ -2353,19 +2340,13 @@ void Game::decreaseViewRange() s16 range = g_settings->getS16("viewing_range"); s16 range_new = range - 10; - wchar_t buf[255]; - const wchar_t *str; if (range_new < 20) { range_new = 20; - str = wgettext("Viewing range is at minimum: %d"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); - delete[] str; - m_game_ui->showStatusText(buf); + std::wstring msg = fwgettext("Viewing range is at minimum: %d", range_new); + m_game_ui->showStatusText(msg); } else { - str = wgettext("Viewing range changed to %d"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); - delete[] str; - m_game_ui->showStatusText(buf); + std::wstring msg = fwgettext("Viewing range changed to %d", range_new); + m_game_ui->showStatusText(msg); } g_settings->set("viewing_range", itos(range_new)); } diff --git a/src/client/gameui.cpp b/src/client/gameui.cpp index 323967550..028052fe6 100644 --- a/src/client/gameui.cpp +++ b/src/client/gameui.cpp @@ -299,12 +299,9 @@ void GameUI::toggleProfiler() updateProfiler(); if (m_profiler_current_page != 0) { - wchar_t buf[255]; - const wchar_t* str = wgettext("Profiler shown (page %d of %d)"); - swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, - m_profiler_current_page, m_profiler_max_page); - delete[] str; - showStatusText(buf); + std::wstring msg = fwgettext("Profiler shown (page %d of %d)", + m_profiler_current_page, m_profiler_max_page); + showStatusText(msg); } else { showTranslatedStatusText("Profiler hidden"); } diff --git a/src/gettext.h b/src/gettext.h index 42b375d86..5a3654be4 100644 --- a/src/gettext.h +++ b/src/gettext.h @@ -59,3 +59,21 @@ inline std::string strgettext(const std::string &text) { return text.empty() ? "" : gettext(text.c_str()); } + +/** + * Returns translated string with format args applied + * + * @tparam Args Template parameter for format args + * @param src Translation source string + * @param args Variable format args + * @return translated string + */ +template +inline std::wstring fwgettext(const char *src, Args&&... args) +{ + wchar_t buf[255]; + const wchar_t* str = wgettext(src); + swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, std::forward(args)...); + delete[] str; + return std::wstring(buf); +} diff --git a/util/updatepo.sh b/util/updatepo.sh index 95acb01ea..dbcb16fde 100755 --- a/util/updatepo.sh +++ b/util/updatepo.sh @@ -54,6 +54,7 @@ xgettext --package-name=minetest \ --add-location=file \ --keyword=N_ \ --keyword=wgettext \ + --keyword=fwgettext \ --keyword=fgettext \ --keyword=fgettext_ne \ --keyword=strgettext \