Search code examples
iphoneiosmultilingual

Ui programmatically with multilingual


I am trying to learn how to implement multilingual for an iphone app. I create my ui programmatically always. please check the code as below

UIImage *buttonImage2 = [[UIImage imageNamed:@"orangebuttonsmall.png"] stretchableImageWithLeftCapWidth:22 topCapHeight:0];
btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
[btnCancel setTitle:@"Save" forState:UIControlStateNormal];
[btnCancel setBackgroundImage:buttonImage2 forState:UIControlStateNormal];
btnCancel.frame = CGRectMake(205, 40.0, 60.0, 30.0);
btnCancel.titleLabel.font = [UIFont boldSystemFontOfSize:14];
btnCancel.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
btnCancel.titleLabel.textColor = [UIColor whiteColor];
btnCancel.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.3]; // [UIColor blackColor];
btnCancel.titleLabel.shadowOffset = CGSizeMake(0, -1);
[btnCancel sizeToFit];
btnCancel.bounds = CGRectInset(btnCancel.bounds, -10, 0);
[btnCancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
[self.containerView addSubview:btnCancel];

when I search for the localization, I get the examples for xibs but not programmatically. now I would like to know how can I change the above code to be multi lingual for example French.


Solution

  • [btnCancel setTitle:NSLocalizedString(@"Save",@"Title for Save Button") forState:UIControlStateNormal];
    

    Then use the terminal command

    genstrings -o en.lproj *.m
    

    to generate a Localizable.strings file

    Then tailor the resultant Localizable.strings file for each language and store in appropriate Lang.lproj subfolder in your project. (French.lproj English.lproj etc.)

    The NSLocalizedString function will pull in the language specific value and substitute it in the code.