I18n for iOS and Android

Hello

I’ve come to need the use of getCurrentLanguage() method to have the I18n in a project. I realized it was made only to support English and Chinese.
I have made some little changes to extend it to the EFIGS’s languages. It is not a big modification but it may interest someone around here.
I made the changes for iPhone and Android, not for Wophone.

Changes in CCApplication_ios.mm

ccLanguageType CCApplication::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];

    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;
    }

    return ret;
}

Changes in CCApplication_android.cpp

ccLanguageType CCApplication::getCurrentLanguage()
{
    char* pLanguageName = getCurrentLanguageJNI();
    ccLanguageType ret = kLanguageEnglish;

    if (0 == strcmp("zh", pLanguageName))
    {
        ret = kLanguageChinese;
    }
    else if (0 == strcmp("en", pLanguageName))
    {
        ret = kLanguageEnglish;
    }
    else if (0 == strcmp("fr", pLanguageName))
    {
        ret = kLanguageFrench;
    }
    else if (0 == strcmp("it", pLanguageName))
    {
        ret = kLanguageItalian;
    }
    else if (0 == strcmp("de", pLanguageName))
    {
        ret = kLanguageGerman;
    }
    else if (0 == strcmp("es", pLanguageName))
    {
        ret = kLanguageSpanish;
    }

    return ret;
}

Changes in CCCommon.h

typedef enum LanguageType
{
    kLanguageEnglish = 0,
    kLanguageChinese,
    kLanguageFrench,
    kLanguageItalian,
    kLanguageGerman,
    kLanguageSpanish,
} ccLanguageType;

hello, Romain Del Boccio, thank you for your work , this will meet somebody’s demand.

Thanks, I would merge it into repo.
Maybe put this method in CCApplication is a little odd, a new class for localization should be better?

I totally agree that new class better for.

You can also add pair "ru" - kLanguageRussian

Random thought, you can take a look in the game Meshem-up, it use xml files to do the localization. Should we make a platform-independent localization approach based on xml? Ming would like to refactor libxml usage to tinyxml, so I will consider the localization after his refactor.

TinyXML?? It’s very slow… Please look at http://pugixml.googlecode.com or http://rapidxml.sourceforge.net

They are both much faster than tinyxml…

Feature #676 create for Romain Del Boccio’s l18n codes

Merged in https://github.com/cocos2d/cocos2d-x/commit/a24a84d82e8707b22f40733dfde1bb6f9e84ca0e

Is there any example how to use i18n in the projects? Any alternative to NSLocalizedString? Where do you usually store the strings?

My suggestion is to use CCApplication::getCurrentLanguage(), then load different xml file, which contents same keys/tags but different language strings.

In my game I’ve done a parser of binary PList and took all Localizable.strings from my iOS project.

It is just easier to use the same files without conversion to XML, plus there are tools for editing translations on mac - I use Linguan.

I’ve just finished with parsing, and so far it all looks good. The next step would be loading appropriate files (for current language and default language, in case if string is not found in the translated file).

If somebody is interested in this, I can post the source code later.

Thanks Dmitry, I would be very interested byt this source code if you can post it.

Yeah Dimitry, me too. It would be brilliant if you could.
Thanks in advanced! :slight_smile:

Why cocos2d-x not supports auto loading resources like iOS?

assets/hello.png
resources/en/hello.png
resources/zh/hello.png

I can’t find best way for multi-language… T.T

1 Like