How to localize my app with Xcode 5?

懵懂的女人 提交于 2019-12-03 02:04:52

问题


This is a follow up question (and answer) on the How to localize my app with Xcode 4? question.

How do I localize my app with Xcode 5.x?


回答1:


It's quite simple once you understand it.

The first thing you want to do is add a localization file to your project. To do so, simply select your project's main group

,

then, from the toolbar, select File → New → File... (or just hold down ⌘N)

.

Under the Resource category, select Strings File

, and name it Localizable.strings (note that it is case sensitive)

.

Now that we have our localizable file, we can click on the Localize... button, in the File Inspector

.

Xcode is going to ask you if you want to localize the file, just click on Localize with Base selected

.

Now this next part is a bit tricky. We need to enter our project's Info section, to do so, click on the project file in Xcode's Navigator, then to your right you'll see a category named PROJECT, click on your project file under this category

.

Now we can add our desired language under the Localizations category. I'll add Norwegian

.

It's important that we only leave our Localizable.strings file checked in the menu that appears

.

Now we can expand our Localizable.strings file in the Navigator to see our localizable files

.

We now how our Base file (within our Localizable.strings file), which will be our app's "main language", and our previously selected language.

It's important to know that the structure of these files needs to be identical. You'll see what I mean in just a sec.

In our Base, I'll add a string named it_worked, and add it's localization

.

And in our previously selected language (In my case Norwegian), i'll add the same string it_worked (to keep the structure), but with a different localization

.

Now that we have our localized file, we can make our app read it when needed.

I added a UILabel to my app, so that we can make our app display the localized text.

[myLabel setText:NSLocalizedString(@"it_worked", nil)];

Now if I launch my app, we'll see our base language

,

and if I change the language of the simulator to Norwegian, we'll see our other language

.


回答2:


You don't need to add the uilabel and change the text on code.

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



来源:https://stackoverflow.com/questions/23308261/how-to-localize-my-app-with-xcode-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!