Objective-c – UIBarButtonSystemItem localizable

iphonelocalizationobjective c

How can I make UIBarbuttonItem localizable?

My implementation:

UIBarButtonItem         *cancelButton = [[UIBarButtonItem alloc] 
                                             initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                             target:self
                                             action:@selector(cancel)];

Originally I thought that it is automatic, because it looks easy to make it like this, but looks like not.

EDIT1: Official Apple dox says that cancel, done, edit, save buttons are localized, but not sure how to make it.

Best Answer

Alright, I think I know what's going on.

UIKit decides in which language to display strings, based on the value of [[NSBundle mainBundle] preferredLocalizations]. The idea behind this is to use translated resources whenever possible, falling back to English otherwise. For example, if you don't provide the translation of your app in Finnish and Finnish is selected in Settings->General->International->Language, your app will be in English. It may be a bit more complicated (UIKit may go through all the list of languages in the order displayed in Settings.app, trying to find the first language your app has translations for), but the point stands.

The above may be too obvious to miss a crucial nuance. The language determined by the above algorithm is used for the app's whole UI. For example, if the app bundle doesn't contain sk.lproj, nothing will be displayed in Slovak. In fact, it does make sense because otherwise some parts of UI would be in Slovak, other parts, in English.

Open the compiled app's bundle to see which *.lproj folders are there. The same set, sorted according to user preferences, will be returned by [[NSBundle mainBundle] preferredLocalizations]. All localizable strings, including system bar button items, will be displayed in one of those languages. If you don't support, for example, Russian, the whole UI will appear in another language, even if Russian is selected in Settings.app.

If this is the case with your app indeed, there are two right things and one wrong thing to do:

  • right: provide Slovak translation for each and every string displayed in your app, translate any text-containing nibs to Slovak too;
  • right: ignore Slovak altogether if you cannot (or don't need to) support it;
  • wrong: select any strings file or any nib, open Xcode's inspector, click "Add Localization…" (if the button is disabled, first click "Make File Localizable"), type "sk", click "Add" and build the project. This will make UIKit think your app is translated to Slovak, and system bar button items will automatically appear in Slovak when it's selected in Settings.app.

If you see a different behavior, there may be something wrong with the project/build/built app. For example, I noticed that when you make a file localized, its non-localized copy doesn't get deleted from the previously built app bundle.

Related Topic