开发者

UIBarButtonSystemItem localizable

开发者 https://www.devze.com 2023-01-23 22:02 出处:网络
How can I make UIBarbuttonItem localizable? My implementation: UIBarButtonItem*cancelButton = [[UIBarButtonItem alloc]

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.


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.


Bump, but I think Vanya’s problem might be the “Localization native development region”/CFBundleDevelopmentRegion entry in Info.plist. If this is set to English and no localizations are explicitly made available as Costique explains, all system strings will be non-localized. But, set it to sk and - violà.


Not sure I understand your question correctly, but standard (system) bar button items are localized and thus automatically appear in the user-selected language. There are notable exceptions like UISwitch showing 0/1 instead of ON/OFF. What language are you having problems with?

That said, you can always use custom bar button items in place of system ones and provide necessary translations yourself. It's just overkill in most cases.


My implementation in Swift:

let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped(button:)))

This uses the system default language for "Done" text.

0

精彩评论

暂无评论...
验证码 换一张
取 消