Localising UISegmentedControl (storyboard based)

In this iOS app, with UIKit in Swift, I create a UISegmentedControl in stroryboard.

I define the text for each segmentTitles in IB ,and need to localise.

I have tried in the main.xxx files

"id.segmentTitles" = ["a", "b", "c", "d", "e"];

to no avail.

I understand this is a very very old issue that UISegmentedControl are not localized from stroryboard:

https://stackoverflow.com/questions/12884751/uisegmentedcontrol-and-localization

I tried to use a similar approach as for UITextView (which are not either localized) helpTextView.text = NSLocalizedString("id.text", tableName: "Main", comment: "helpTextView")

But cannot make it.

I found a work around, by localising in code:

        let seg0 = NSLocalizedString("a", comment: "Segment")
        segmentedControl.setTitle(seg0, forSegmentAt: 0)

However, I get error messages in log unless I clear the segment titles in IB:

ERROR: id.segmentTitles[0] not found in table Main of bundle CFBundle 0x600003b101c0 

Is there a solution to this ?

Thanks for the post, it's been a while since I have use localization in Storyboards. And normally just use the localization files. Any reason why you are not?

You can actually localize UISegmentedControl directly from the Storyboard's .strings file, but you must use the exact Object ID of the segmented control and index each segment individually. Find the Object ID of your segmented control in the Identity Inspector in Interface Builder. Open your Main.strings file. Add the translations using the bracket index syntax:

If you prefer to keep your localizations in your main Localizable.strings file rather than dealing with Storyboard Object IDs, the cleanest workaround is to create a custom subclass of UISegmentedControl. This allows you to set your keys directly in Interface Builder and have the control translate itself automatically.

I hope this helps

Albert
  Worldwide Developer Relations.

Localising UISegmentedControl (storyboard based)
 
 
Q