CracyD
CracyD

Reputation: 368

Localizing attributed UITextView from storyboard

I am using the storyboard and have a view where I have subclassed some UITextViews.

My problem is that I am using ibtool --generate-strings-file to extract strings from the storyboard for localization and afterwards use ibtool -write on another storyboard file to apply the translated strings.

When I use ibtool any UITextViews that have attributed text is ignored by the ibtool --generate-strings-file command and omitted from the resulting strings file.

Is it possible to extract attributed text from a storyboard for localization?

Upvotes: 18

Views: 6654

Answers (4)

Solariane
Solariane

Reputation: 191

on Xcode 6.1, the best way is to copy the attributed text of a text view into a “BASE” RTF text ( using TextEdit for example or directly from XCode > New File > ressources > RTF ).

Going through the TextEdit way, you need to import your text into your project. Obviously, if you have done it through Xcode, nothing to import.

then just you use the utilies panel to find the “localize..." button which will do it's deed for you.

to import the correct version just do ( in viewWillAppear for ex. ),

 NSURL *url = [[NSBundle mainBundle] URLForResource:[fileName stringByDeletingPathExtension] withExtension:[fileName pathExtension]];
NSError *error;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithFileURL:url
                                                                           options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
                                                                documentAttributes:nil
                                                                             error:&error];

[_originalMessage setAttributedText:attributedString];

Update for Swift 4:

var attrString: NSAttributedString?
let fileUrl: URL = Bundle.main.url(forResource: "mytextfile", withExtension: ".rtf")!

do {
   attrString = try NSAttributedString(url: fileUrl, options: [.documentType:NSAttributedString.DocumentType.rtf], documentAttributes: nil)
} catch {
    // Somebody do something!!        
}

Upvotes: 19

Abramodj
Abramodj

Reputation: 5879

Here's a workaround which I find quite useful: just create a ViewController with the translated TextView. In details (here I just translate the attributed text into english):

1) Crete a new Controller with "New File" -> "UIViewController with Xib". Name it "AttributedTranslated" and fill it with just a TextView with the attributed text being translated, using Interface Builder.

2) In your main controller .m file, write down the following method:

- (BOOL)isEng {
    return [[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"];
}

3) Define an "AttributedTranslated" object and a View in your .h file

IBOutlet UIView            *attrView;    
AttributedTranslated       *attr;

4) On the xib file (or storyboard) of your main controller, create a View containing just the attributed textView (in the original language) and link it to "attrView".

5) On you viewDidLoad do something like the following:

if ([self isEng]) {

    desc = [[Description alloc] init];
    [attrView addSubview:attr.view];
}

Maybe it's not the best way to do it, but it does allow one to translate the attributed text in Interface Builder, easily!

Upvotes: -3

m.y
m.y

Reputation: 691

It wouldn't make sense to localize attributed text from the storyboard as you would need to know where to apply the attributes after the translation. The word order might have changed and where you would have specified some blue bold text for instance might no longer make sense.

You can of course still do it via code where you can split up your string in multiple ones and localize them individually. Then you can specify setAttributes:range: so that you know that the right attributes will always be applied on the right range of the string.

Upvotes: 4

CracyD
CracyD

Reputation: 368

I ended up concluding that it couldn't be done using ibtool in the present version.

Instead I let the Textview be a plain text view and, using my subclass, parsed its text property so I could make a NSMutableAttributedString and set the needed properties.

That enabled me to use ibtool to extract the strings and still have an attributed textview.

Upvotes: 5

Related Questions