如何使用 Xcode 4 本地化我的应用程序?

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

How to localize my app with Xcode 4?

xcodelocalizationinternationalizationxcode4

提问by jacob

I just updated to Xcode 4.

我刚刚更新到 Xcode 4。

Normally when localizing apps we add "Localizable.String" file to the project, and then navigate to "Get Info" menu and click the "Make It Localized" button.

通常在本地化应用程序时,我们将“Localizable.String”文件添加到项目中,然后导航到“获取信息”菜单并单击“使其本地化”按钮。

However, in Xcode 4, there is no "Get Info" menu.

但是,在 Xcode 4 中,没有“获取信息”菜单。

When I try to add a language it only effects the "InfoPlist.String" file (wich can only localize the app's name).

当我尝试添加一种语言时,它只会影响“InfoPlist.String”文件(只能本地化应用程序的名称)。

How may I add localization to my app in Xcode 4?

如何在 Xcode 4 中为我的应用程序添加本地化?

回答by Aleksander Azizi

It's simple once you understand it.

一旦你理解它就很简单了。

If you want to accomplish this with Xcode 5.x and iOS 7or Xcode 6.x and iOS 8, check out "How to localize my app with Xcode 5?" instead.

如果您想使用 Xcode 5.x 和iOS 7或 Xcode 6.x 和iOS 8完成此操作,请查看“如何使用 Xcode 5 本地化我的应用程序?”。

I liked SNR's link, but his answer was a bit short.

我喜欢SNR链接,但他的回答有点短。

Also, I see that this question is a bit old, and my answer may be different from older versions of Xcode. (I used Xcode v. 4.3.3)

另外,我看到这个问题有点老了,我的答案可能与旧版本的 Xcode 不同。(我使用 Xcode v. 4.3.3)

However, i have updated my answer to work with bothXcode 4.3.5and below + 4.4and above (and Xcode 5.x and 6.x here: How to localize my app with Xcode 5?).

但是,我已经更新了我的答案,以便同时使用Xcode 4.3.5及以下 + 4.4及以上(以及 Xcode 5.x 和 6.x:如何使用 Xcode 5 本地化我的应用程序?)。

To begin you should add a new "Strings File" in the iOS Resource category.

首先,您应该在 iOS 资源类别中添加一个新的“字符串文件”。

Strings File

字符串文件

Next, create the file as "Localizable.strings". Create As

接下来,将文件创建为“Localizable.strings”。 创建为

When the file is created, select it and open File Inspector .

创建文件后,选择它并打开 File Inspector 。

The Localizable FileFile Inspector

可本地化的文件文件检查器

EDIT: Things have changed (a bit) with the new Xcode 4.4, 4.5 (and above) (for iOS 6 support). If you'r Notusing Xcode 4.4 or above, joust skip this step.

编辑:随着新的 Xcode 4.4、4.5(及更高版本)(支持 iOS 6),事情发生了变化(有点)。如果您使用的不是Xcode 4.4 或更高版本,请跳过此步骤。

{The Xcode 4.4, 4.5 (and above)way:

{Xcode 4.4、4.5 (及以上)方式:

Click the "Make localized" button Make localized button

单击“进行本地化”按钮 制作本地化按钮

Now head over to your Main Project page. Main Project page

现在转到您的主项目页面。 主项目页面

And click the "+" button under Localization, and select the languages you want to support.

然后单击“本地化”下的“+”按钮,然后选择您要支持的语言。

(I'll select German / Deutsch)

(我会选择德语/德语)

List of languages

语言列表

Now a window will appear asking you what files you want to localize, make sure Onlythe "Localizable.strings" file is selected and click Finish.

现在将出现一个窗口,询问您要本地化哪些文件,确保选择“Localizable.strings”文件并单击“完成”。

Only the "Localizable.strings" file is selected

仅选择“Localizable.strings”文件

}

}

{The Xcode 4.3.5and below way:

{Xcode 4.3.5及以下方式:

Click the "+" button under Localization, and select the languages you want to support.

单击“本地化”下的“+”按钮,然后选择要支持的语言。

(I'll select German / Deutsch)

(我会选择德语/德语)

List of languages

语言列表

}

}

.

.

.

.

.

.

.

.

You should now see that you have two files under the "Localizable.strings" file.

您现在应该看到在“Localizable.strings”文件下有两个文件。

Localizable Files

本地化文件

Next, add your localization strings inside both of the localization files.

接下来,在两个本地化文件中添加本地化字符串。

English

英语

German

德语

Now here comes the coding part.

现在到了编码部分。

Here i'll simply declare a UILabeland set it's text to the Localizable file string.

在这里,我将简单地声明 aUILabel并将其文本设置为 Localizable 文件字符串。

Declare:

宣布:

IBOutlet UILabel *testLabel;

And Inside ViewDidLoad i'll set the text using NSLocalizedString:

在 ViewDidLoad 中,我将使用NSLocalizedString以下方法设置文本:

[testLabel setText:NSLocalizedString(@"TEST", nil)];

To finish up, just connect our testLabelin "Interface Builder".

最后,只需连接我们testLabel的“Interface Builder”。

Connect IBOutlet

连接IBOutlet

Now you should be able to run the Project and see the localized string. And if you change the language, you should see the localized string change as well.

现在您应该能够运行该项目并查看本地化的字符串。如果您更改语言,您应该也会看到本地化的字符串发生变化。

English ResultChange the languageGerman Result

英语成绩更改语言德国成绩

回答by Pierre

Select the file you want to localize and klick on the file inspector in the utilities section. There is a section Localization

选择要本地化的文件,然后单击实用程序部分的文件检查器。有一节本地化

回答by pragmatic

You can view the file info (and add localizations) when you select the file and open the "File Inspector" via the main menu's "View" -> "Utilities" -> "File Inspector".

当您选择文件并通过主菜单的“查看”->“实用程序”->“文件检查器”打开“文件检查器”时,您可以查看文件信息(并添加本地化)。

回答by korat prashant

first you need to add new Localizable.string file and select it and go to View" -> "Utilities" -> "File Inspector click on + button to add language in localization section. also check it XCode 4, Adding localization

首先,您需要添加新的 Localizable.string 文件并选择它并转到“查看”->“实用程序”->“文件检查器”,单击 + 按钮在本地化部分添加语言。还检查它 XCode 4,添加本地化

回答by Kaibin

the way of Localization in xcode4 has changed! right click the localizable.strings, and to add support for another language, simply click on the ‘+' in that ‘Localization' pane on the right.

xcode4 本地化的方式已经改变了!右键单击 localizable.strings,并添加对另一种语言的支持,只需单击右侧“本地化”窗格中的“+”。

回答by rafaperez

You can take advanced of the User Defined Runtime Attributes:

您可以使用用户定义的运行时属性:

http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html

http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html

First define a new category for UILabel:

首先为 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

textLocalized 字符串您要本地化的字符串

enter image description here

在此处输入图片说明

And you don't need to declare the IBOutlet, and don't need code this in the ViewDidLoad [testLabel setText:NSLocalizedString(@"TEST", nil)];

而且你不需要声明IBOutlet,也不需要在ViewDidLoad [testLabel setText:NSLocalizedString(@"TEST", nil)];

回答by Nick N

If you want a dynamic way of switching languages inside your app AND using a Pods based solution check out:

如果您想要在应用程序内以动态方式切换语言并使用基于 Pods 的解决方案,请查看:

https://github.com/nullic/DPLocalizationManager

https://github.com/nullic/DPLocalizationManager

Install with: pod 'DPLocalization', '~> 1.2'

安装: pod 'DPLocalization', '~> 1.2'

It supports the same standard localization files but also uses dynamic linking using User Defined runtime attributesand also inside code such as viewDidLoad.

它支持相同的标准本地化文件,但也使用动态链接User Defined runtime attributes和内部代码,例如viewDidLoad.

Some examples in the example project:

示例项目中的一些示例:

 self.startup.text = DPLocalizedString(@"TITLE", nil);
 self.label.autolocalizationKey = @"LABEL_TEXT";
 [self.label updateAutolocalizationArguments:@[@"Hello", @1234567890, [NSDate date]]];
 self.autolocalizationKey = @"TITLE";

Download the git project that includes example project showing several examples.

下载包含显示多个示例的示例项目的 git 项目。

回答by VisualFrames

Or you could use TraductoProto automate this process for you and save a lot oftime. It is a Mac app that integrates well with Xcode.

或者您可以使用TraductoPro为您自动化此过程并节省大量时间。它是一款与 Xcode 集成良好的 Mac 应用程序。