iPhone:界面生成器中的 NSLocalizedString?

发布于 2024-09-30 11:23:26 字数 110 浏览 10 评论 0原文

有什么方法可以在界面生成器中插入 NSLocalizedString 吗? 例如,将标签文本设置为本地化字符串而不是静态字符串?

我真的很讨厌为每个需要本地化字符串的单个项目创建一个属性。

Is there any way to insert an NSLocalizedString in interface builder.
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦行七里 2024-10-07 11:23:26

即使这篇文章很旧,对于那些对自动本地化 IB 文件感兴趣的人,请查看以下内容:https://github.com/angelolloqui/AGi18n

免责声明:我是该库的开发者

Even if this post is old, for those interested in automatically localizing your IB files, check this out: https://github.com/angelolloqui/AGi18n

DISCLAIMER: I am the developer of the library

诗酒趁年少 2024-10-07 11:23:26

您可以高级使用用户定义的运行时属性:

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

首先为 UILabel 定义一个新类别:

#import "UILabel+Localized.h"

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

然后在界面生成器中,用户定义的运行时属性:

textLocalized String 将您的字符串本地化

< img src="https://i.sstatic.net/oaKLE.png" alt="在此处输入图像描述">

You can take advanced of the User Defined Runtime Attributes:

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

First define a new category for 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

enter image description here

梦旅人picnic 2024-10-07 11:23:26

为了避免创建一堆类别,请仅创建一个对 NSObject 进行分类的类别,然后按照建议检查 isKindOfClass。请参阅下面的代码:

#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

To avoid creating a bunch of categories, create just one that categorize the NSObject and then check for the isKindOfClass as suggested. 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
浅唱ヾ落雨殇 2024-10-07 11:23:26

NSLocalizedString 不是本地化 Interface Builder 文件的推荐方法。查看ibtool

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

NSLocalizedString is not the recommended way to localize Interface Builder files. Check out ibtool:

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

梦途 2024-10-07 11:23:26

我已经做了@OAK 提到的同样的事情。这是完整的代码。

界面生成器本地化指南

I have done same thing as @OAK mentioned. Here is full code.

Interface Builder Localization HowTo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文