Xcode 4.6 localization within plist file

戏子无情 提交于 2019-11-30 02:26:37

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.

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

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

From:

dummy.plist

To:

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

Localized Plist contents

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

Imagine you had a Plist like this:

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ú";

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:

And your Localizable.strings for Engligh:

"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 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"];

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:

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