iOS Localizable.strings file for XIB files?

淺唱寂寞╮ 提交于 2019-12-03 02:24:21

You have two options how to translate xib files. One is you connect the UI elements to outlets and set your strings in your viewDidLoad method using the NSLocalizedString macros.

The second option is to provide a separate xib for each language your app supports. You don't have to create them manually, you can use the ibtool command (i assume your source language is English and target is Portugese):

ibtool --strings-file pt.lproj/Example.strings en.lproj/Example.xib –write pt.lproj/Example.xib

To collect strings found in your project you can use genstrings command - however i recommend using this python script to collect all your strings - it can nicely handle the situation when you need to add/remove strings to your app without having to translate and/or manually merge all previous strings

Edit

Oh and i found the article that i learned this trick from

I made a category to do this:

@implementation UIView (Localise)

- (void)localise
{
    [self.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        [view localise];
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)view;
            [label setText:NSLocalizedString(label.text, nil)];
        }
    }];
}

Then in your views:

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self localise];
}

You'll need to add support for other UI elements (textField.placeholder = NSLocal...blah blah), but that'll do the trick for UILabel and children.

Probably not great performance-wise but does the job.

It's going to depend on the app and the number of localizations, but in general I prefer separate .xib files, because layout may change. Put the localized files in pt.lproj/.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!