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 ?

Answered by Developer Tools Engineer in 890040022

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.

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.

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.

@DTS Engineer Thanks for the help.

I have tried to localise in the Main.Strings, but I probably miss the correct syntax. I used:

"ESW-3P-rRR.segmentTitles" = ["a", "b", "c", "d", "e"];

What is the right one ? Unfortunately your post does not include the example for syntax.

Accepted Answer

.strings files do not support arrays, so the syntax in a .strings file would be

"ESW-3P-rRR.segmentTitles[0]" = "a";
"ESW-3P-rRR.segmentTitles[1]" = "b";
Localising UISegmentedControl (storyboard based)
 
 
Q