Many strings are used directly as printf/ImGui format strings, and translations are loaded from user/installer-modifiable JSON with no validation. A translated value that drops or changes a conversion specifier would be passed to printf with mismatched varargs (undefined behavior) on a wallet screen. overlayTranslations() now compares each translated value's argument signature against the English source and keeps English on mismatch. Also adds the send_status_unconfirmed string used by the deferred-send-result path. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
87 lines
2.4 KiB
C++
87 lines
2.4 KiB
C++
// DragonX Wallet - ImGui Edition
|
|
// Copyright 2024-2026 The Hush Developers
|
|
// Released under the GPLv3
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
#include <nlohmann/json_fwd.hpp>
|
|
|
|
namespace dragonx {
|
|
namespace util {
|
|
|
|
/**
|
|
* @brief Internationalization support
|
|
*
|
|
* Simple string table implementation for translating UI text.
|
|
* Strings are stored in JSON files in res/lang/ directory.
|
|
*/
|
|
class I18n {
|
|
public:
|
|
/**
|
|
* @brief Get the singleton instance
|
|
*/
|
|
static I18n& instance();
|
|
|
|
/**
|
|
* @brief Load a language file
|
|
* @param locale Language code (e.g., "en", "es", "zh")
|
|
* @return true if loaded successfully
|
|
*/
|
|
bool loadLanguage(const std::string& locale);
|
|
|
|
/**
|
|
* @brief Translate a string
|
|
* @param key The string key (usually English text)
|
|
* @return Translated string, or key if not found
|
|
*/
|
|
const char* translate(const char* key) const;
|
|
|
|
/**
|
|
* @brief Get current locale
|
|
*/
|
|
const std::string& getCurrentLocale() const { return current_locale_; }
|
|
|
|
/**
|
|
* @brief Get list of available languages
|
|
*/
|
|
const std::vector<std::pair<std::string, std::string>>& getAvailableLanguages() const { return available_languages_; }
|
|
|
|
/**
|
|
* @brief Register an available language
|
|
* @param code Language code (e.g., "en")
|
|
* @param name Display name (e.g., "English")
|
|
*/
|
|
void registerLanguage(const std::string& code, const std::string& name);
|
|
|
|
private:
|
|
I18n();
|
|
void loadBuiltinEnglish();
|
|
// Overlay translated strings onto the English base, but reject any value whose printf
|
|
// format-specifier signature differs from the English source (keep English instead).
|
|
// Many strings are used directly as printf/ImGui format strings, so a mismatched
|
|
// translation (dropped/changed specifier) is undefined behavior — this prevents it.
|
|
void overlayTranslations(const nlohmann::json& translations);
|
|
|
|
std::string current_locale_ = "en";
|
|
std::unordered_map<std::string, std::string> strings_;
|
|
std::vector<std::pair<std::string, std::string>> available_languages_;
|
|
};
|
|
|
|
/**
|
|
* @brief Convenience function for translation
|
|
* @param key The string key
|
|
* @return Translated string
|
|
*/
|
|
inline const char* tr(const char* key) {
|
|
return I18n::instance().translate(key);
|
|
}
|
|
|
|
} // namespace util
|
|
} // namespace dragonx
|
|
|
|
// Convenience macro for translations
|
|
#define TR(key) dragonx::util::tr(key)
|