douglasddf / IOSLocalizationDemo

Demonstrates an approach to internationalization use with centralized files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IOSLocalizationDemo

Demonstrates an approach to internationalization use with centralized files.

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

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:

An article tutorial (in portuguese)

<script async src="https://static.medium.com/embed.js"></script>Language Localization em projetos iOS

About

Demonstrates an approach to internationalization use with centralized files.

License:MIT License


Languages

Language:Objective-C 100.0%