IPhone Localization: Simple Project Not Working
I am doing my first localized project and I have been battling it for several hours with no luck.
I need to create an application that displays texts and images in different languages based on user selection.
I've read most of Apple's docs on the subject, but I can't seem to get a simple example working.
These are my steps so far:
1) Create a new project.
2) Manually create the "en.lproj" directory in your projects folder.
3) Using TexEdit create a file called "Localizable.strings" and save it in Unicode UTF-16. The file looks like this:
/*
Localizable.strings
Multilanguage02
Created by Gonzalo Floria on 5/6/10.
Copyright 2010 __MyCompanyName__. All rights reserved.
*/
"Hello" = "Hi";
"Goodbye" = "Bye";
4) I dragged this file into the Resources folder on Xcode and it appears under "subdir" "en" below it (with a dropdown triangle on the left). If I try to see it on Xcode everything looks wrong, much? characters but Im guessing because it is a UTF-16 file. Right?
5) Now that my view has loaded, I can access these lines like this:
NSString *translated;
translated = NSLocalizedString(@"Hello", @"User greetings");
NSLog(@"Translated text is %@",translated);
My problem is allowing the user to switch the language. I created es.lproj with Localizable.strings file (in Spanish) but I MUST NOT access it.
I tried this line:
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"es", nil] forKey:@"AppleLanguages"];
But it only works NEXT application load time. Is there no way to allow the user to switch languages during application launch?
Do I need to execute my own dictionary files and forget about the NSLocalizableString family?
Thanks for ANY advice or pointers.
Gonso
a source to share
It is already discussed here.
Their suggestion is to create a sub-bundle and then use a method NSLocalizedStringFromTableInBundle(...)
(as described in the reference manual) to get a localized string from a specific table in the bundle.
I'm just guessing, I haven't tried it, but I think this might be a good way to solve your problem.
a source to share