rkh rkh - 2 months ago 11x
iOS Question

Localization for UITextView in storyboard

So I have an application with different buttons, labels and some Text Views in storyboard where I entered the text directly in the storyboard. I enabled base localization and added a couple of languages.

This generated storyboards for Base (English) and the other languages with a list of items objectIDs.
I translated everything, and the labels and buttons (ALL OF THEM) work and show in the language I set the device to.

The text fields however keep showing the initial English text no matter which language I set...
Are there any extra steps involved for Text View?


So, I did some research, and it seems that in order for this to work correctly, the text for the UITextView needs to be set programmatically.

Source: Devforums.apple


as I understand it, strings such as the text property of a text view/field have to be set in code using NSLocalizedString. The first 1/2 hour of WWDC 2013 video session #219 Making Your App World Ready covers this if you have the time to watch it

So, it seems that the workaround (if you don't want to set the text programmatically) is to convert the strings file into a storyboard before shipping the app. This does seem to work as intended and shows the UITextView properly localized.

EDIT: Found another workaround that allows to keep .strings file.

In - (void)viewDidLoad:

for(UIView* v in self.view.subviews)
    if([v isKindOfClass:[UITextView class]])
        UITextView* txv = (UITextView*)v;
        NSString *loctxt = [txv.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        txv.text = NSLocalizedString(loctxt, @"");

This produces a Percent Escapes encoded string from whatever is inside the storyboard, like this:


In your Localizable.strings file, you use the above as the key, and this will produce the localized text in the app at runtime for the selected locale, like this:

"Hello%20World" = "Hallo Welt";

The Percent escaping takes care of all escape characters in the base string.