Domanda

Ho uno storyboard iPhone con alcuni punti di vista. Per esempio, un titolo oggetto di navigazione è chiamato News, che dovrebbe essere tradotto per altre lingue.

Quando aggiungo una nuova localizzazione al mio storyboard, ha creato un duplicato del mio storyboard corrente per la nuova lingua. Qui posso cambiare il titolo per l'elemento di navigazione, ma per me non sembra molto utile. E se il mio storyboard contiene 100 viste e ho bisogno di supportare 10 lingue? Se ho bisogno di cambiare qualcosa nel mio storyboard originale, devo fare le stesse modifiche per tutte le lingue. Che sembra molto strano. In quali situazioni possono essere utili questo?

Che cosa posso fare, invece? Dovrei avere solo lo storyboard inglese e tradurre manualmente ogni elemento della ViewController utilizzando NSLocalizedString?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top