简体   繁体   English

iPhone:界面构建器中的NSLocalizedString?

[英]iPhone: NSLocalizedString in interface builder?

Is there any way to insert an NSLocalizedString in interface builder. 有没有办法在界面构建器中插入NSLocalizedString。 For example set a label text to a localized string instead of a static string? 例如,将标签文本设置为本地化字符串而不是静态字符串?

I really hate to create a property for every single item that requires a localized string. 我真的很讨厌为每个需要本地化字符串的项目创建属性。

Even if this post is old, for those interested in automatically localizing your IB files, check this out: https://github.com/angelolloqui/AGi18n 即使这篇文章是旧的,对于那些有兴趣自动本地化您的IB文件的人,请查看: https//github.com/angelolloqui/AGi18n

DISCLAIMER: I am the developer of the library 免责声明:我是图书馆的开发者

You can take advanced of the User Defined Runtime Attributes: 您可以使用用户定义的运行时属性的高级:

http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html

First define a new category for UILabel: 首先为UILabel定义一个新类别:

#import "UILabel+Localized.h"

@implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
     [self setText:NSLocalizedString(aText, nil)];
}
@end

Then in the interface builder, User Defined Runtime Attributes : 然后在界面构建器中,用户定义的运行时属性:

textLocalized String your string to localized textLocalized将字符串串起来进行本地化

在此输入图像描述

To avoid creating a bunch of categories, create just one that categorize the NSObject and then check for the isKindOfClass as suggested. 为了避免创建一堆类别,只需创建一个对NSObject进行分类的类,然后按照建议检查isKindOfClass。 See the code below: 请参阅以下代码:

#import "NSObject+Localized.h"

@implementation NSObject (Localized)

///
/// This method is used to translate strings in .xib files.
/// Using the "User Defined Runtime Attributes" set an entry like:
/// Key Path: textLocalized
/// Type: String
/// Value: {THE TRANSLATION KEY}
///
-(void) setTextLocalized:(NSString *)key
{
    if ([self isKindOfClass:[UILabel class]])
    {
        UILabel *label = (UILabel *)self;
        [label setText:NSLocalizedString(key, nil)];
    }
    else if ([self isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton *)self;
        [button setTitle:NSLocalizedString(key, nil) forState:UIControlStateNormal];
    }
    else if ([self isKindOfClass:[UIBarButtonItem class]])
    {
        UIBarButtonItem *button = (UIBarButtonItem *)self;
        [button setTitle:NSLocalizedString(key, nil)];
    }
}

@end

NSLocalizedString is not the recommended way to localize Interface Builder files. NSLocalizedString不是本地化Interface Builder文件的推荐方法。 Check out ibtool : 看看ibtool

http://www.bdunagan.com/2009/03/15/ibtool-localization-made-easy/ http://www.bdunagan.com/2009/03/15/ibtool-localization-made-easy/

I have done same thing as @OAK mentioned. 我和@OAK提到了同样的事情。 Here is full code. 这是完整的代码。

Interface Builder Localization HowTo Interface Builder Localization HowTo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM