1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-15 18:57:08 +00:00

Add support for translating content titles and descriptions (#12208)

This commit is contained in:
rubenwardy 2024-02-24 19:13:07 +00:00 committed by GitHub
parent 57de599a29
commit b4be483d3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 252 additions and 47 deletions

View file

@ -36,6 +36,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/guiscalingfilter.h"
#include "irrlicht_changes/static_text.h"
#include "client/tile.h"
#include "content/content.h"
#include "content/mods.h"
#if USE_SOUND
#include "client/sound/sound_openal.h"
@ -204,6 +206,57 @@ GUIEngine::GUIEngine(JoystickController *joystick,
m_menu.reset();
}
/******************************************************************************/
std::string findLocaleFileInMods(const std::string &path, const std::string &filename)
{
std::vector<ModSpec> mods = flattenMods(getModsInPath(path, "root", true));
for (const auto &mod : mods) {
std::string ret = mod.path + DIR_DELIM "locale" DIR_DELIM + filename;
if (fs::PathExists(ret)) {
return ret;
}
}
return "";
}
/******************************************************************************/
Translations *GUIEngine::getContentTranslations(const std::string &path,
const std::string &domain, const std::string &lang_code)
{
if (domain.empty() || lang_code.empty())
return nullptr;
std::string filename = domain + "." + lang_code + ".tr";
std::string key = path + DIR_DELIM "locale" DIR_DELIM + filename;
if (key == m_last_translations_key)
return &m_last_translations;
std::string trans_path = key;
ContentType type = getContentType(path);
if (type == ContentType::GAME)
trans_path = findLocaleFileInMods(path + DIR_DELIM "mods" DIR_DELIM, filename);
else if (type == ContentType::MODPACK)
trans_path = findLocaleFileInMods(path, filename);
// We don't need to search for locale files in a mod, as there's only one `locale` folder.
if (trans_path.empty())
return nullptr;
m_last_translations_key = key;
m_last_translations = {};
std::string data;
if (fs::ReadFile(trans_path, data)) {
m_last_translations.loadTranslation(data);
}
return &m_last_translations;
}
/******************************************************************************/
bool GUIEngine::loadMainMenuScript()
{

View file

@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/sound.h"
#include "client/tile.h"
#include "util/enriched_string.h"
#include "translation.h"
/******************************************************************************/
/* Structs and macros */
@ -165,7 +166,22 @@ public:
return m_scriptdir;
}
/**
* Get translations for content
*
* Only loads a single textdomain from the path, as specified by `domain`,
* for performance reasons.
*
* WARNING: Do not store the returned pointer for long as the contents may
* change with the next call to `getContentTranslations`.
* */
Translations *getContentTranslations(const std::string &path,
const std::string &domain, const std::string &lang_code);
private:
std::string m_last_translations_key;
/** Only the most recently used translation set is kept loaded */
Translations m_last_translations;
/** find and run the main menu script */
bool loadMainMenuScript();