我可以将 uilabel 链接到 Interface Builder 中的可本地化字符串吗?
已经用谷歌搜索过,但没有找到解决方案:
基本上,我设置了一个 Localized.strings ,我在代码中使用它。然而,如果我能以某种方式在我的 XIB 中引用这些值,那就太好了,这样我就可以避免为每种语言创建一个烦人的 XIB...
这可能吗?
have googled around but found no solution:
Basically, i have a Localizable.strings set up, which i'm using in my code. However, it would be really sweet if i somehow could just refer those values in my XIB's too, so that i can avoid having to create one annoying XIB per language...
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以实现 UILabel 的子类,并让它在从笔尖初始化时自动提取本地化值。
在 XIB 中,您只需设置标记(如果您愿意,也可以设置英文文本)并让 UILabel 从该值中提取本地化文本。
You could implement a subclass of UILabel and have it automatically pull the localized value when it's initialized from the nib.
In the XIB you would then just set the token (or english text if you prefer) and have UILabel pull the localized text from this value.
我向您介绍快捷方式
1。创建UILabel的子类
2.在 IB 中将类设置为 LocalizedLabel
3.直接在 IB 中输入 Localized.strings 中的密钥
瞧,现在花更少的时间来创建无用的 IBOutlet,只是为了使其成为可本地化的 UILabel。
注意
这不会使本地化字符串显示在界面生成器中,而只是显示“键”。因此,在构建 TextView 时,请记住相应地设置约束
I present to you the swifty way
1. Create a subclass of UILabel
2. Set the class to be LocalizedLabel in IB
3. Enter your key from Localizable.strings directly in IB
Voila, now spend less time on creating useless IBOutlets just for making it a localizable UILabel.
NOTE
This does not make the localized string show in the interface builder, but rather just the "key". So when building your TextView's remember to set constraints accordingly
无法直接使用 Interface Builder 从
Localized.strings
进行设置。您可能想要翻译 NIB 的原因是每种语言的单词长度可能有所不同。因此,您可以更改每种语言的笔尖布局。
您可以在
UIViewController
的viewDidLoad
方法中设置它们。There is no way to set then from the
Localizable.strings
directly with Interface Builder.The reason you might want to translate the NIB is because the length of the words may vary in each language. Thus allowing you to change the layout of you nib per language.
What you could set them in the
viewDidLoad
method of yourUIViewController
.NSLocalizedString
的优点是易于实现。风险在于您可能会忘记为新的 Gui 元素添加条目。您可以使用
itool
从原始xib文件中转储字符串,翻译这些转储的字符串,然后将翻译后的字符串加载到other-language-xib文件中。与
NSLocalizedString
相比,这样做有一些优点。创建 XIB 时,将其设置为本地化(如前所述),然后针对此原始 xib 运行
itool
以提取字符串。将字符串文件翻译成所需的语言,然后将翻译加载到本地化的 xib 中。
如果您在 google 上搜索
ibtool --generate-strings-file
,您应该会找到一些教程和示例。语法应类似于:
Resources/English.lproj 目录中的
$xibfile.strings
将包含原始(英语)字符串。翻译后的$xibfile.strings
必须位于相应的xx.lproj
目录中。如果您将构建脚本添加到 xcode,则每次构建时都会调用翻译的加载。
NSLocalizedString
has the advantage of being easy to implement. Risk is that you might forget to add an entry for a new Gui element.You can use
itool
to dump the string from the original xib file, translate these dumped strings and then load the translated string into the other-language-xib file.There are some advantages doing it this way compared to
NSLocalizedString
.When you create a XIB you set it to being localized (as before), then run
itool
against this original xib to extract the strings.Translate the strings file into the desired language then load the translation into the localized xib.
If you google for
ibtool --generate-strings-file
you should find a few tutorials and examples.The syntax should be similar to:
$xibfile.strings
in the Resources/English.lproj directory will contain the original (English language) strings. The translated$xibfile.strings
must go in the appropriatexx.lproj
directory.If you add a buildscript to xcode then the loading of the translation will be called at each build.
IB 无法使用 Localized.strings 中的字符串,但还有另一个选项,称为基本国际化。
简而言之:您可以创建附加的单独的 .strings 文件到每种所需语言的 xib/storyboard。在结果中,您将获得 xib 及其针对每种语言的自己的翻译。所以它可以复制你的一些值
Localized.strings,但是比通过代码设置或者复制xib文件更清晰的方式。
请参阅有关 Base 国际化的 Apple 文档。
IB cannot use strings from Localizable.strings, but there is another option called Base Internationalization.
In short: you can create separate .strings file attached to the xib/storyboard for every required language. In the result you will have xib with it's own translations for each language. So it can duplicate some of the values that you have in
Localizable.strings, but it's more clear way than setting through the code or copying xib files.
Please refer to the Apple Documentation about Base Internationalization.
另一种快捷方式,但带有扩展:
然后您将能够直接从 IB 编写本地化密钥,并且它将在运行时进行本地化。
Another swifty way, but with extension :
You will then be able to write the localization key directly from IB, and it will be localized at runtime.
您可以在代码中执行此操作:
但我认为除了本地化整个笔尖之外,没有其他方法可以仅使用笔尖来完成此操作。我过去肯定没有找到其他方法。
You can do this in code:
But I don't think there is a way to do it with just nibs other than localizing the entire nib. I've certainly not found another way in the past.
这是我的版本。希望有帮助。
This is my version. Hope it helps.
Apple 支持的本地化 .xib 文件的方法。这允许您本地化每种语言/国家/地区的文本和布局。
The Apple-supported way to the localize the .xib file. This allow you to localize the text and layout for each language/country.