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://download.csdn.net/detail/lideguo1979/8342891">点击下载代码</a>
http://download.csdn.net/detail/lideguo1979/8342891



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

原文链接:https://www.f2er.com/cocos2dx/345017.html

猜你在找的Cocos2d-x相关文章