方法一 使用 xml
在vs中创建xml
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>startGame</key>
<string>开始游戏</string>
<key>japanese</key>
<string>地方</string>
</dict>
</plist>
代码中使用
Dictionary* dic = Dictionary::createWithContentsOfFile("chineseString.xml");
String* strchinese = (String*)dic->objectForKey("startGame");
LabelBMFont* bmFont = LabelBMFont::create(strchinese->getCString(),"font_1.fnt");
addChild(bmFont,0,1000);
bmFont->setPosition(Point(200,500));
bmFont->setWidth(20);
方法二 使用类库
首先创建一类库 名为G2UTool
#ifndef __TOOLS_H__
#define __TOOLS_H__
#include <string>
#include "iconv/iconv.h"
using namespace std;
class G2UTool
{
public:
static int GBKToUTF8(std::string &gbkStr);
};
#endif
#include "G2UTool.h"
int G2UTool::GBKToUTF8(std::string &gbkStr)
{
iconv_t iconvH;
iconvH = iconv_open("utf-8","gb2312");
if(iconvH == 0){
return -1;
}
const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*)malloc(strLength*4);
char* pBuff = outbuf;
memset(outbuf,0,strLength*4);
size_t outLength = strLength*4;
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength)){
iconv_close(iconvH);
return -1;
}
gbkStr = pBuff;
iconv_close(iconvH);
return 0;
}
然后再cocos2dx中调用
std::string str2 = "开始游戏";
G2UTool::GBKToUTF8(str2);
LabelBMFont* bmFont = LabelBMFont::create(str2.c_str(),"font_1.fnt");
addChild(bmFont,0,1000);
bmFont->setPosition(Point(200,500));
bmFont->setWidth(20);