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.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With