Search code examples
objective-clocalizationcocos2d-iphonelocalizable.strings

cocos2d custom localization system for iphone games


I'm using a custom localization system for my game; in that tutorial he adds the label in a custom method but my text labels are added in init

Tutorial's example:

- (void) setHelloWorldLabel
{
    // create and initialize a Label
    CCLabel* label = [CCLabel labelWithString:AMLocalizedString(@"hello",@"Hello World") fontName:@"Marker Felt" fontSize:32];

    // ask director the the window size
    CGSize size = [[CCDirector sharedDirector] winSize];

    // position the label on the center of the screen
    label.position =  ccp( size.width /2 , size.height/2 );

    //Check if it's already been added to the layer.
    if ([self getChildByTag:50])
        [self removeChildByTag:50 cleanup:YES];

    // add the label as a child to this Layer
    [self addChild:label z:0 tag:50];
}

Setting a language

-(void) menuCallbackEN: (id) sender
{
    LocalizationSetLanguage(@"English");
    [self setHelloWorldLabel];
}

How to deal with multiple text labels?

Some code sample would help me :)


Solution

  • You could add another method which could be called on init and on language change events. This method should look like this:

    - (void)initLocalizableLables
    {
        // Remove old labels
        for (NSInteger i=[children_ count]-1; i>=0; i--)
        {
            CCNode *c = [children_ objectAtIndex:i];
    
            if ([c isKindOfClass:[CCLabel class]])
            {
                [c removeFromParentAndCleanup:YES];
            }
        }
    
        // Add labels with localization    
        CCLabel* label = [CCLabel labelWithString:AMLocalizedString(@"hello",@"Hello World") fontName:@"Marker Felt" fontSize:32];
        ...
        [self addChild:label z:0 tag:50];
    }
    
    - (void)init
    {
        ...
        [self initLocalizableLables]; // add localized labels
        ...
    }
    
    - (void)languageDidChange
    {
        [self initLocalizableLables]; // remove old localized labels and add new
    }