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; return _sharedJsonLocalizationManager;
} }
void JsonLocalizationManager::destroyInstance()
{
if (_sharedJsonLocalizationManager != nullptr)
{
delete _sharedJsonLocalizationManager;
_sharedJsonLocalizationManager = nullptr;
}
}
JsonLocalizationManager::JsonLocalizationManager() JsonLocalizationManager::JsonLocalizationManager()
:languageData(nullptr) :languageData(nullptr)
{ {
@ -58,6 +67,8 @@ std::string JsonLocalizationManager::getLocalizationString(std::string key)
return result; return result;
} }
static BinLocalizationManager* _sharedBinLocalizationManager = nullptr; static BinLocalizationManager* _sharedBinLocalizationManager = nullptr;
ILocalizationManager* BinLocalizationManager::getInstance() ILocalizationManager* BinLocalizationManager::getInstance()
@ -70,14 +81,21 @@ ILocalizationManager* BinLocalizationManager::getInstance()
return _sharedBinLocalizationManager; return _sharedBinLocalizationManager;
} }
void BinLocalizationManager::destroyInstance()
{
if (_sharedBinLocalizationManager != nullptr)
{
delete _sharedBinLocalizationManager;
_sharedBinLocalizationManager = nullptr;
}
}
BinLocalizationManager::BinLocalizationManager() BinLocalizationManager::BinLocalizationManager()
{ {
} }
BinLocalizationManager::~BinLocalizationManager() BinLocalizationManager::~BinLocalizationManager()
{ {
} }
bool BinLocalizationManager::initLanguageData(std::string file) bool BinLocalizationManager::initLanguageData(std::string file)
@ -122,18 +140,24 @@ std::string BinLocalizationManager::getLocalizationString(std::string key)
return result; return result;
} }
static ILocalizationManager* _sharedLocalizationManager = nullptr;
static bool isCurrentBinManager = true;
ILocalizationManager* LocalizationHelper::getCurrentManager() ILocalizationManager* LocalizationHelper::getCurrentManager()
{ {
if (!_sharedLocalizationManager) if (isCurrentBinManager)
{ return BinLocalizationManager::getInstance();
_sharedLocalizationManager = BinLocalizationManager::getInstance(); else
} return JsonLocalizationManager::getInstance();
return _sharedLocalizationManager;
} }
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: public:
static ILocalizationManager* getInstance(); static ILocalizationManager* getInstance();
static void destroyInstance();
JsonLocalizationManager();
~JsonLocalizationManager();
public: public:
/* Init manager with special localize json data file. /* Init manager with special localize json data file.
@ -45,6 +43,10 @@ namespace cocostudio {
*/ */
virtual std::string getLocalizationString(std::string key); virtual std::string getLocalizationString(std::string key);
protected:
JsonLocalizationManager();
~JsonLocalizationManager();
protected: protected:
rapidjson::Document * languageData; rapidjson::Document * languageData;
}; };
@ -53,9 +55,7 @@ namespace cocostudio {
{ {
public: public:
static ILocalizationManager* getInstance(); static ILocalizationManager* getInstance();
static void destroyInstance();
BinLocalizationManager();
~BinLocalizationManager();
/* Init manager with special localize binary data file. /* Init manager with special localize binary data file.
* @param file Name of localize file. * @param file Name of localize file.
@ -70,6 +70,10 @@ namespace cocostudio {
*/ */
virtual std::string getLocalizationString(std::string key); virtual std::string getLocalizationString(std::string key);
protected:
BinLocalizationManager();
~BinLocalizationManager();
protected: protected:
std::unordered_map<std::string, std::string> languageData; std::unordered_map<std::string, std::string> languageData;
}; };
@ -78,15 +82,22 @@ namespace cocostudio {
{ {
public: public:
/* Get current localization manager. /* Get current localization manager.
* @return If the manager has been set, return current localization manager, * @return The instance of current localization manager.
* otherwise return the singleton instance of BinLocalizationManager. * If the manager hasn't been set, the default manager will be BinLocalizationManager.
*/ */
static ILocalizationManager* getCurrentManager(); static ILocalizationManager* getCurrentManager();
/* Set current localization manager. /* 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 // 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"); lm->initLanguageData("your localize file name.lang.json");
cocostudio::LocalizationHelper::setCurrentManager(lm);
*/ */
// If you want to load binary localize data, use follow block // 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"); lm->initLanguageData("your localize file name.lang.csb");
cocostudio::LocalizationHelper::setCurrentManager(lm);
*/ */
// turn on display FPS // turn on display FPS

View File

@ -409,6 +409,12 @@ void ProjectConfig::parseCommandLine(const vector<string> &args)
vector<string> pathes = split((*it), ';'); vector<string> pathes = split((*it), ';');
setSearchPath(pathes); setSearchPath(pathes);
} }
else if (arg.compare("-language-data-path") == 0)
{
++it;
if (it == args.end()) break;
setLanguageDataPath(*it);
}
++it; ++it;
} }
@ -615,6 +621,22 @@ const vector<string> &ProjectConfig::getSearchPath() const
return _searchPath; 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 bool ProjectConfig::isAppMenu() const
{ {
return _isAppMenu; return _isAppMenu;

View File

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