Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4.6 localization within plist file

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.

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

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.

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.

like image 414
Sebastian Flückiger Avatar asked Mar 20 '13 10:03

Sebastian Flückiger


1 Answers

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.

File localization image

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: Original plist

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

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);
like image 151
redent84 Avatar answered Oct 14 '22 00:10

redent84