cocos2d-x本地化简化/繁体中文

时间:2012-05-22 07:16:02

标签: android iphone localization cocos2d-iphone cocos2d-x

我需要将简体/繁体中文分开。在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。谢谢。

3 个答案:

答案 0 :(得分:2)

我在cocos2D helloworld上测试了以下代码。我可以通过 zh-Hans zh-Hant 分离简化/传统。

步骤1。在HelloWorldLayer.m中,您需要在顶部添加此行或无法编译:

#import <Foundation/NSLocale.h>

第二步。现在,你可以获得语言。例如,

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

答案 1 :(得分: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();
}

答案 2 :(得分:0)

您需要在cocos2dx android jni中进行一些更改: 在 cocos2d-x-2.x.x / 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;
}