1.Change the interface in LocalizationManager.

2.Add new args of localization in simulator.
This commit is contained in:
beichen.liu 2016-03-03 15:49:50 +08:00
parent 05411a44f5
commit 0489fd76e8
5 changed files with 83 additions and 24 deletions

View File

@ -18,6 +18,15 @@ ILocalizationManager* JsonLocalizationManager::getInstance()
return _sharedJsonLocalizationManager;
}
void JsonLocalizationManager::destroyInstance()
{
if (_sharedJsonLocalizationManager != nullptr)
{
delete _sharedJsonLocalizationManager;
_sharedJsonLocalizationManager = nullptr;
}
}
JsonLocalizationManager::JsonLocalizationManager()
:languageData(nullptr)
{
@ -58,6 +67,8 @@ std::string JsonLocalizationManager::getLocalizationString(std::string key)
return result;
}
static BinLocalizationManager* _sharedBinLocalizationManager = nullptr;
ILocalizationManager* BinLocalizationManager::getInstance()
@ -70,14 +81,21 @@ ILocalizationManager* BinLocalizationManager::getInstance()
return _sharedBinLocalizationManager;
}
void BinLocalizationManager::destroyInstance()
{
if (_sharedBinLocalizationManager != nullptr)
{
delete _sharedBinLocalizationManager;
_sharedBinLocalizationManager = nullptr;
}
}
BinLocalizationManager::BinLocalizationManager()
{
}
BinLocalizationManager::~BinLocalizationManager()
{
}
bool BinLocalizationManager::initLanguageData(std::string file)
@ -122,18 +140,24 @@ std::string BinLocalizationManager::getLocalizationString(std::string key)
return result;
}
static ILocalizationManager* _sharedLocalizationManager = nullptr;
static bool isCurrentBinManager = true;
ILocalizationManager* LocalizationHelper::getCurrentManager()
{
if (!_sharedLocalizationManager)
{
_sharedLocalizationManager = BinLocalizationManager::getInstance();
}
return _sharedLocalizationManager;
if (isCurrentBinManager)
return BinLocalizationManager::getInstance();
else
return JsonLocalizationManager::getInstance();
}
void LocalizationHelper::setCurrentManager(ILocalizationManager* manager)
void LocalizationHelper::setCurrentManager(bool isBinary)
{
_sharedLocalizationManager = manager;
isCurrentBinManager = isBinary;
}
bool LocalizationHelper::isBinManager()
{
return isCurrentBinManager;
}

View File

@ -27,9 +27,7 @@ namespace cocostudio {
{
public:
static ILocalizationManager* getInstance();
JsonLocalizationManager();
~JsonLocalizationManager();
static void destroyInstance();
public:
/* Init manager with special localize json data file.
@ -45,6 +43,10 @@ namespace cocostudio {
*/
virtual std::string getLocalizationString(std::string key);
protected:
JsonLocalizationManager();
~JsonLocalizationManager();
protected:
rapidjson::Document * languageData;
};
@ -53,9 +55,7 @@ namespace cocostudio {
{
public:
static ILocalizationManager* getInstance();
BinLocalizationManager();
~BinLocalizationManager();
static void destroyInstance();
/* Init manager with special localize binary data file.
* @param file Name of localize file.
@ -70,6 +70,10 @@ namespace cocostudio {
*/
virtual std::string getLocalizationString(std::string key);
protected:
BinLocalizationManager();
~BinLocalizationManager();
protected:
std::unordered_map<std::string, std::string> languageData;
};
@ -78,15 +82,22 @@ namespace cocostudio {
{
public:
/* Get current localization manager.
* @return If the manager has been set, return current localization manager,
* otherwise return the singleton instance of BinLocalizationManager.
* @return The instance of current localization manager.
* If the manager hasn't been set, the default manager will be BinLocalizationManager.
*/
static ILocalizationManager* getCurrentManager();
/* Set current localization manager.
* @param manager Instance of current localization manager.
* @param isBinary If the param is true, current manager will be set to BinLocalizationManager.
* If the param is false, current manager will be set to JsonLocalizationManager.
*/
static void setCurrentManager(ILocalizationManager* manager);
static void setCurrentManager(bool isBinary);
/* Get the type of current localization manager.
* @return If current manager is BinLocalizationManager, return true.
* Otherwise return false, that means current manager is JsonLocalizationManager.
*/
static bool isBinManager();
};
}

View File

@ -79,16 +79,16 @@ bool AppDelegate::applicationDidFinishLaunching() {
// If you want to load json localize data, use follow block
/*
cocostudio::JsonLocalizationManager * lm = cocostudio::JsonLocalizationManager::getInstance();
cocostudio::LocalizationHelper::setCurrentManager(false);
cocostudio::ILocalizationManager * lm = cocostudio::LocalizationHelper::getCurrentManager();
lm->initLanguageData("your localize file name.lang.json");
cocostudio::LocalizationHelper::setCurrentManager(lm);
*/
// If you want to load binary localize data, use follow block
/*
cocostudio::BinLocalizationManager * lm = cocostudio::BinLocalizationManager::getInstance();
cocostudio::LocalizationHelper::setCurrentManager(true);
cocostudio::ILocalizationManager * lm = cocostudio::LocalizationHelper::getCurrentManager();
lm->initLanguageData("your localize file name.lang.csb");
cocostudio::LocalizationHelper::setCurrentManager(lm);
*/
// turn on display FPS

View File

@ -409,6 +409,12 @@ void ProjectConfig::parseCommandLine(const vector<string> &args)
vector<string> pathes = split((*it), ';');
setSearchPath(pathes);
}
else if (arg.compare("-language-data-path") == 0)
{
++it;
if (it == args.end()) break;
setLanguageDataPath(*it);
}
++it;
}
@ -615,6 +621,22 @@ const vector<string> &ProjectConfig::getSearchPath() const
return _searchPath;
}
void ProjectConfig::setLanguageDataPath(const std::string &filePath)
{
bool isBinary = true;
string jsonExtension = ".json";
int exLength = jsonExtension.length();
if (filePath.length() >= exLength &&
(0 == filePath.compare(filePath.length() - exLength, exLength, jsonExtension)))
{
isBinary = false;
}
cocostudio::LocalizationHelper::setCurrentManager(isBinary);
cocostudio::ILocalizationManager* lm = cocostudio::LocalizationHelper::getCurrentManager();
lm->initLanguageData(filePath);
}
bool ProjectConfig::isAppMenu() const
{
return _isAppMenu;

View File

@ -104,6 +104,7 @@ public:
const std::string &getBindAddress() const;
void setSearchPath(const vector<string> &args);
const vector<string> &getSearchPath() const;
void setLanguageDataPath(const std::string &filePath);
bool isAppMenu() const;
bool isResizeWindow() const;
@ -133,6 +134,7 @@ private:
int _fileUploadPort;
string _bindAddress;
vector<string> _searchPath;
string _languageDataPath;
void normalize();
string replaceProjectDirToMacro(const string &path) const;