是否有选项可以在Interfacebuilder中隐式本地化标签 [英] Is there an option to implicitly localise labels in Interfacebuilder
问题描述
在博客文章的某处,我偶然发现了一个字符串文件,如下所示:
Somewhere in a blog post I stumbled upon a strings file which looked like this:
// de.lproj/Localizable.strings
"This is the title" = "Das ist der Titel"
给我看起来接口构建器中的实际标签由编译器处理,因此不再需要使用 NSLocalizedString(@SOME_IDENTIFIER,@);
进行显式转换。
To me this looked like the actual labels in Interface builder were processed by the compiler so that no explicit translations using NSLocalizedString(@"SOME_IDENTIFIER", @"");
would be necessary any more.
我现在的问题是,是否存在某种快捷方式,或者我是否需要在我的视图中本地化所有单个标签,例如在 awakeFromNib
方法中。
My question now, is whether there is some kind of shortcut or do I need to localise all my individual labels on my view e.g. in the awakeFromNib
method.
推荐答案
我找到了一条通往将流程半自动化,这样我就不必这样做了:
I have figured out a way to semi-automate the process so that I don't have to do this:
label1.text = NSLocalizedString(@"label1_key", @"");
label2.text = NSLocalizedString(@"label2_key", @"");
....
labeln.text = NSLocalizedString(@"labeln_key", @"");
因此,对于应本地化的所有标签,我将其文本设置为 __ KeyForLabelX < IB中的/ code>。然后在viewcontroller的
viewWillAppear
方法中,我遍历视图中的项目并将文本设置为本地化值:
So for all labels which should be localised I set their text to __KeyForLabelX
in IB. Then in the viewWillAppear
method of the viewcontroller I loop through the items on the view and set the text to the localized value:
for (UIView *view in self.view){
if([view isMemberOfClass:[UILabel class]]){
UILabel *l = (UILabel *)view;
BOOL shouldTranslate = [l.text rangeOfString:@"__"].location != NSNotFound;
NSString *key = [l.text stringByReplacingOccurrencesOfString:@"__" withString:@"TranslationPrefix"];
if (shouldTranslate){
l.text = NSLocalizedString(key, @"");
}
}
}
我的.strings文件然后看像这样:
My .strings file then look like this:
"TranslationPrefixKeyForLabelX" = "Translation of Label X";
更新要进一步调整机制,您还可以检查其他 UIView
类似 UIButtons
, UITextField
s(包括提示文字)等等。
Update: To further adapt the mechanism you could also check for other UIView
s like UIButtons
, UITextField
s (including prompt text) etc.
这篇关于是否有选项可以在Interfacebuilder中隐式本地化标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!