4

我需要区分简体/繁体中文。在 iPhone 的 cocos2d 中,我只使用“hans”和“hant”。但是在 cocos2d-x 中,我深入研究了代码,这里是 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;

请注意,只有中文的“zh”(可能是简体/繁体)

那么我该如何区分它们呢?

编辑:我使用 cocos2d-x 并且需要使用 android。不只是 iPhone。米奇的答案仅适用于 iPhone。谢谢。

4

3 回答 3

2

我在 cocos2D helloworld 上测试了以下代码。我可以通过zh-Hanszh-Hant来区分简体/繁体。

第1步。在 HelloWorldLayer.m 中,需要在顶部添加这一行,否则编译失败:

#import <Foundation/NSLocale.h>

第2步。现在,您可以获得语言。例如,

-(id)init{
    NSString* currentLang = [[NSLocale preferredLanguages] objectAtIndex:0] ;
    NSLog(@"Language: %@", currentLang);
}
于 2012-05-25T09:37:11.517 回答
1

下面是我如何修改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();
}
于 2015-05-28T02:14:27.083 回答
0

您需要在 cocos2dx android jni 中进行一些更改:在cocos2d-x-2.xx/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java中,

代替

return Locale.getDefault().getLanguage();

return Locale.getDefault().toString();

因此,您可以在 CCApplication::getCurrentLanguage() 中获取 zh_CN、zh_TW,此外,您必须更新 CCApplication::getCurrentLanguage() 中的实现(位于 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;
}
于 2013-08-13T09:44:35.797 回答