cocos2d-x项目在wp8下中文显示不了。有两个原因:
1. 本身字符串的utf8编码, 有问题。
2. 显示中文所须要的字体,缺失。
要解决第二个问题非常easy,在fonts下加入一个中文字体。在显示的时候制定此中文字体
labelArcade = CCLabelTTF::create("中文字符串", "中文字体名称",字体大小);
至于第一个问题,说的是不能像上文那样直接用字符串写“中文字符串”,解决方式有gb2312ToUnicode的方法,小转换一下子。百度一搜有非常多演示样例代码,恕不赘言,由于这样的办法并非太好--总是要做多语言的嘛。我们本来也不会直接写上中文字符串在那里。
所以。我们建一个文件。专门放置中文字符(还有对应的专门放置英语、法语、西班牙语的文件),然后通过读取文件的方式,读过来的字符串自然已经utf8编码过:
labelArcade = CCLabelTTF::create(localizeUtil::getLocalizeStringByKey("mode_arcade"), localizeUtil::getLocalizeFontName(), localizeUtil::getFontSizeH2());
另附localizeUtil的实现:
localizeUtil.h
class localizeUtil { public: static bool init(); static const char* getLocalizeStringByKey(std::string key); static const char* getLocalizeFontName(); static const float getFontSizeH1(); static const float getFontSizeH2(); static const float getFontSizeH3(); static const float getFontSizeH4(); static const float getFontSizeH5(); };
localizeUtil.cpp
#include "localizeUtil.h" #include "cocos2d.h" USING_NS_CC; static char* fontName; static char* fontNameChinese; static char* fontNameEnglish; static char* languageMappingFolder; static char* resourcePath; const char* localizeUtil::getLocalizeStringByKey(std::string key) { CCDictionary *chnStrings = CCDictionary::createWithContentsOfFile(resourcePath); const char *value = ((CCString*)chnStrings->objectForKey(key))->getCString(); return value; } const char* localizeUtil::getLocalizeFontName() { return fontName; } const float localizeUtil::getFontSizeH1() { return 50; } const float localizeUtil::getFontSizeH2() { return 40; } const float localizeUtil::getFontSizeH3() { return 30; } const float localizeUtil::getFontSizeH4() { return 20; } const float localizeUtil::getFontSizeH5() { return 16; } bool localizeUtil::init() { fontNameChinese = "SimpleGB2312"; fontNameEnglish = "Arial"; resourcePath = "local/s%/strings.xml"; const char* lc = CCApplication::getInstance()->getCurrentLanguageCode(); ccLanguageType currentLanguageType = Application::getInstance()->getCurrentLanguage(); switch (currentLanguageType) { case kLanguageChinese: fontName = fontNameChinese; languageMappingFolder = "zhcn"; resourcePath = "local/zhcn/strings.xml"; break; case kLanguageEnglish: fontName = fontNameEnglish; languageMappingFolder = "enus"; resourcePath = "local/enus/strings.xml"; break; case kLanguageSpanish: fontName = fontNameEnglish; languageMappingFolder = "es"; resourcePath = "local/es/strings.xml"; break; case kLanguageFrench: fontName = fontNameEnglish; languageMappingFolder = "fr"; resourcePath = "local/fr/strings.xml"; break; default: fontName = fontNameEnglish; languageMappingFolder = "enus"; resourcePath = "local/enus/strings.xml"; } return true; }
文件夹结构:
strings.xml
<dict> <key>mode</key> <string>Mode</string> <key>mode_classic</key> <string>Classic</string> </dict>