Add fwgettext util function

This commit is contained in:
rubenwardy 2021-08-19 19:13:25 +01:00 committed by GitHub
parent 3b842a7e02
commit 24b66dede0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 38 deletions

@ -1927,24 +1927,18 @@ void Game::processKeyInput()
} else if (wasKeyDown(KeyType::INC_VOLUME)) { } else if (wasKeyDown(KeyType::INC_VOLUME)) {
if (g_settings->getBool("enable_sound")) { if (g_settings->getBool("enable_sound")) {
float new_volume = rangelim(g_settings->getFloat("sound_volume") + 0.1f, 0.0f, 1.0f); 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); g_settings->setFloat("sound_volume", new_volume);
const wchar_t *str = wgettext("Volume changed to %d%%"); std::wstring msg = fwgettext("Volume changed to %d%%", myround(new_volume * 100));
swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100)); m_game_ui->showStatusText(msg);
delete[] str;
m_game_ui->showStatusText(buf);
} else { } else {
m_game_ui->showTranslatedStatusText("Sound system is disabled"); m_game_ui->showTranslatedStatusText("Sound system is disabled");
} }
} else if (wasKeyDown(KeyType::DEC_VOLUME)) { } else if (wasKeyDown(KeyType::DEC_VOLUME)) {
if (g_settings->getBool("enable_sound")) { if (g_settings->getBool("enable_sound")) {
float new_volume = rangelim(g_settings->getFloat("sound_volume") - 0.1f, 0.0f, 1.0f); 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); g_settings->setFloat("sound_volume", new_volume);
const wchar_t *str = wgettext("Volume changed to %d%%"); std::wstring msg = fwgettext("Volume changed to %d%%", myround(new_volume * 100));
swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100)); m_game_ui->showStatusText(msg);
delete[] str;
m_game_ui->showStatusText(buf);
} else { } else {
m_game_ui->showTranslatedStatusText("Sound system is disabled"); m_game_ui->showTranslatedStatusText("Sound system is disabled");
} }
@ -2329,20 +2323,13 @@ void Game::increaseViewRange()
s16 range = g_settings->getS16("viewing_range"); s16 range = g_settings->getS16("viewing_range");
s16 range_new = range + 10; s16 range_new = range + 10;
wchar_t buf[255];
const wchar_t *str;
if (range_new > 4000) { if (range_new > 4000) {
range_new = 4000; range_new = 4000;
str = wgettext("Viewing range is at maximum: %d"); std::wstring msg = fwgettext("Viewing range is at maximum: %d", range_new);
swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); m_game_ui->showStatusText(msg);
delete[] str;
m_game_ui->showStatusText(buf);
} else { } else {
str = wgettext("Viewing range changed to %d"); std::wstring msg = fwgettext("Viewing range changed to %d", range_new);
swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); m_game_ui->showStatusText(msg);
delete[] str;
m_game_ui->showStatusText(buf);
} }
g_settings->set("viewing_range", itos(range_new)); g_settings->set("viewing_range", itos(range_new));
} }
@ -2353,19 +2340,13 @@ void Game::decreaseViewRange()
s16 range = g_settings->getS16("viewing_range"); s16 range = g_settings->getS16("viewing_range");
s16 range_new = range - 10; s16 range_new = range - 10;
wchar_t buf[255];
const wchar_t *str;
if (range_new < 20) { if (range_new < 20) {
range_new = 20; range_new = 20;
str = wgettext("Viewing range is at minimum: %d"); std::wstring msg = fwgettext("Viewing range is at minimum: %d", range_new);
swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); m_game_ui->showStatusText(msg);
delete[] str;
m_game_ui->showStatusText(buf);
} else { } else {
str = wgettext("Viewing range changed to %d"); std::wstring msg = fwgettext("Viewing range changed to %d", range_new);
swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new); m_game_ui->showStatusText(msg);
delete[] str;
m_game_ui->showStatusText(buf);
} }
g_settings->set("viewing_range", itos(range_new)); g_settings->set("viewing_range", itos(range_new));
} }

@ -299,12 +299,9 @@ void GameUI::toggleProfiler()
updateProfiler(); updateProfiler();
if (m_profiler_current_page != 0) { if (m_profiler_current_page != 0) {
wchar_t buf[255]; std::wstring msg = fwgettext("Profiler shown (page %d of %d)",
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); m_profiler_current_page, m_profiler_max_page);
delete[] str; showStatusText(msg);
showStatusText(buf);
} else { } else {
showTranslatedStatusText("Profiler hidden"); showTranslatedStatusText("Profiler hidden");
} }

@ -59,3 +59,21 @@ inline std::string strgettext(const std::string &text)
{ {
return text.empty() ? "" : gettext(text.c_str()); 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 <typename ...Args>
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>(args)...);
delete[] str;
return std::wstring(buf);
}

@ -54,6 +54,7 @@ xgettext --package-name=minetest \
--add-location=file \ --add-location=file \
--keyword=N_ \ --keyword=N_ \
--keyword=wgettext \ --keyword=wgettext \
--keyword=fwgettext \
--keyword=fgettext \ --keyword=fgettext \
--keyword=fgettext_ne \ --keyword=fgettext_ne \
--keyword=strgettext \ --keyword=strgettext \