2023-04-07 23:08:34 +02:00
|
|
|
#pragma once
|
|
|
|
#include "file_manager/folder.hpp"
|
|
|
|
#include "gta/joaat.hpp"
|
|
|
|
#include "local_index.hpp"
|
|
|
|
#include "remote_index.hpp"
|
|
|
|
|
2023-06-06 07:40:40 +00:00
|
|
|
#include <cpr/response.h>
|
|
|
|
|
2023-04-07 23:08:34 +02:00
|
|
|
namespace big
|
|
|
|
{
|
|
|
|
using translation_map = std::unordered_map<rage::joaat_t, std::string>;
|
|
|
|
|
|
|
|
class translation_service
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
translation_service();
|
|
|
|
virtual ~translation_service() = default;
|
|
|
|
translation_service(const translation_service&) = delete;
|
|
|
|
translation_service(translation_service&&) noexcept = delete;
|
|
|
|
translation_service& operator=(const translation_service&) = delete;
|
|
|
|
translation_service& operator=(translation_service&&) noexcept = delete;
|
|
|
|
|
|
|
|
void init();
|
|
|
|
|
|
|
|
std::string_view get_translation(const std::string_view translation_key) const;
|
2023-07-01 22:25:40 +00:00
|
|
|
std::string_view get_translation(const rage::joaat_t translation_key, const std::string_view fallback = {0, 0}) const;
|
2023-04-07 23:08:34 +02:00
|
|
|
|
|
|
|
std::map<std::string, translation_entry>& available_translations();
|
|
|
|
const std::string& current_language_pack();
|
|
|
|
void select_language_pack(const std::string& pack_id);
|
|
|
|
void update_language_packs();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void load_translations();
|
2023-07-01 22:25:40 +00:00
|
|
|
bool does_language_exist(const std::string_view language);
|
2023-04-07 23:08:34 +02:00
|
|
|
nlohmann::json load_translation(const std::string_view pack_id);
|
|
|
|
|
|
|
|
bool download_language_pack(const std::string_view pack_id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Downloads the remote index to compare with our local index
|
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
* @return false
|
|
|
|
*/
|
|
|
|
bool download_index();
|
|
|
|
bool load_local_index();
|
|
|
|
void save_local_index();
|
|
|
|
/**
|
|
|
|
* @brief Attempts to load the remote from the local index fallback
|
|
|
|
*/
|
|
|
|
void use_fallback_remote();
|
2023-06-06 07:40:40 +00:00
|
|
|
cpr::Response download_file(const std::string& filename);
|
2023-04-07 23:08:34 +02:00
|
|
|
|
2023-07-01 22:25:40 +00:00
|
|
|
void try_set_default_language();
|
|
|
|
|
2023-04-07 23:08:34 +02:00
|
|
|
private:
|
|
|
|
const std::string m_url;
|
2023-06-06 07:40:40 +00:00
|
|
|
const std::string m_fallback_url;
|
|
|
|
|
2023-04-07 23:08:34 +02:00
|
|
|
std::unique_ptr<folder> m_translation_directory;
|
|
|
|
local_index m_local_index;
|
|
|
|
remote_index m_remote_index;
|
|
|
|
|
|
|
|
translation_map m_translations;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline auto g_translation_service = translation_service();
|
|
|
|
|
|
|
|
template<std::size_t N>
|
|
|
|
struct TranslationLiteral
|
|
|
|
{
|
|
|
|
rage::joaat_t m_hash;
|
|
|
|
char m_key[N]{};
|
|
|
|
|
|
|
|
consteval TranslationLiteral(char const (&pp)[N])
|
|
|
|
{
|
|
|
|
std::ranges::copy(pp, m_key);
|
|
|
|
m_hash = rage::joaat(pp);
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string_view translation() const
|
|
|
|
{
|
|
|
|
if (const auto translation = g_translation_service.get_translation(m_hash); translation.length())
|
|
|
|
return translation;
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<TranslationLiteral T>
|
|
|
|
constexpr auto operator"" _T()
|
|
|
|
{
|
|
|
|
return T.translation();
|
|
|
|
}
|
2023-02-01 19:46:33 +01:00
|
|
|
}
|