How to use NSLocalizedString in IB [iPhone SDK]?

前端 未结 5 1799
误落风尘
误落风尘 2021-02-03 13:55

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

5条回答
  •  故里飘歌
    2021-02-03 14:45

    One solution is to create multiple localized nib files. This probably works best if your localization needs are very straightforward and you're simply going hand your resources over to expert software localizers at the end of development.

    If you're localizing while you're developing and the UI is changing rapidly, duplicate nib files can be a big pain, since every UI tweek must be duplicated in each local version of the nib. To avoid this, you'll need to write some code in your view controllers to handle setting the localized strings, typically in the view controller's -viewDidLoad method. Make sure that every control with localized text is an IBOutlet and wired them up to your view controller in IB. Then your view controller's -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 type of view setup in -viewDidLoad since the objects in your nib aren't fully created until after your -init method runs. -viewDidLoad runs after -init but before your view becomes visible.

提交回复
热议问题