Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 925
rkh Avatar asked Jan 11 '23 21:01

rkh


2 Answers

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

Quote:

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:

Hello%20World

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.

like image 161
user1459524 Avatar answered Jan 30 '23 13:01

user1459524


As my comment on Dmitry's answer cannot be formatted nicely, I repeat this as an answer here. The Swift version of his solution looks like this:

override func viewDidLoad() {
    super.viewDidLoad()
    for view in self.view.subviews {
        if let tv = view as? UITextView, ident = view.restorationIdentifier {
            tv.text = NSLocalizedString("\(ident).text", tableName: "Main", comment: "")
        }
    }
    // Do any additional setup after loading the view.
}

(Note that in Swift, NSLocalizedString replaces several Objective-C macros, one of them being NSLocalizedStringFromTable)

P.S.: Unfortunately, in iOS 10 this seems not to work any more. Instead, the call gives back the id that was supplied as first parameter (e.g. "abc-xy-pqr.text"). Any ideas?

like image 25
Stefan Avatar answered Jan 30 '23 14:01

Stefan