Xcode 4.6 localization within plist file
Asked Answered
L

1

8

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.

Logorrhea answered 20/3, 2013 at 10:40 Comment(0)
P
24

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);
Pusan answered 20/3, 2013 at 11:11 Comment(5)
this seems a good solution. i would like to 'reask' what you mean with localized strings within the plist. can i use 'NSLocalizedString:@"XX"' within the plist? or do you mean casting afterwards?Vindicate
thats quite awesome! thank you very much for taking the time :) acceptVindicate
Let's say you've localized in English and Spanish. When I use menuTitleLabel.text = NSLocalizedString(@"MY_MENU_TITLE", nil); what will a user using French as the system language see? Wouldn't she just see "MY_MENU_TITLE"? Which... really wouldn't make any sense to the user?Saturated
@JihoKang Of course not. The user will see the default development language (set in the Info.plist file) unless there's a localization created in the project for that language and the localization files for that language doesn't exist. Feel free to try it.Pusan
You should never use NSlocalizedString() with variables or constants, never do something like "NSLocalizedString(someVariable, 'comment')". If you do this you will get errors when you need to run "genstrings" or "export for localization"Maurita

© 2022 - 2024 — McMap. All rights reserved.