Cocos2d-x 3.2 Monopoly game project development-Part 2 internationalization-solve Chinese garbled characters, cocos2d-x Project Development
Solve the Chinese Garbled text first. cheyiliu, a netizen, provides a simple solution as follows. If you have a better solution, please provide it.
1. Compile the plist file string_zh.plist to display Chinese characters in the file:
<? 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 player </string> <key> multi_game </key> <string> multiplayer games </string> ....................................... </Dict> </plist>
2. string_en.plist is used to display English characters:
<?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. Compile the LanguageString class to load the corresponding character plist file according to the current system language environment. The file only needs to be loaded once and can be written as a singleton.
LanguageString. h header file class LanguageString {private: LanguageString (); public: static LanguageString * instance; static Dictionary * strings ;~ LanguageString (); static LanguageString * getInstance (); String * getLanguageString (const char * strName );};
LanguageString. cpp file # 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 (); // obtain the current system language type if (strings = NULL) {switch (currentequalagetype) {case kmeanageenglish: // if the system is English, load the string_en.plist file {strings = Dictionary: createWithContentsOfFile ("string_en.plist"); strings-> retain (); break;} case kmeanagechinese: if the system is Chinese, load the string_zh.plist file {strings = Dictionary: createWithContentsOfFile ("string_zh.plist"); strings-> retain (); break ;}}} string * retStr = static_cast <String *> (strings-> objectForKey (strName); // obtain the return retStr String based on the key ;}
4. usage. When using a string, you only need to input the corresponding key.
LanguageString: getInstance ()-> getLanguageString ("here is the key value "));
For example:
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"> click to download the Code </a>
http://download.csdn.net/detail/lideguo1979/8342891
To be continued ......................