Cocos2d-x Simplified / Traditional Chinese Localization

I need to separate Simplified / Traditional Chinese. in cocos2d for iPhone, I just use hans and hant. but in cocos2d-x I dive into the code and here is the code for CCApplication:

ccLanguageType ret = kLanguageEnglish; if ([languageCode isEqualToString:@"zh"]) { ret = kLanguageChinese; } else if ([languageCode isEqualToString:@"en"]) { ret = kLanguageEnglish; } else if ([languageCode isEqualToString:@"fr"]){ ret = kLanguageFrench; } else if ([languageCode isEqualToString:@"it"]){ ret = kLanguageItalian; } else if ([languageCode isEqualToString:@"de"]){ ret = kLanguageGerman; } else if ([languageCode isEqualToString:@"es"]){ ret = kLanguageSpanish; } else if ([languageCode isEqualToString:@"ru"]){ ret = kLanguageRussian; } return ret; 

Please note that only "zh" is for Chinese (as simplified / traditional, maybe)

so how can i tell them apart?

EDIT: I am using cocos2d-x and you need to work with Android. not just the iPhone. Mickey only answers on the iPhone. Thank you

+4
source share
3 answers

You need to make some changes to cocos2dx android jni: in cocos2d-x-2.xx / cocos2dx / platform / android / java / src / org / cocos2dx / lib / Cocos2dxHelper.java ,

replace

 return Locale.getDefault().getLanguage(); 

from

 return Locale.getDefault().toString(); 

Thus, you can get zh_CN, zh_TW in CCApplication :: getCurrentLanguage (), also you have to update the implementations in CCApplication :: getCurrentLanguage () (finds cocos2d-x-2.xx / cocos2dx / platform / android / CCApplication.cpp):

 ccLanguageType CCApplication::getCurrentLanguage() { std::string languageName = getCurrentLanguageJNI(); if (languageName.find("zh_CN") == 0) { return kLanguageChineseCN; // need define this value by yourself } else if (languageName.find("zh_TW") == 0) { return kLanguageChineseTW; // need define this value by yourself } else if (languangeName.find("en") == 0) { return kLanguageEnglish; } ... return kLanguageEnglish; } 
0
source

I checked the following code on cocos2D helloworld. I can separate the simplified / traditional zh-hans and zh-hant .

Step 1. In HelloWorldLayer.m you need to add this line at the top or not to compile:

 #import <Foundation/NSLocale.h> 

Step2. Now you can get the language. For instance,

 -(id)init{ NSString* currentLang = [[NSLocale preferredLanguages] objectAtIndex:0] ; NSLog(@"Language: %@", currentLang); } 
+2
source

This is how I modified the cocos2d-x code to distinguish between simplified and traditional Chinese. Please note that this is for cocos2d-x v3.0 +.

For iOS, change 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; } ..... ..... 

For Android, change 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)) ..... ..... 

and also change libcocos2d / org / cocos2dx / lib / Cocos2dxHelper.java

 public static String getCurrentLanguage() { // This would return language code as well as region code, eg zh_CN return Locale.getDefault().toString(); } 
+1
source

Source: https://habr.com/ru/post/1413682/


All Articles