2

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.

Spider-Paddy
  • 303
  • 2
  • 14

5 Answers5

2

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

for (UITabBarItem *tabBarItem in tabBar)
{
  tabBarItem.title = NSLocalizedString(...);
}
Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
2

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
  • 303
  • 2
  • 14
  • 1
    Why do not use the inbuilt localization, i.e `NSLocalizedString (@"HOME", nil)` would return the value for every entry specified in the localization string – spacebiker Mar 15 '12 at 22:18
  • This is really not necessary as @XabierArrabal said. Just use the NSLocalized string and a strings file to localize your app. – mota Feb 23 '13 at 08:20
1

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
  • 14,245
  • 13
  • 50
  • 64
  • 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 Jun 24 '10 at 15:16
  • 1
    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 Jun 24 '10 at 20:08
1

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..

phi
  • 10,634
  • 6
  • 53
  • 88
0

I'm using:

NSArray *itemsTabBar = [[NSArray alloc] initWithArray:[self.tabBarController.tabBar items]];

    [[itemsTabBar objectAtIndex:0] setTitle:@"Contacts"];
    [[itemsTabBar objectAtIndex:1] setTitle:@"Settings"];