2015-01-07 17:50:15 +08:00
|
|
|
//
|
|
|
|
// AppLang.cpp
|
|
|
|
// Simulator
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AppLang.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
AppLang::AppLang()
|
|
|
|
: _hasInit(false)
|
|
|
|
{
|
|
|
|
_localizationFileName = "lang";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppLang::readLocalizationFile()
|
|
|
|
{
|
|
|
|
if (!_hasInit)
|
|
|
|
{
|
|
|
|
_hasInit = true;
|
|
|
|
|
2015-04-02 18:27:53 +08:00
|
|
|
auto fileUtils = FileUtils::getInstance();
|
|
|
|
|
|
|
|
if (!fileUtils->isFileExist(_localizationFileName))
|
2015-01-07 17:50:15 +08:00
|
|
|
{
|
2015-04-02 18:27:53 +08:00
|
|
|
cocos2d::log("[WARNING]:not find %s", _localizationFileName.c_str());
|
2015-01-07 17:50:15 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-04-02 18:27:53 +08:00
|
|
|
auto fullFilePath = fileUtils->fullPathForFilename(_localizationFileName);
|
|
|
|
std::string fileContent = FileUtils::getInstance()->getStringFromFile(fullFilePath);
|
2015-01-07 17:50:15 +08:00
|
|
|
if(fileContent.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_docRootjson.Parse<0>(fileContent.c_str()).HasParseError())
|
|
|
|
{
|
2015-05-25 11:43:03 +08:00
|
|
|
cocos2d::log("[WARNING]:read json file %s failed because of %d", _localizationFileName.c_str(), _docRootjson.GetParseError());
|
2015-01-07 17:50:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AppLang* AppLang::getInstance()
|
|
|
|
{
|
|
|
|
static AppLang *lang = nullptr;
|
|
|
|
if (!lang)
|
|
|
|
{
|
|
|
|
lang = new AppLang;
|
|
|
|
lang->readLocalizationFile();
|
|
|
|
}
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AppLang::getString(const std::string &lang, const std::string &key)
|
|
|
|
{
|
|
|
|
std::string tmpKey = key;
|
|
|
|
const char *ckey = tmpKey.c_str();
|
|
|
|
|
|
|
|
std::string tmpLang = lang;
|
|
|
|
const char *langKey = tmpLang.c_str();
|
|
|
|
|
|
|
|
if (!_docRootjson.IsObject())
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_docRootjson.HasMember(langKey))
|
|
|
|
{
|
|
|
|
const rapidjson::Value& v = _docRootjson[langKey];
|
|
|
|
if (v.HasMember(ckey))
|
2015-04-02 18:27:53 +08:00
|
|
|
{
|
2015-01-07 17:50:15 +08:00
|
|
|
return v[ckey].GetString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|