views:

144

answers:

4

Hi all,

I have a tab bar app that has to display in different languages depending on a user's preference but I can't find much info on how to change the tab names at run-time. I need the tabs to show the correct names at startup, not when the tabs are accessed.

Best info I could find was running

self.tabBarController.selectedIndex = 1;
tabBarController.selectedViewController.tabBarItem.title = @"Tab Name";

from the app delegate but this first makes the tab active & then sets the name.

Is there not a better way to set tab names at run-time? Ideally I'd like to set them all in one go.

A: 

If you have a reference to the UITabBar, you can use something like:

for (UITabBarItem *tabBarItem in tabBar)
{
  tabBarItem.title = NSLocalizedString(...);
}
Kevin Sylvestre
A: 

I suggest you to localize your XIB file:

  • Right click on your XIB file
  • "Get Info"
  • "General" tab on the top
  • "Make File Localizable" button in the bottom
  • Back to "General" tab on the top
  • "Add Localization" button in the bottom + enter the locale you want (e.g. "en", "fr", "he", "ru" etc.)

Repeat the last step until you have all the requested languages.
I prefer to use "en" instead of the default "English" that is created automatically - if you prefer "en" too then delete the "English" in the end...

Now you can enter different titles to the tabs for each locale...

Michael Kessler
Doesn't localisation default to what is set on the phone i.e. user can't choose their preferred language, it is automatically chosen for them?I ask because here in Switzerland people are multilingual so there might be someone, for example, who's locale is German but they prefer Italian. Would localisation allow for that?
Spider-Paddy
As far as I know the current locale is single for all the applications - the one that you set in the general settings...
Michael Kessler
A: 

The 2 responses didn't work for me, I eventually did it like this:

// Create temp strings to hold tab names
NSString *tab0Name;
NSString *tab1Name;
NSString *tab2Name;
NSString *tab3Name;
NSString *tab4Name;

// Set strings according to language
if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"FR"])
{
    tab0Name = @"Accueil";
    tab1Name = @"Produits";
    tab2Name = @"Caisse";
    tab3Name = @"Branches";
    tab4Name = @"Plus";
}
else if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"IT"])
{
    tab0Name = @"Home";
    tab1Name = @"Prodotti";
    tab2Name = @"Checkout";
    tab3Name = @"Filiali";
    tab4Name = @"More";
}
else if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"EN"])
{
    tab0Name = @"Home";
    tab1Name = @"Products";
    tab2Name = @"Checkout";
    tab3Name = @"Branches";
    tab4Name = @"More";
}
else    // Default to german unless specifically set to another language
{
    tab0Name = @"Home";
    tab1Name = @"Produkte";
    tab2Name = @"Checkout";
    tab3Name = @"Filialen";
    tab4Name = @"Mehr";
}

// Set tab name
self.tabBarController.selectedIndex = 1;
tabBarController.selectedViewController.tabBarItem.title = tab1Name;
self.tabBarController.selectedIndex = 2;
tabBarController.selectedViewController.tabBarItem.title = tab2Name;
self.tabBarController.selectedIndex = 3;
tabBarController.selectedViewController.tabBarItem.title = tab3Name;
self.tabBarController.selectedIndex = 4;
tabBarController.selectedViewController.tabBarItem.title = tab4Name;
self.tabBarController.selectedIndex = 0;
tabBarController.selectedViewController.tabBarItem.title = tab0Name;    // Home last so it's shown first
Spider-Paddy
Any suggestions or corrections are welcome.
Spider-Paddy
A: 

The way I did it is by defining outlets in the app delegate:

IBOutlet UITabBarItem *tabBarItem1;
IBOutlet UITabBarItem *tabBarItem2;
IBOutlet UITabBarItem *tabBarItem3;
IBOutlet UITabBarItem *tabBarItem4;

and then, after connecting the outlets at IB, puting this to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions :

[tabBarItem1 setTitle:NSLocalizedString(@"tab1", @"")];
[tabBarItem2 setTitle:NSLocalizedString(@"tab2", @"")];
[tabBarItem3 setTitle:NSLocalizedString(@"tab3", @"")];
[tabBarItem4 setTitle:NSLocalizedString(@"tab4", @"")];

It works but I'm not happy with that either - for some reason I can't get a localizable MainWindow.xib properly working..

Irene