Question

I've an iPhone storyboard with some views. For instance, a navigation item title is named News, which should be translated for other languages.

When I add a new localization to my storyboard, it created a duplicate of my current storyboard for the new language. Here I can change the title for the navigation item, but for me it does not seem very useful. What if my storyboard contains 100 views and I need to support 10 languages? If I need to change something in my original storyboard, I have to make the same changes for all languages. That seems very odd. In which situations can this be useful?

What can I do instead? Should I have only the english storyboard and manually translate each element in the ViewController using NSLocalizedString?

Was it helpful?

Solution

You can do the localization by changing the titles of the UI elements in code:

self.title = NSLocalizedString("News", nil);

If you want to localize your app in Dutch for example, you would have this in Dutch.lproj/Localizable.strings:

"News" = "Nieuws";

You can then do this for every UI element and language.

OTHER TIPS

In iOS 6 there is a Project setting under the Info tab that says "Use Base Internationalization". If you check the box, it will pull all of the strings out of the Storyboard and into internationalized .strings files.

This way you don't have to have multiple copies of the Storyboard.

enter image description here

As of iOS 6 you can decide to use Base Internationalization: All storyboard and xib/nib files exist only once in a folder named Base.lproj. The localization of the GUI elements is placed in related .strings files in each localization directory.

For example "MainStoryboard.storyboard" will be placed in Base.lproj. An associated file called "MainStoryboard.strings" is placed in en.lproj and whatever localization you apply.

This is really very handsome, especially in combination with layout constraints!

You might find this video tutorial here useful:

http://www.youtube.com/watch?v=cF1Rf02QvZQ

The approach is to have a separate storyboard for each language to localize, then let a script take care of propagating the changes you make in the original storyboard for all other languages.

For apps with a deployment target of iOS 5 and storyboards I use something like this to localize my tabs where my first ViewController on a storyboard is a UITabBarController:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // ...

    if ([self.window.rootViewController isKindOfClass:UITabBarController.class]) {
        UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;

        // Localize tab items
        NSArray *tabBarItems = tabBarController.tabBar.items;

        [tabBarItems enumerateObjectsWithOptions:NSEnumerationConcurrent 
                                      usingBlock:^(UITabBarItem *item, NSUInteger tabIndex, BOOL *stop) {
            NSString *keyName = [NSString stringWithFormat:@"tabBarItem%i", tabIndex];
            item.title = NSLocalizedString(keyName, @"TabBarItems");
        }];
    } else {
        // The root view controller is not the UITTabBarController
    }

    // ...
}

and have something like this in my Localizable.strings files:

// MARK: TabBar
"tabBarItem0" = "My First Tab Label";
"tabBarItem1" = "My Second Tab Label";
"tabBarItem2" = "My Third Tab Label";

You might find Polyglot Localization useful as well. In stead of calling NSLocalizedString() in code, you can directly specify the key it in your Storyboard. You can avoid unnecessary outlets and reduce boilerplate code.

https://github.com/negusoft/Polyglot

I suggest you learn the commandline ibtool it helps asist you in localizing all of your storyboards :) Here is a tutorial here http://www.albertmata.net/articles/introduction-to-internationalization-using-storyboards-on-ios-5.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top