Question

i need to separate simplified/traditional chinese. in cocos2d for iPhone, i simply 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;

note that only 'zh' for chinese (both simplified/traditional, maybe)

so how can i distinguish them?

EDIT: I use cocos2d-x and need to work with android. not just iPhone. Mickey's answer works for iPhone only. thanks.

Was it helpful?

Solution

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

replace

return Locale.getDefault().getLanguage();

with

return Locale.getDefault().toString();

Thus, you can get zh_CN, zh_TW in CCApplication::getCurrentLanguage(), also, you must update the implementations in CCApplication::getCurrentLanguage() (locates cocos2d-x-2.x.x/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;
}

OTHER TIPS

I tested the following code on cocos2D helloworld. I can seperate simplified/traditional by zh-Hans and zh-Hant.

Step1. In HelloWorldLayer.m, you need to add this line at the top or fail to compile :

#import <Foundation/NSLocale.h>

Step2. Now, you can get language. For example,

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

Here's how I modified cocos2d-x code to distinguish simplified and traditional Chinese. Note, this is for cocos2d-x v3.0+.

For iOS, modify 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 , modify 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 modify 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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top