2016-03-20 21:53:44 +08:00
|
|
|
#include "editor-support/cocostudio/LocalizationManager.h"
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
2016-02-26 17:15:24 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "editor-support/cocostudio/CSLanguageDataBinary_generated.h"
|
|
|
|
|
|
|
|
using namespace cocostudio;
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
2016-02-29 18:00:14 +08:00
|
|
|
static JsonLocalizationManager* _sharedJsonLocalizationManager = nullptr;
|
|
|
|
|
2016-03-01 14:27:12 +08:00
|
|
|
ILocalizationManager* JsonLocalizationManager::getInstance()
|
2016-02-29 18:00:14 +08:00
|
|
|
{
|
|
|
|
if (!_sharedJsonLocalizationManager)
|
|
|
|
{
|
|
|
|
_sharedJsonLocalizationManager = new (std::nothrow) JsonLocalizationManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedJsonLocalizationManager;
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:49:50 +08:00
|
|
|
void JsonLocalizationManager::destroyInstance()
|
|
|
|
{
|
|
|
|
if (_sharedJsonLocalizationManager != nullptr)
|
|
|
|
{
|
|
|
|
delete _sharedJsonLocalizationManager;
|
|
|
|
_sharedJsonLocalizationManager = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 17:15:24 +08:00
|
|
|
JsonLocalizationManager::JsonLocalizationManager()
|
|
|
|
:languageData(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (languageData && languageData->HasMember(key.c_str()) &&
|
|
|
|
(*languageData)[key.c_str()].IsString())
|
|
|
|
result = (*languageData)[key.c_str()].GetString();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:49:50 +08:00
|
|
|
|
|
|
|
|
2016-02-29 18:00:14 +08:00
|
|
|
static BinLocalizationManager* _sharedBinLocalizationManager = nullptr;
|
|
|
|
|
2016-03-01 14:27:12 +08:00
|
|
|
ILocalizationManager* BinLocalizationManager::getInstance()
|
2016-02-29 18:00:14 +08:00
|
|
|
{
|
|
|
|
if (!_sharedBinLocalizationManager)
|
|
|
|
{
|
|
|
|
_sharedBinLocalizationManager = new (std::nothrow) BinLocalizationManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedBinLocalizationManager;
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:49:50 +08:00
|
|
|
void BinLocalizationManager::destroyInstance()
|
2016-02-26 17:15:24 +08:00
|
|
|
{
|
2016-03-03 15:49:50 +08:00
|
|
|
if (_sharedBinLocalizationManager != nullptr)
|
|
|
|
{
|
|
|
|
delete _sharedBinLocalizationManager;
|
|
|
|
_sharedBinLocalizationManager = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 17:15:24 +08:00
|
|
|
|
2016-03-03 15:49:50 +08:00
|
|
|
BinLocalizationManager::BinLocalizationManager()
|
|
|
|
{
|
2016-02-26 17:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BinLocalizationManager::~BinLocalizationManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
auto items = lanSet->languageItems();
|
|
|
|
int count = items->size();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
auto it = items->Get(i);
|
|
|
|
std::string key = it->key()->c_str();
|
|
|
|
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;
|
|
|
|
}
|
2016-03-01 14:27:12 +08:00
|
|
|
|
2016-03-03 15:49:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
static bool isCurrentBinManager = true;
|
2016-03-03 16:19:16 +08:00
|
|
|
static ILocalizationManager* _sharedLocalizationManager = nullptr;
|
2016-03-01 14:27:12 +08:00
|
|
|
|
|
|
|
ILocalizationManager* LocalizationHelper::getCurrentManager()
|
|
|
|
{
|
2016-03-03 16:19:16 +08:00
|
|
|
if (!_sharedLocalizationManager)
|
|
|
|
{
|
|
|
|
_sharedLocalizationManager = BinLocalizationManager::getInstance();
|
|
|
|
isCurrentBinManager = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sharedLocalizationManager;
|
2016-03-03 15:49:50 +08:00
|
|
|
}
|
|
|
|
|
2016-03-03 16:19:16 +08:00
|
|
|
void LocalizationHelper::setCurrentManager(ILocalizationManager* manager, bool isBinary)
|
2016-03-03 15:49:50 +08:00
|
|
|
{
|
2016-03-03 16:19:16 +08:00
|
|
|
_sharedLocalizationManager = manager;
|
2016-03-03 15:49:50 +08:00
|
|
|
isCurrentBinManager = isBinary;
|
2016-03-01 14:27:12 +08:00
|
|
|
}
|
|
|
|
|
2016-03-03 15:49:50 +08:00
|
|
|
bool LocalizationHelper::isBinManager()
|
2016-03-01 14:27:12 +08:00
|
|
|
{
|
2016-03-03 15:49:50 +08:00
|
|
|
return isCurrentBinManager;
|
2016-03-01 14:27:12 +08:00
|
|
|
}
|