oarfish
oarfish

Reputation: 4641

How do I keep localized Storyboards or XIBs in sync?

I use XCode 6. Imagine the following scenario: Your app's base language is English and you decide to add a German localisation. You now have two versions of Interface.xib. If you were to add UI elements to the base version, you could simply convert the German version to localisable strings and back and all changes would be ported (if strings were added, those would have to be manually edited of course)

But what if it's the German version of Interface.xib you edited? How do you get the new UI elements into the English version? You can't simply use the same trick, at least I have found no way to convert back from localised strings, probably XCode has no idea it should use the remaining German XIB and fill in the English localised strings.

What do I do, except always make UI changes in the base version in the future?

Upvotes: 2

Views: 416

Answers (2)

Douglas Frari
Douglas Frari

Reputation: 4407

You can use a strategy to keep your XIBs or Storyboards in single file and locate visual components them on demand, programmatically making.

I created a demo project and put on my github:

I believe that with a few steps you can assess whether the strategy is valid for what you need:

  • Configure your project in the languages you want to support;

enter image description here

  • Create a file to support Location;

enter image description here

  • With the created file, add the target language, then Xcode will create a file for each language and you can put your keys and translation values.

enter image description here

  • Create your visual component inherited from its target;

enter image description here

If you customize a visual component (such as UILabel), override the method can 'drawRect' and dynamically replace the key placed in the text field. So you can keep the common design for all screens without the worry of maintaining more than one layout for internationalization.

- (void)drawRect:(CGRect)rect {
    NSString *text = NSLocalizedStringWithDefaultValue([self text], nil, [NSBundle mainBundle], [self text], @"");
    if (self.attributedText && [[self text] length] > 0) {

        NSDictionary *attributes = [(NSAttributedString *)self.attributedText attributesAtIndex:0
                                                                                 effectiveRange:NULL];
        [self setText:text];
        self.attributedText = [[NSAttributedString alloc] initWithString:self.text attributes:attributes];
    }else{
        [self setText:text];
    }

    [super drawRect:rect];
}

When you run your project now note that the UILabel component will dynamically set the key value as you set the property of their XIB or Storyboard.

enter image description here

General helps:

Upvotes: 2

Michael Teper
Michael Teper

Reputation: 4651

You are correct in your conclusion. The way localization is designed in Xcode expects you to originate all UI changes in the base language.

Upvotes: 2

Related Questions