How to use NSLocalizedString in IB [iPhone SDK]?

When I assign text programmatically, I can use NSLocalizedString (....), but if I write text directly in IB (say, in UILabel), how to localize it?

+6
iphone localization
source share
5 answers

One solution is to create multiple localized nib files. This probably works best if your localization needs are very simple and you just pass your resources on to the software localization experts at the end of development.

If you localize at design time and the user interface changes quickly, duplicating nib files can be a big pain, as each tweek user interface must be duplicated in every local version of nib. To avoid this, you will need to write code in the controllers of your view to process localized strings, usually in the -viewDidLoad control -viewDidLoad . Ensure that each localized text control is an IBOutlet and is connected to the controller of your view in IB. Then your view controller -viewDidLoad will look something like this:

 - (void)viewDidLoad { [super viewDidLoad]; hello.text = NSLocalizedString(@"Hello", @"Hello label"); world.text = NSLocalizedString(@"world", @"world label"); // ... etc } 

You do this view setup in -viewDidLoad , since the objects in your banner are not created completely until the -init method is -init . -viewDidLoad starts after -init , but before your view becomes visible.

+11
source share

This is a big and complicated topic. A good starting point is Introduction to Internationalization Programming Topics , and Preparing File Files for Localization

+6
source share

You can create multiple versions of the .nib file for each locale. There are also tools to easily edit lines in them. Apple has some good documentation on this.

+3
source share

I was also looking for a solution, not necessarily for the iPhone, but Xcode / IB in general. All the links did not relate to the fact that you may need a internal key to indicate the status and want to display the string localized to the user in the label or text box corresponding to this key. At the first stage, I did not find a standard approach on how to store, for example, the key value for the key by default for general users and displays a localized string for this key value in the label.

I found a solution that does not need a lot of encodings and matches the bindings in ib.

First you provide a Localizable.strings file, for example. with line containing

 "MyKeyValue" = "Localized display label"; 

Now you can localize the key value with: NSLocalizedString (aKeyValue, nil).

In the label, you did not find any value transformer associated with the NSLocalized String. Therefore, I created the KeyToLocalizedStringTransformer class to convert the key value to a localized string:

 @interface KeyToLocalizedStringTransformer : NSValueTransformer {} @implementation KeyToLocalizedStringTransformer + (Class)transformedValueClass { return [NSString class]; } + (BOOL)allowsReverseTransformation { return NO; } - (id)transformedValue:(id)aValue { NSString *NLString = [NSString stringWithString:NSLocalizedString(aValue,nil)]; return NLString; } 

The final step for preparation is to register a transformer, for example. in + initialize:

 NSValueTransformer *transformer = [[KeyToLocalizedStringTransformer alloc] init]; [NSValueTransformer setValueTransformer:transformer forName:@"KeyToLocalizedStringTransformer"]; 

Now you can use the value transformer in bindings for a text field or cell (just enter a name if you only see NSUnArchiveFromData, etc.)

Sorry, there is no image here from IB, because I am new here and have no reputation: you should imagine binding to the default user controller, Kontroller key: values, model key path: MyStateKey and value converter as described.

As a result, you don’t have to do anything in a duplicated nib nib with a label, just translate the string into Localizable.strings.

+2
source share

from iOS 7 and Xcode 5, you should avoid using NSLocalizedString where possible. The preferred method is called “basic localization” and works through the storyboard. This will save you a lot of work. If you google for "basic localization", you will find enough tutorials to get you.

0
source share

All Articles