如何本地化 iOS 故事板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8312936/
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
How to localize an iOS storyboard
提问by dhrm
I've an iPhone storyboard with some views. For instance, a navigation item title is named News
, which should be translated for other languages.
我有一个带有一些视图的 iPhone 故事板。例如,导航项标题名为News
,应翻译为其他语言。
When I add a new localization to my storyboard, it created a duplicate of my current storyboard for the new language. Here I can change the title for the navigation item, but for me it does not seem very useful. What if my storyboard contains 100 views and I need to support 10 languages? If I need to change something in my original storyboard, I have to make the same changes for all languages. That seems very odd. In which situations can this be useful?
当我向我的故事板添加新的本地化时,它为新语言创建了我当前故事板的副本。在这里我可以更改导航项的标题,但对我来说似乎不是很有用。如果我的故事板包含 100 个视图并且我需要支持 10 种语言怎么办?如果我需要更改原始故事板中的某些内容,我必须对所有语言进行相同的更改。这看起来很奇怪。这在哪些情况下有用?
What can I do instead? Should I have only the english storyboard and manually translate each element in the ViewController using NSLocalizedString
?
我可以做什么?我应该只有英文故事板并使用 手动翻译 ViewController 中的每个元素NSLocalizedString
吗?
采纳答案by freytag
You can do the localization by changing the titles of the UI elements in code:
您可以通过更改代码中 UI 元素的标题来进行本地化:
self.title = NSLocalizedString("News", nil);
If you want to localize your app in Dutch for example, you would have this in Dutch.lproj/Localizable.strings
:
例如,如果你想用荷兰语本地化你的应用程序,你可以在Dutch.lproj/Localizable.strings
:
"News" = "Nieuws";
You can then do this for every UI element and language.
然后,您可以为每个 UI 元素和语言执行此操作。
回答by Collin
In iOS 6 there is a Project setting under the Info tab that says "Use Base Internationalization". If you check the box, it will pull all of the strings out of the Storyboard and into internationalized .strings files.
在 iOS 6 中,信息选项卡下有一个项目设置,上面写着“使用基础国际化”。如果您选中该框,它会将所有字符串从 Storyboard 中拉出并放入国际化的 .strings 文件中。
This way you don't have to have multiple copies of the Storyboard.
这样您就不必拥有多个 Storyboard 副本。
回答by freytag
As of iOS 6 you can decide to use Base Internationalization: All storyboard and xib/nib files exist only once in a folder named Base.lproj. The localization of the GUI elements is placed in related .strings files in each localization directory.
从 iOS 6 开始,您可以决定使用 Base Internationalization:所有 storyboard 和 xib/nib 文件在名为 Base.lproj 的文件夹中只存在一次。GUI 元素的本地化放置在每个本地化目录中的相关 .strings 文件中。
For example "MainStoryboard.storyboard" will be placed in Base.lproj. An associated file called "MainStoryboard.strings" is placed in en.lproj and whatever localization you apply.
例如“MainStoryboard.storyboard”将被放置在Base.lproj 中。一个名为“MainStoryboard.strings”的关联文件放置在 en.lproj 和您应用的任何本地化中。
This is really very handsome, especially in combination with layout constraints!
这个真的很帅,尤其是结合布局约束!
回答by reabrn
You might find this video tutorial here useful:
您可能会发现这里的视频教程很有用:
http://www.youtube.com/watch?v=cF1Rf02QvZQ
http://www.youtube.com/watch?v=cF1Rf02QvZQ
The approach is to have a separate storyboard for each language to localize, then let a script take care of propagating the changes you make in the original storyboard for all other languages.
方法是为每种语言有一个单独的故事板进行本地化,然后让脚本负责传播您在所有其他语言的原始故事板中所做的更改。
回答by Jens Kohl
For apps with a deployment target of iOS 5 and storyboards I use something like this to localize my tabs where my first ViewController on a storyboard is a UITabBarController
:
对于部署目标为 iOS 5 和故事板的应用程序,我使用类似这样的方法来本地化我的选项卡,其中我在故事板上的第一个 ViewController 是UITabBarController
:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// ...
if ([self.window.rootViewController isKindOfClass:UITabBarController.class]) {
UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
// Localize tab items
NSArray *tabBarItems = tabBarController.tabBar.items;
[tabBarItems enumerateObjectsWithOptions:NSEnumerationConcurrent
usingBlock:^(UITabBarItem *item, NSUInteger tabIndex, BOOL *stop) {
NSString *keyName = [NSString stringWithFormat:@"tabBarItem%i", tabIndex];
item.title = NSLocalizedString(keyName, @"TabBarItems");
}];
} else {
// The root view controller is not the UITTabBarController
}
// ...
}
and have something like this in my Localizable.strings
files:
并在我的Localizable.strings
文件中有这样的内容:
// MARK: TabBar
"tabBarItem0" = "My First Tab Label";
"tabBarItem1" = "My Second Tab Label";
"tabBarItem2" = "My Third Tab Label";
回答by blurkidi
You might find Polyglot Localization useful as well. In stead of calling NSLocalizedString() in code, you can directly specify the key it in your Storyboard. You can avoid unnecessary outlets and reduce boilerplate code.
您可能会发现多语言本地化也很有用。无需在代码中调用 NSLocalizedString(),您可以直接在 Storyboard 中指定它的键。您可以避免不必要的出口并减少样板代码。
回答by James Campbell
I suggest you learn the commandline ibtool it helps asist you in localizing all of your storyboards :) Here is a tutorial here http://www.albertmata.net/articles/introduction-to-internationalization-using-storyboards-on-ios-5.html
我建议您学习命令行 ibtool,它有助于帮助您本地化所有故事板:) 这里有一个教程http://www.albertmata.net/articles/introduction-to-internationalization-using-storyboards-on-ios- 5.html