IOS Configuration

TMLKit allows some fine-tuning of its behavior via TMLConfiguration.

Basics Configuration

You would need to configure TMLKit in your main.m file like this:

#import <TMLKit/TMLKit.h>

int main(int argc, char * argv[])  
{
    @autoreleasepool {

        [TML sharedInstanceWithApplicationKey:@"<APPLICATION KEY>" accessToken:@"<ACCESS TOKEN>"];
// ...
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

Advance Configuration

You can configure TMLKit with a configuration object:

TMLConfiguration *config = [TMLConfiguration initWithApplicationKey:@"<APPLICATION KEY>" accessToken:@"<ACCESS TOKEN>"];

// Enable/disable translation mode
config.translationEnabled = YES;

// informs TMLKit whether or not it should automatically localized strings found in Interface Builder files
config.localizeNIBStrings = YES;

[TML sharedInstanceWithConfiguration:config];

or modify the configuration object at runtime:

TMLConfiguration *config = TMLSharedConfiguration();  
config.translationEnabled = NO;  

The latter route is something you'd take if you wanted to define global tokens:

TMLConfiguration *config = TMLSharedConfiguration();  
// you only need to do this once
[config setDefaultTokenValue:@"ACME Corporation" forName:@"CompanyName"];

// and you don't need to explicitly specify it anywhere else in your code:
TMLLocalizedString(@"Please contact {CompanyName}...");  

See TMLConfiguration.h for more.

Reusing localizable strings

iOS doesn't allow applications to switch locales at run time. In order to switch locale - you have to go to System Settings and do it there, which inadvertently restarts the application. This doesn't work very well when using Translation Mode or allowing users to switch locale at run time. To facilitate locale switching at run time, TMLKit provides ability to re-localize existing localizable strings via TMLLocalizedStringWithReuseIdenitifer() macros. They are identical to TMLLocalizedString() macros we've seen earlier, with the only addition of a reuse identifier, by which TMLKit can obtain original localizable strings in order to reapply new translation strings:

- (void)viewDidLoad {
  UILabel *label = [[UILabel alloc]init];
  label.text = TMLLocalizedStringWithReuseIdenitifer(@"You have {count || message}", @"messageLabel", @{@"count": @5});
  self.messageLabel = label;
}

- (void)updateTMLLocalizedStringWithInfo:(NSDictionary *)info forReuseIdentifier:(NSString *)reuseIdentifier { 
  if ([reuseIdentifier isEqualToString:@"messageLabel"] == YES) { 
    self.messageLabel.text = info[TMLLocalizedStringInfoKey]; 
    } 
    else { 
      [super updateTMLLocalizedStringWithInfo:info forReuseIdentifier:reuseIdentifier]; 
    } 
}

We can refine this a little bit, without even implementing -updateTMLLocalizedStringWithInfo:forReuseIdentifier:. The default implementation of that method will look for a property identified by the reuse identifier, on the sender object:

- (void)viewDidLoad {
  UILabel *label = [[UILabel alloc]init];
  label.text = TMLLocalizedStringWithReuseIdenitifer(@"You have {count || message}", @"messageLabel.text", @{@"count": @5});
  self.messageLabel = label;
}

This is all that's required as long as you have a @property(messageLabel) defined on that class. This also works with attributed strings:

- (void)viewDidLoad {
  UILabel *label = [[UILabel alloc]init];
  label.attributedText = TMLLocalizedStringWithReuseIdenitifer(@"{user} sent {target} [bold: {count || gift}]", @"titleLabel.attributedText", @{@"user": currentUser, @"target": targetUser, @"count": @5));
  self.titleLabel = label;
}

This approach allows a view, or a view controller, to reload strings w/o having to reload itself completely. However, that is completely doable:

- (void)viewDidLoad {
  [self configureView];
}

- (void)configureView {
  UILabel *label = [[UILabel alloc]init];
  label.text = TMLLocalizedString(@"You have {count || message}", @{@"count": @5});
  self.messageLabel = label;
}

- (void)updateReusableTMLStrings {
  [super updateReusableTMLStrings];
  [self configureView];
}

Notice here that we're using TMLLocalizedString() macro as TMLLocalizedStringWithReuseIdentifier() isn't needed here at all. That's because we override -[NSObject updateReusableTMLStrings] method and handle re-localization ourselves. In fact, this is how TMLKit handles collection views like UITableView and UICollectionView by default. That behavior, however, is configurable via:

TMLConfiguration *config = TMLSharedConfiguration();  
config.automaticallyReloadDataBackedViews = NO;