I. cocos2d-x built-in code Introduction
Note that the localization of the cocos2d-x needs to be tested on a real machine. It is not integrated with the solution and needs to be designed by itself. The only function provided is as follows:
Definition in cccomment. h:
typedef enum LanguageType{ kLanguageEnglish = 0, kLanguageChinese, kLanguageFrench, kLanguageItalian, kLanguageGerman, kLanguageSpanish, kLanguageRussian, kLanguageKorean} ccLanguageType;
Ccapplication. cpp is defined as follows:
#ifndef _CURRENT_LANGUAGE_TEST_H_#define _CURRENT_LANGUAGE_TEST_H_#include "cocos2d.h"#include "../testBasic.h"class CurrentLanguageTest : public CCLayer{public: CurrentLanguageTest();};class CurrentLanguageTestScene : public TestScene{public: virtual void runThisTest();};#endif // _CURRENT_LANGUAGE_TEST_H_
ccLanguageType CCApplication::getCurrentLanguage(){ ccLanguageType ret = kLanguageEnglish; LCID localeID = GetUserDefaultLCID(); unsigned short primaryLanguageID = localeID & 0xFF; switch (primaryLanguageID) { case LANG_CHINESE: ret = kLanguageChinese; break; case LANG_ENGLISH: ret = kLanguageEnglish; break; case LANG_FRENCH: ret = kLanguageFrench; break; case LANG_ITALIAN: ret = kLanguageItalian; break; case LANG_GERMAN: ret = kLanguageGerman; break; case LANG_SPANISH: ret = kLanguageSpanish; break; case LANG_RUSSIAN: ret = kLanguageRussian; break; case LANG_KOREAN: ret = kLanguageKorean; break; } return ret;}
Usage:
ccLanguageType currentLanguageType = CCApplication::sharedApplication()->getCurrentLanguage();
The testcpp project has a test instance. For more information, see the code currentcancagetest. h/CPP.
#ifndef _CURRENT_LANGUAGE_TEST_H_#define _CURRENT_LANGUAGE_TEST_H_#include "cocos2d.h"#include "../testBasic.h"class CurrentLanguageTest : public CCLayer{public: CurrentLanguageTest();};class CurrentLanguageTestScene : public TestScene{public: virtual void runThisTest();};#endif // _CURRENT_LANGUAGE_TEST_H_
#include "CurrentLanguageTest.h"CurrentLanguageTest::CurrentLanguageTest(){ CCLabelTTF* label = CCLabelTTF::create("Current language Test", "Arial", 28); addChild(label, 0); label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-50) ); CCLabelTTF *labelLanguage = CCLabelTTF::create("", "Arial", 20); labelLanguage->setPosition(VisibleRect::center()); ccLanguageType currentLanguageType = CCApplication::sharedApplication()->getCurrentLanguage(); switch (currentLanguageType) { case kLanguageEnglish: labelLanguage->setString("current language is English"); break; case kLanguageChinese: labelLanguage->setString("current language is Chinese"); break; case kLanguageFrench: labelLanguage->setString("current language is French"); break; case kLanguageGerman: labelLanguage->setString("current language is German"); break; case kLanguageItalian: labelLanguage->setString("current language is Italian"); break; case kLanguageRussian: labelLanguage->setString("current language is Russian"); break; case kLanguageSpanish: labelLanguage->setString("current language is Spanish"); break;case kLanguageKorean: labelLanguage->setString("current language is Korean"); break; } addChild(labelLanguage);}void CurrentLanguageTestScene::runThisTest(){ CCLayer* pLayer = new CurrentLanguageTest(); addChild(pLayer); CCDirector::sharedDirector()->replaceScene(this); pLayer->release();}
Ii. Resource file (including images) adaptation method
Specify the directory for reading resources and read from it later, such as Chinese and English. Some common resources can be placed under the resource. The program cannot be found in hd_xx and will be searched under the resource.
CCFileUtils::sharedFileUtils()->setResourceDirectory("hd_ch");CCFileUtils::sharedFileUtils()->setResourceDirectory("hd_en");
Iii. String resource adaptation method
Put strings in the resource folder for processing as resources
{
"Name": "nickname"
"Interest": "basketball"
}
{
"Name": "Ming"
"Interest": "basketball"
}
Save the preceding files to the same file name and different resource paths. Then, read the data by using the following methods, provided that the resource path has been set (refer to section 2 of this Article ).
unsigned long size ;char* file = (char*)CCFileUtils::sharedFileUtils()->getFileData("test.json","r",&size);Json::Reader reader;Json::Value root;if (!reader.parse(std::string(file),root,false)){return;}std::string name = root["name"].asString();std::string interest = root["interest"].asString();
Recommended Singleton Mode
The above code is jsoncpp Code. For the configuration method, refer to the previous article. The configuration method is simple. You only need to import a third-party library file to your project.
JSON library configuration and usage
Of course, you can also use XML for implementation. Refer to the configuration method of tinyxml:
Http://blog.csdn.net/xzongyuan/article/details/9157555
Finally, I found a reference article, but I have not read it carefully.
Http://blog.csdn.net/zyxhjy/article/details/8318529