plist 文件中的 Xcode 4.6 本地化 [英] Xcode 4.6 localization within plist file

查看:15
本文介绍了plist 文件中的 Xcode 4.6 本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在我的项目中添加更多语言并获得了字符串 &图形本地化没有太多麻烦.

I have started adding more languages to a project of mine and got strings & graphics localized without much trouble.

我还有最后一个问题,它与 plist 文件有关.

I have one last problem and it is with a plist file.

此 plist 文件包含应用程序的默认类别名称,并在字典中填充了英文字符串.

This plist file holds default category names for the app and is filled with English strings in a dictionary.

我的问题是:有没有办法本地化 plist 文件?我想向 plist 添加本地化字符串,但不知道如何.

My question is: is there a way to localize a plist file? I though about adding localized strings to the plist but could not figure out how.

我不想在代码中决定要采用哪个 plist 文件,因为默认 plist 文件在第一次使用时会被用户覆盖.

I dont want to have to decide in code what plist file to take since the default plist file gets overwritten by the user upon first use.

推荐答案

本地化 Plist 文件

这里更简单的解决方案是本地化整个 plist.通过这样做,您将为每种受支持的语言提供不同的 plist 文件.

Localized Plist files

Easier solution here would be to localize the entire plist. By doing so, you will have a different plist file for each supported language.

在您的项目中选择 plist 文件,然后在 File Inspector 菜单中选择 Localize.

Select the plist file in your project, and select Localize in the File Inspector menu.

它将为每种支持的语言创建一个包含 Plist 文件的新文件夹.

It will create a new folder containing a Plist file for each supported language.

发件人:

dummy.plist

dummy.plist

收件人:

> en.lproj
>  >  dummy.plist
> es.lproj
>  >  dummy.plist
> de.lproj
>  >  dummy.plist

<小时>

本地化的 Plist 内容

另一种解决方案是在 plist 中使用本地化字符串,并在打印出提取的字符串之前简单地调用 NSLocalizedString.

想象一下你有一个这样的 Plist:

Imagine you had a Plist like this:

您可以通过将键添加到 Localizable.strings 文件来简单地本地化其字符串.例如,在西班牙语中:

You can simply localize its strings by adding the keys to your Localizable.strings file. For example, in Spanish:

"My menu title" = "Mi título del menú";
"My menu description" = "Mi descripción del menú";

或者,我的建议是,将您的母语字符串从 Plist 移出到一个字符串文件中,并用可本地化的键替换 Plist 字符串:

Or, my recommendation, move also your native language strings out of the Plist to a string file and replace the Plist strings with a localizable key:

还有你的 Localizable.strings 用于英语:

And your Localizable.strings for Engligh:

"MY_MENU_TITLE" = "My menu title";
"MY_MENU_DESCRIPTION" = "My menu description";

和西班牙语:

"MY_MENU_TITLE" = "Mi título del menú";
"MY_MENU_DESCRIPTION" = "Mi descripción del menú";

我发现最新版本的新语言更易于维护和本地化,因为所有必需的字符串都在同一个文件中.

I've found the latest easier to maintain and easier to localize for new languages, as all the required strings are in the same file.

最后将您的代码更改为使用 NSLocalizableString 而不是从 Plist 文件中读取的纯字符串.例如,假设您有以下代码:

And finally change your code to use NSLocalizableString instead of the plain string read from the Plist file. For example, imagine you have the code:

NSDictionary* plistDict = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"menuElements" ofType:@"plist"]];

menuTitleLabel.text = plistDict[@"menuTitle"];
menuDescriptionLabel.text = plistDict[@"menuDescription"];

只需将其更改为:

NSDictionary* plistDict = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"menuElements" ofType:@"plist"]];

menuTitleLabel.text = NSLocalizedString(plistDict[@"menuTitle"], nil);
menuDescriptionLabel.text = NSLocalizedString(plistDict[@"menuDescription"], nil);

如果这是你的情况,你可以完全摆脱 plist 文件:

If this is your case you could get rid of the plist file completely:

menuTitleLabel.text = NSLocalizedString(@"MY_MENU_TITLE", nil);
menuDescriptionLabel.text = NSLocalizedString(@"MY_MENU_DESCRIPTION", nil);

这篇关于plist 文件中的 Xcode 4.6 本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆