下面是我如何修改cocos2d-x代码来区分简体和繁体中文。注意,这是针对 cocos2d-x v3.0+ 的。
对于 iOS,修改 cocos2d_libs.xcodeproj/platform/ios/CCApplication-ios.mm
LanguageType Application::getCurrentLanguage()
{
// get the current language and country config
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
// get the current language code.(such as English is "en", Chinese is "zh" and so on)
NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage];
NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode];
LanguageType ret = LanguageType::ENGLISH;
if ([languageCode isEqualToString:@"zh"])
{
/** CHANGE THE FOLLOWING LINES */
NSString* scriptCode = [temp objectForKey:NSLocaleScriptCode];
NSString* countryCode = [temp objectForKey:NSLocaleCountryCode];
// On iOS, either chinese hong kong or chinese taiwan are traditional chinese.
if ([scriptCode isEqualToString:@"Hant"] || [countryCode isEqualToString:@"HK"] || [countryCode isEqualToString:@"TW"]) {
ret = LanguageType::CHINESE_TRADITIONAL; // You need to add these enum values to LanguageType
} else {
ret = LanguageType::CHINESE_SIMPLIFIED; // You need to add these enum values to LanguageType
}
}
else if ([languageCode isEqualToString:@"en"])
{
ret = LanguageType::ENGLISH;
}
.....
.....
对于 Android ,修改 cocos2d/cocos/platform/android/CCApplication-android.cpp
LanguageType Application::getCurrentLanguage()
{
std::string languageName = getCurrentLanguageJNI();
const char* pLanguageName = languageName.c_str();
const char* languageCode = getCurrentLanguageCode();
LanguageType ret = LanguageType::ENGLISH;
if (0 == strcmp("zh", languageCode))
{
/** Change the following lines */
if (languageName.find("TW") != std::string::npos) {
ret = LanguageType::CHINESE_TRADITIONAL;
} else {
ret = LanguageType::CHINESE_SIMPLIFIED;
}
}
else if (0 == strcmp("en", languageCode))
{
ret = LanguageType::ENGLISH;
}
else if (0 == strcmp("fr", languageCode))
.....
.....
并修改 libcocos2d/org/cocos2dx/lib/Cocos2dxHelper.java
public static String getCurrentLanguage() {
// This would return language code as well as region code, e.g. zh_CN
return Locale.getDefault().toString();
}