首页 > 代码库 > Cocos2d-x 3.2 大富翁游戏项目开发-第十九部分 国际化-解决中文乱码

Cocos2d-x 3.2 大富翁游戏项目开发-第十九部分 国际化-解决中文乱码

先解决一下中文乱码问题吧,网友cheyiliu 提供了比较简单的解决办法,具体如下,如有更好方法,欢迎提供


技术分享


1、 编写plist文件string_zh.plist把需要中文显示字符,放入到该文件中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>single_game</key>
    <string>单人游戏</string>

    <key>multi_game</key>
    <string>多人游戏</string>

…………………………………	
</dict>
</plist>

2、 string_en.plist用来显示英文字符:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>single_game</key>
    <string>Single Game</string>
	……………………..
</dict>
</plist>

3、编写类LanguageString   根据当前系统语言环境加载相应的字符plist文件,文件只需加载一次就可以了,写成单例

LanguageString.h头文件
class LanguageString
{

private:
	LanguageString();

public:
	static LanguageString* instance;
	static Dictionary *strings;
	~LanguageString();
	static LanguageString * getInstance();

	String* getLanguageString(const char* strName);

};

LanguageString.cpp文件
#include "LanguageString.h"

LanguageString* LanguageString::instance;
Dictionary * LanguageString::strings;

LanguageString::LanguageString()
{
}


LanguageString::~LanguageString()
{
}

LanguageString * LanguageString::getInstance()
{
	if(instance == NULL)
	{
		instance = new LanguageString();
	}
	
	return instance;
}

String* LanguageString::getLanguageString(const char* strName)
	{
		LanguageType currentLanguageType = Application::getInstance()->getCurrentLanguage();//获取当前系统语言类型

		if(strings == NULL)
		{
			switch (currentLanguageType)
			{
			case kLanguageEnglish: //系统是英文,则加载string_en.plist文件
				{
					strings = Dictionary::createWithContentsOfFile("string_en.plist");
					strings->retain();
					break;
				}

			case kLanguageChinese: 系统是中文,则加载string_zh.plist文件
				{
					strings = Dictionary::createWithContentsOfFile("string_zh.plist");
					strings->retain();
					break;
				}

			}
		}
		String* retStr = static_cast<String*>(strings->objectForKey(strName)); //根据key获取字符串
		return retStr;

	}


4、使用方法,当使用字符串时只要传入相应的key就可以了

       LanguageString::getInstance()->getLanguageString(“此处是key值”));

    如 :

      LabelTTF* singleGameTTF = LabelTTF::create(LanguageString::getInstance()->getLanguageString(SINGLE_GAME)->getCString() ,FONT_MENU,Btn_FontSize);


<a target=_blank href=http://www.mamicode.com/"http://download.csdn.net/detail/lideguo1979/8342891">点击下载代码>
http://download.csdn.net/detail/lideguo1979/8342891



未完待续......................

Cocos2d-x 3.2 大富翁游戏项目开发-第十九部分 国际化-解决中文乱码