1

I have a problem. I want to translate the cancel button in a UIImagePickerController. I tried different solutions but none of them worked.

Please find my actual code that works for changing title text but not for cancel button :

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {    
    imagePicker.navigationBar.topItem?.title = localizedString("ES_CUSTOMERS_EDIT_PHOTOS_TITLE")

    imagePicker.navigationBar.backItem?.title = localizedString("ES_CANCEL_BUTTON")
}

Click here to see What i want to change

Thomtomdup
  • 33
  • 6
  • Look here: https://stackoverflow.com/a/27283220/2252297 it's in Objective-C if you need help with to translate it to swift let me know:) – Samer Murad Oct 30 '18 at 09:48
  • See: [How do I localize UIImagePickerController](https://stackoverflow.com/questions/8258391/how-do-i-localize-uiimagepickercontroller) and [How to change cancel button title in UIImagePickerController](https://stackoverflow.com/questions/16436495/how-to-change-cancel-button-title-in-uiimagepickercontroller) – Dávid Pásztor Oct 30 '18 at 09:51
  • Unless you want to show a different text it is not needed to translate it by yourself. If the user has the device in Spanish the button will show a message in Spanish. – vicegax Oct 30 '18 at 09:53
  • Thank you all for your reply. I try all of them solutions but nothing change. I update my post and added an image to give you more details. – Thomtomdup Oct 30 '18 at 10:27

2 Answers2

0

Try this,

    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

         viewController.navigationItem.title = "your text..!!"    

    }

In Info.plist add (or change if already exists) the Localisations property array to the one you need (eg - Spanish or German or anything)

Niki
  • 1,566
  • 1
  • 19
  • 36
0

If you have set the Cancel button using UIBarButtonItem.SystemItem of cancel (or Interface Builder), it will be localized for you automatically as others have mentioned. If you're building the Cancel button yourself for some reason (not needed/recommended if the text is just 'Cancel'), use the appropriate constructor for UIBarButtonItem, for example based on your screenshot:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("Cancel", comment: "Cancel the action"), style: .plain, target: <appropriate target here>)

If you're not sure how localization works in iOS, please see the Apple Localizing Your App documentation.

Mark Thormann
  • 2,522
  • 1
  • 21
  • 23