plist 文件中的 Xcode 4.6 本地化

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

Xcode 4.6 localization within plist file

objective-cxcodelocalizationplist

提问by Sebastian Flückiger

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

我已经开始向我的项目添加更多语言,并且没有太多麻烦地对字符串和图形进行了本地化。

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 文件保存了应用程序的默认类别名称,并在字典中填充了英文字符串。

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 文件在第一次使用时被用户覆盖。

回答by redent84

Localized Plist files

本地化的 Plist 文件

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。通过这样做,您将为每种支持的语言拥有不同的 plist 文件。

Select the plist file in your project, and select Localizein the File Inspectormenu.

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

File localization image

文件本地化图片

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

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

From:

从:

dummy.plist

dummy.plist

To:

到:

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


Localized Plist contents

本地化的 Plist 内容

Another solution would be to use localized strings inside the plist, and simply call NSLocalizedStringbefore printing out the extracted string.

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

Imagine you had a Plist like this: Original plist

想象一下,你有一个这样的 Plist: 原始 plist

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

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

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

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: Localized plist contents

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

And your Localizable.stringsfor Engligh:

而你Localizable.strings的英语:

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

and Spanish:

和西班牙语:

"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.

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

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

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

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

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

Simply change it to:

只需将其更改为:

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

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

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

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

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