xcode 如何在不为每种语言创建 xib 的情况下本地化 xib 视图中的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8815269/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:58:07  来源:igfitidea点击:

How can I localize text in xib views without creating xib for each language?

iosxcodeinterface-builder

提问by Oksana

I want to localize text inside xib view. Currently I have several xibs for each language.

我想本地化 xib 视图中的文本。目前我有每种语言的几个 xib。

Is it possible to localize the text inside xib file without creating separate xibs for each language?

是否可以在不为每种语言创建单独的 xib 的情况下本地化 xib 文件中的文本?

回答by Michael Dautermann

Sounds like you want to learn about .string files.

听起来您想了解 .string 文件。

Here is a related question you can look at for more info: Open .string files as an NSDictionary

这是一个相关的问题,您可以查看更多信息: Open .string files as an NSDictionary

回答by Dheeraj Vepakomma

Coming from Android background, I was asking the same question. Even Apple recommendscreating separate xib files for each language. Fortunately, this process can be automated using the ibtoolin a build script. It's described nicely in this article.

来自Android背景,我问了同样的问题。甚至 Apple 也建议为每种语言创建单独的 xib 文件。幸运的是,可以使用ibtool构建脚本中的自动执行此过程。在这篇文章中有很好的描述。

回答by Vineesh TP

Localization are applied for text not Xib. So, you don't need to consider xib file. Localization is easy not too Complicated. Check this link It will help You.

本地化应用于文本而不是 Xib。所以,你不需要考虑xib文件。本地化很容易不太复杂。检查这个链接它会帮助你。

http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial

http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial

回答by WiseGuy

This problem was driving me nuts.

这个问题让我发疯。

I came across this post and several others while trying to make xib localization easier for myself. I posted my method of including IBOutles for labels/buttons on this question, worked great for me, keeps all changes limited to the Localization.strings files.

我在尝试让自己更轻松地进行 xib 本地化时遇到了这篇文章和其他几篇文章。我在这个问题上发布了包含标签/按钮的 IBOutles 的方法,对我很有用,将所有更改限制在 Localization.strings 文件中。

https://stackoverflow.com/a/15485572/1449834

https://stackoverflow.com/a/15485572/1449834

回答by Jidong Chen

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

我认为直接本地化 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];

If you have UIButton and other UIView to handle, you could just hook awakeFromNib of UIView and localize each kind of view.

如果你有 UIButton 和其他 UIView 来处理,你可以只钩 UIView 的awakeFromNib 并本地化每种视图。