xib 文件的 iPhone 本地化

发布于 2024-09-18 13:55:50 字数 83 浏览 3 评论 0原文

我刚刚熟悉 xib 文件的本地化,想知道是否有一种方法可以通过直接引用 plist 来本地化 xib 中的字符串...

欣赏一些想法..

I am just getting familiar with the localization of xib files and was wondering if there's a way of localizing the strings within the xib by refering to the plists directly...

Appreciate some thoughts..

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

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

发布评论

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

评论(4

北城挽邺 2024-09-25 13:55:50

如果您不想直接本地化 .xib 文件,则可以将它们包含的文本提取到 .strings 文件中,并且在翻译 .strings 文件后,将它们注入回 .xib 文件中以生成本地化版本。这些提取/注入操作是使用 ibtool 实用程序完成的。

我在此网站上找到了有关如何执行此操作的详细说明。

If you don't want to localize the .xib files directly, you can extract the text they contain into .strings files and, once the .strings files are translated, inject them back into your .xib file to produce localized versions. These extract/inject operations are done using the ibtool utility.

I found detailed instructions on how to do this on this website.

救赎№ 2024-09-25 13:55:50

我的方法有点偏离@Clafou 的答案,但它可能更简单一些。我只需在 .h 控制器中为按钮和标签字符串(在 xib 上创建)设置 IBOutlet。

 @interface DetailViewController : UIViewController {


IBOutlet UILabel *TitleLabelMain;

}

然后转到我的 .m 控制器,并使用 NSLocalizedStrings 为这些标签和按钮指定一个值。

- (void)viewWillAppear:(BOOL)animated {

TitleLabelMain.text = NSLocalizedString(@"titleLabel",nil);

当然,您需要在 Localized.strings 中定义一个值

"titleLabel" = "THIS TEXT IS LOCALIZED!";

My method stems a bit off @Clafou answer, however it may be a bit more straightforward. I simply set IBOutlets for my button and label strings (created on xib) within my .h controller.

 @interface DetailViewController : UIViewController {


IBOutlet UILabel *TitleLabelMain;

}

Then went to my .m controller and gave these labels and buttons a value with NSLocalizableStrings.

- (void)viewWillAppear:(BOOL)animated {

TitleLabelMain.text = NSLocalizedString(@"titleLabel",nil);

And of course you will need a value defined in Localizable.strings

"titleLabel" = "THIS TEXT IS LOCALIZED!";
眼眸 2024-09-25 13:55:50

Apple 建议的方法是将字符串导出到 .lproj 包中的 .strings 文件中,该文件将由 Cocoa 的本地化框架切换。

Xcode 可以从 xib 生成 .strings 文件,这使得本地化变得非常简单。

右键单击 Xcode 中的 xib 文件,然后选择获取信息。选择常规选项卡,然后单击底部的使文件可本地化。然后,您可以通过单击同一选项卡上的添加本地化来添加本地化。

我推荐本教程 了解分步信息(以及漂亮的图片)。

The Apple suggested way to do this is to exprot the strings into .strings files in .lproj bundles, that will be switched out by Cocoa's localization framework.

Xcode can generate the .strings files from the xib, which make localization pretty straight forward.

Right click on the xib file in Xcode, and choose Get Info. Select the General tab and on the bottom click Make File Localizable. Then you will be able to add localizations by clicking Add Localization on that same tab.

I would recommend this tutorial for step-by-step information (and pretty pictures).

红焚 2024-09-25 13:55:50

我认为直接本地化 xibs 不是一个好的选择。我正在使用 https://github.com/steipete/Aspects 来挂钩 UILabel 的 awakeFromNib 方法和在那里本地化文本。例子:

 #define Localized(_v_) NSLocalizedString(_v_, nil)
 NSError *err = nil;
[UILabel aspect_hookSelector:@selector(awakeFromNib)
                 withOptions:AspectPositionAfter
                  usingBlock:^(id<AspectInfo> aspectInfo) {
                      UILabel *label = aspectInfo.instance;
                      NSString *lStr = Localized(label.text);
                      if (lStr) {
                          label.text = lStr;
                      }
} error:&err];

I think localize xibs directly is not an good option. I'm using https://github.com/steipete/Aspects to hook the awakeFromNib method of UILabel and localize text there. Example:

 #define Localized(_v_) NSLocalizedString(_v_, nil)
 NSError *err = nil;
[UILabel aspect_hookSelector:@selector(awakeFromNib)
                 withOptions:AspectPositionAfter
                  usingBlock:^(id<AspectInfo> aspectInfo) {
                      UILabel *label = aspectInfo.instance;
                      NSString *lStr = Localized(label.text);
                      if (lStr) {
                          label.text = lStr;
                      }
} error:&err];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文