2020-10-21 10:12:00 +08:00
|
|
|
#include "LocalizationManager.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CSLanguageDataBinary_generated.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
using namespace cocostudio;
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
static JsonLocalizationManager* _sharedJsonLocalizationManager = nullptr;
|
|
|
|
|
|
|
|
ILocalizationManager* JsonLocalizationManager::getInstance()
|
|
|
|
{
|
|
|
|
if (!_sharedJsonLocalizationManager)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sharedJsonLocalizationManager = new JsonLocalizationManager();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedJsonLocalizationManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonLocalizationManager::destroyInstance()
|
|
|
|
{
|
|
|
|
if (_sharedJsonLocalizationManager != nullptr)
|
|
|
|
{
|
|
|
|
delete _sharedJsonLocalizationManager;
|
|
|
|
_sharedJsonLocalizationManager = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
JsonLocalizationManager::JsonLocalizationManager() : languageData(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
JsonLocalizationManager::~JsonLocalizationManager()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(languageData);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonLocalizationManager::initLanguageData(std::string file)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
std::string data = FileUtils::getInstance()->getStringFromFile(file);
|
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
if (!languageData)
|
|
|
|
languageData = new rapidjson::Document;
|
|
|
|
languageData->Parse(data.c_str());
|
|
|
|
if (languageData->IsObject())
|
|
|
|
result = true;
|
|
|
|
else
|
|
|
|
CC_SAFE_DELETE(languageData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string JsonLocalizationManager::getLocalizationString(std::string key)
|
|
|
|
{
|
|
|
|
std::string result = key;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (languageData && languageData->HasMember(key.c_str()) && (*languageData)[key.c_str()].IsString())
|
2019-11-23 20:27:39 +08:00
|
|
|
result = (*languageData)[key.c_str()].GetString();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BinLocalizationManager* _sharedBinLocalizationManager = nullptr;
|
|
|
|
|
|
|
|
ILocalizationManager* BinLocalizationManager::getInstance()
|
|
|
|
{
|
|
|
|
if (!_sharedBinLocalizationManager)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sharedBinLocalizationManager = new BinLocalizationManager();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedBinLocalizationManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinLocalizationManager::destroyInstance()
|
|
|
|
{
|
|
|
|
if (_sharedBinLocalizationManager != nullptr)
|
|
|
|
{
|
|
|
|
delete _sharedBinLocalizationManager;
|
|
|
|
_sharedBinLocalizationManager = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
BinLocalizationManager::BinLocalizationManager() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
BinLocalizationManager::~BinLocalizationManager() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool BinLocalizationManager::initLanguageData(std::string file)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
languageData.clear();
|
|
|
|
|
|
|
|
Data buf = FileUtils::getInstance()->getDataFromFile(file);
|
|
|
|
if (!buf.isNull())
|
|
|
|
{
|
|
|
|
auto lanSet = flatbuffers::GetLanguageSet(buf.getBytes());
|
|
|
|
if (lanSet && lanSet->languageItems()->size() > 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
result = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto items = lanSet->languageItems();
|
2021-12-25 10:04:45 +08:00
|
|
|
int count = items->size();
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = items->Get(i);
|
|
|
|
std::string key = it->key()->c_str();
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string value = it->value()->c_str();
|
|
|
|
if (!key.empty())
|
|
|
|
languageData[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BinLocalizationManager::getLocalizationString(std::string key)
|
|
|
|
{
|
|
|
|
std::string result = key;
|
|
|
|
|
|
|
|
if (!languageData.empty())
|
|
|
|
{
|
|
|
|
auto it = languageData.find(key);
|
|
|
|
if (it != languageData.end())
|
|
|
|
result = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static bool isCurrentBinManager = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
static ILocalizationManager* _sharedLocalizationManager = nullptr;
|
|
|
|
|
|
|
|
ILocalizationManager* LocalizationHelper::getCurrentManager()
|
|
|
|
{
|
|
|
|
if (!_sharedLocalizationManager)
|
|
|
|
{
|
|
|
|
_sharedLocalizationManager = BinLocalizationManager::getInstance();
|
2021-12-25 10:04:45 +08:00
|
|
|
isCurrentBinManager = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return _sharedLocalizationManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalizationHelper::setCurrentManager(ILocalizationManager* manager, bool isBinary)
|
|
|
|
{
|
|
|
|
_sharedLocalizationManager = manager;
|
2021-12-25 10:04:45 +08:00
|
|
|
isCurrentBinManager = isBinary;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalizationHelper::isBinManager()
|
|
|
|
{
|
|
|
|
return isCurrentBinManager;
|
|
|
|
}
|