2022-02-11 01:38:14 -05:00
|
|
|
#include "pch.h"
|
|
|
|
#include "locale.h"
|
|
|
|
#include <filesystem>
|
|
|
|
|
2022-03-04 01:08:06 -05:00
|
|
|
Locale::eReturnCodes Locale::Init(const char* path, const char* def, const char* fallback)
|
2022-02-11 01:38:14 -05:00
|
|
|
{
|
|
|
|
std::string localePath = path;
|
|
|
|
if (localePath.back() != '/')
|
|
|
|
{
|
|
|
|
localePath += '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _GTA_
|
|
|
|
m_path = PLUGIN_PATH((char*)localePath.c_str());
|
|
|
|
#else
|
|
|
|
m_path = localePath;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!std::filesystem::exists(m_path))
|
|
|
|
{
|
|
|
|
#ifdef _GTA_
|
|
|
|
gLog << "Locale directory doesn't exist" << std::endl;
|
|
|
|
#endif
|
|
|
|
return eReturnCodes::DIR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the list of available languages
|
|
|
|
We won't load them here, we'll load them when we need them
|
|
|
|
*/
|
|
|
|
#ifdef _GTA_
|
|
|
|
gLog << "Loading languages..." << std::endl;
|
|
|
|
#endif
|
|
|
|
for (auto& entry : std::filesystem::directory_iterator(m_path))
|
|
|
|
{
|
|
|
|
if (entry.path().extension() == ".json")
|
|
|
|
{
|
2022-03-03 15:55:19 -05:00
|
|
|
std::string fileName = entry.path().stem().string();
|
2022-02-11 01:38:14 -05:00
|
|
|
#ifdef _GTA_
|
2022-03-03 15:55:19 -05:00
|
|
|
gLog << "Found locale: " << fileName << std::endl;
|
2022-02-11 01:38:14 -05:00
|
|
|
#endif
|
2022-03-03 15:55:19 -05:00
|
|
|
m_locales.push_back(fileName);
|
|
|
|
|
2022-03-04 01:08:06 -05:00
|
|
|
if (!strcmp(fallback, fileName.c_str()))
|
2022-03-03 15:55:19 -05:00
|
|
|
{
|
|
|
|
std::string localePath = m_path + fileName + ".json";
|
|
|
|
|
|
|
|
if(m_pCallbackJson)
|
|
|
|
{
|
|
|
|
delete m_pCallbackJson;
|
|
|
|
m_pCallbackJson = nullptr;
|
|
|
|
}
|
|
|
|
m_pCallbackJson = new CJson(localePath.c_str(), true);
|
|
|
|
}
|
2022-02-11 01:38:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeof(m_locales) == 0)
|
|
|
|
{
|
|
|
|
#ifdef _GTA_
|
|
|
|
gLog << "No language files found" << std::endl;
|
|
|
|
#endif
|
|
|
|
return eReturnCodes::NO_LOCALE_FOUND;
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:35:08 -05:00
|
|
|
|
|
|
|
// Look for default language and set it
|
|
|
|
std::vector<std::string>& vec = Locale::GetLocaleList();
|
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
for (std::string& locale : vec)
|
|
|
|
{
|
|
|
|
if (locale == def)
|
|
|
|
{
|
|
|
|
Locale::SetLocale(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_pJson)
|
|
|
|
{
|
|
|
|
#ifdef _GTA_
|
|
|
|
gLog << "Failed to load default language." << std::endl;
|
|
|
|
#endif
|
|
|
|
return eReturnCodes::DEF_LOCALE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2022-02-11 01:38:14 -05:00
|
|
|
return eReturnCodes::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>& Locale::GetLocaleList()
|
|
|
|
{
|
|
|
|
return m_locales;
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:35:08 -05:00
|
|
|
size_t Locale::GetCurrentLocaleIndex()
|
2022-02-11 01:38:14 -05:00
|
|
|
{
|
2022-02-11 16:35:08 -05:00
|
|
|
return localeIndex;
|
2022-02-11 01:38:14 -05:00
|
|
|
}
|
|
|
|
|
2022-02-19 13:32:25 -05:00
|
|
|
Locale::eReturnCodes Locale::SetLocale(size_t index)
|
2022-02-11 01:38:14 -05:00
|
|
|
{
|
|
|
|
if(m_pJson)
|
|
|
|
{
|
|
|
|
delete m_pJson;
|
|
|
|
m_pJson = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index < 0 || index >= m_locales.size())
|
|
|
|
{
|
|
|
|
return eReturnCodes::INVALID_INDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string localeFile = m_locales[index];
|
|
|
|
localeFile += ".json";
|
|
|
|
std::string localePath = m_path + localeFile;
|
2022-02-11 16:35:08 -05:00
|
|
|
m_pJson = new CJson(localePath.c_str(), true);
|
|
|
|
localeIndex = index;
|
2022-02-11 01:38:14 -05:00
|
|
|
return eReturnCodes::SUCCESS;
|
|
|
|
}
|
|
|
|
|