xcode 从故事板本地化归因 UITextView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14459860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Localizing attributed UITextView from storyboard
提问by CracyD
I am using the storyboard and have a view where I have subclassed some UITextViews.
我正在使用故事板并有一个视图,其中我已经对一些 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.
我的问题是我使用ibtool --generate-strings-file
从情节提要中提取字符串进行本地化,然后ibtool -write
在另一个情节提要文件上使用以应用翻译后的字符串。
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.
当我使用ibtool
任何具有属性文本的 UITextViews 时,ibtool --generate-strings-file
命令会忽略并从结果字符串文件中省略。
Is it possible to extract attributed text from a storyboard for localization?
是否可以从故事板中提取属性文本进行本地化?
采纳答案by CracyD
I ended up concluding that it couldn't be done using ibtool
in the present version.
我最终得出结论,ibtool
在当前版本中无法使用它。
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.
相反,我让 Textview 成为纯文本视图,并使用我的子类解析其 text 属性,以便我可以创建NSMutableAttributedString
并设置所需的属性。
That enabled me to use ibtool
to extract the strings and still have an attributed textview.
这使我能够ibtool
用来提取字符串并且仍然具有属性文本视图。
回答by Solariane
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 ).
在 Xcode 6.1 上,最好的方法是将文本视图的属性文本复制到“BASE”RTF 文本中(例如使用 TextEdit 或直接从 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.
通过 TextEdit 方式,您需要将文本导入到您的项目中。显然,如果您是通过 Xcode 完成的,则无需导入。
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. ),
只需导入正确的版本(例如在 viewWillAppear 中),
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:
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!!
}
回答by m.y
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.
当然,您仍然可以通过代码来完成,您可以将字符串拆分为多个字符串并单独本地化它们。然后您可以指定 setAttributes:range: 以便您知道正确的属性将始终应用于字符串的正确范围。
回答by Abramodj
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):
这是一个我觉得非常有用的解决方法:只需使用翻译后的 TextView 创建一个 ViewController。详细说明(这里我只是将属性文本翻译成英文):
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.
1) 使用“新文件”->“带有 Xib 的 UIViewController”创建一个新控制器。将其命名为“AttributedTranslated”,并使用 Interface Builder 仅填充一个带有正在翻译的属性文本的 TextView。
2) In your main controller .m file, write down the following method:
2) 在你的主控制器 .m 文件中,写下以下方法:
- (BOOL)isEng {
return [[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"];
}
3) Define an "AttributedTranslated" object and a View in your .h file
3) 在 .h 文件中定义一个“AttributedTranslated”对象和一个视图
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".
4) 在主控制器的 xib 文件(或故事板)上,创建一个仅包含属性 textView(原始语言)的视图并将其链接到“attrView”。
5) On you viewDidLoad do something like the following:
5) 在 viewDidLoad 上执行如下操作:
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!
也许这不是最好的方法,但它确实允许人们轻松地在 Interface Builder 中翻译属性文本!