mrmuggles
mrmuggles

Reputation: 2131

Localizing strings with XCode 4.2 (IOS-IPhone dev)

In some of my screens in my program the localized strings work and in some it doesn't. (Xcode 4.2)

What I've done:

  1. Added a Localizable.strings in the folder "Resources"

  2. in my .h file, inside the @interface ClassViewController : UIViewController {} I added:

    IBOutlet UILable *labelName;
    
  3. Also in the .h file, I added

    property (nonatomic, assign) IBOutlet UILabel *labelName;
    
  4. In the .m file, I added :

    @synthetize labelName;
    
  5. Still in the .m file, I added inside "-(foid)dealloc" :

    [labelName release];
    
  6. In -(void)viewDidLoad I added :

    self.labelName.text = [NSString stringWithFormat:@"KEY"];
    
  7. Finally, in the xib file (with the Interface manager), I linked the label object with the variable.

So, as I said, this method works in some screen and not in others. Any idea?

Solution:

That what a stupid mistake. the line to enter the text should be:

NSLocalizedString(@"KEY", nil);

Upvotes: 1

Views: 693

Answers (4)

Gagan
Gagan

Reputation: 31

In XCode-4.2 you can do the following:

  1. Click on "target"
  2. Then select "Build Phase"
  3. Select "Add Build Phase" & choose "Add Copy files".
  4. After select "copy files(int item)".( where 'int' may be 1or 2 or 3 etc. depending upon how many items previously have been created).Set "Destination" to "Resources".
  5. Now click on '+' & choose your created '.string' file(eg. localization.string).

It works, hope it will help you.

Upvotes: 1

VsSoft
VsSoft

Reputation: 288

As Vince pointed out, you need to use NSLocalizedString function. So in this case the code in 6 would be:

    self.labelName.text = NSLocalizedString(@"The key for this label",@"Some comment");

That should work for you.

Upvotes: 1

Nick Lockwood
Nick Lockwood

Reputation: 40995

What do you imagine this line does?

self.labelName.text = [NSString stringWithFormat:@"KEY"];

Because it's functionally equivalent to writing this:

self.labelName.text = @"KEY";

But I suspect that what you meant to write was this instead:

self.labelName.text = NSLocalizedString(@"KEY", @"description");

Upvotes: 0

Paul N
Paul N

Reputation: 1931

Use instead:

self.labelName.text = NSLocalizedString(@"KEY", "");

From the Documentation:

  • Name: NSLocalizedString
  • Description: NSString *NSLocalizedString(NSString *key, NSString *comment)
  • Availability: iOS (2.0 and later)
  • Abstract: Returns a localized version of a string.

Upvotes: 4

Related Questions