Translations.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "Translations.h"
  2. #include "../functions/Settings/Settings.h"
  3. #include <optional>
  4. #include <charconv>
  5. std::string result;
  6. static bool ifParse;
  7. int LangTR = 0;
  8. std::string LoadResourceString(int resourceId, LPCSTR resourceType) {
  9. HMODULE handle = GetModuleHandle("minty.dll");
  10. HRSRC resource = FindResource(handle, MAKEINTRESOURCE(resourceId), resourceType);
  11. if (!resource) {
  12. LOG_ERROR("Resource not found.");
  13. return "";
  14. }
  15. HGLOBAL loadedResource = LoadResource(handle, resource);
  16. if (!loadedResource) {
  17. LOG_ERROR("Failed to load resource.");
  18. return "";
  19. }
  20. DWORD size = SizeofResource(handle, resource);
  21. LPCSTR data = static_cast<LPCSTR>(LockResource(loadedResource));
  22. if (size == 0 || !data) {
  23. LOG_ERROR("Resource data is invalid.");
  24. return "";
  25. }
  26. return std::string(data, size);
  27. }
  28. auto& settings = cheat::Settings::getInstance();
  29. std::optional<std::string> safeGet(nlohmann::json& json, const std::string& key, const std::string& lang) {
  30. if (json.contains(lang) && json[lang].is_object() && json[lang].contains(key))
  31. return json[lang][key].get<std::string>();
  32. return key;
  33. }
  34. std::u8string u8result;
  35. enum class Lang {
  36. EN,
  37. RU,
  38. ID,
  39. RO
  40. };
  41. const char* _(const char* code) {
  42. static nlohmann::json trJson;
  43. if (result.empty()) {
  44. result = LoadResourceString(R_LANGUAGES, RT_RCDATA);
  45. if (result.empty())
  46. return code;
  47. try {
  48. trJson = nlohmann::json::parse(result);
  49. }
  50. catch (nlohmann::json::parse_error& e) {
  51. LOG_ERROR("JSON parsing error: %s", e.what());
  52. return code;
  53. }
  54. }
  55. std::string retStr = code;
  56. int languageValue = settings.f_Language.getValue();
  57. std::optional<std::string> ret = safeGet(trJson, code, magic_enum::enum_name(static_cast<Lang>(languageValue)).data());
  58. if (ret.has_value())
  59. retStr = ret.value();
  60. u8result = std::u8string(retStr.begin(), retStr.end());
  61. return reinterpret_cast<const char*>(u8result.c_str());
  62. }