Skip to content

Instantly share code, notes, and snippets.

@tadamatu
Last active December 10, 2018 10:57
Show Gist options
  • Save tadamatu/da2b04644e1e1ad4666c to your computer and use it in GitHub Desktop.
Save tadamatu/da2b04644e1e1ad4666c to your computer and use it in GitHub Desktop.
ローカライズ文字の取得関数
//[cocos2d-x]
//ローカライズ文字の取得関数
//searchKey 対象キー
//comment コメント(覚書などに利用)
//Return ローカライズ文字列
std::string LocalizedString(const char* searchKey, const char* comment){
std::string ret = comment;
static map<std::string, std::string> localizable;
if(localizable.empty()){
FileUtils* fileUtil = FileUtils::getInstance();
LanguageType language = Application::getInstance()->getCurrentLanguage();
if (language == LanguageType::JAPANESE) {
fileUtil->addSearchPath("ja.lproj");
if (language == LanguageType::★★★★) { //ローカライズしたいLanguageTypeを指定
fileUtil->addSearchPath("★★★★"); //LanguageTypeごとのフォルダを指定
} else {
fileUtil->addSearchPath("en.lproj");
}
//ファイル名を読み込み
std::string fullpath = fileUtil->fullPathForFilename("Localizable.strings");
std::string strings = fileUtil->getStringFromFile(fullpath);
if (strings == "") return ret;
//行ごとに処理
list<std::string> stringLines = split(strings, "\n");
for(list<std::string>::iterator begin = stringLines.begin(), end = stringLines.end(); begin != end; ++begin){
list<std::string> stringKeyValue = split(*begin, "=");
if(stringKeyValue.size() >= 2){
list<std::string>::iterator keyv = stringKeyValue.begin();
localizable.insert(pair<std::string, std::string>(*keyv, *(++keyv)));
}
}
}
map<std::string, std::string>::iterator iterator = localizable.find(searchKey);
if(iterator != localizable.end()){
ret = iterator->second.c_str();
}
//改行に対応(//nで改行として認識できる)
std::string result = stringReplace(ret, "\\n", "\n");
return result;
}
@tadamatu
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment