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 ?
The easiest way to do this is to use a String Catalog rather than .strings files, as it automatically includes the correct keys and you don't need to guess at their formats.
Starting from a storyboard that is not yet localized in the File Inspector, click the Localize… button. Then in the alert dialogue, check the box for using a String Catalog.
Alternatively, you can migrate an existing storyboard with .strings files to String Catalogs by control-clicking on the file in the File Navigator and selecting "Migrate to String Catalog".
From then on, every time you build the project, the String Catalog will update to include the correct keys to cover what's in the storyboard.