iPad App - Change languages programmatically
ios, ipad, iphone, localization, objective-c
Solution
FOUND THE SOLUTION!!!
The following test app had a function that read the desired string from the correct 'Localizable.strings' file (based on the language selected): https://github.com/object2dot0/Advance-Localization-in-ios-apps
I took this code, and the code required to set the app's primary language (found in the above answer posted by Brayden: How to force NSLocalizedString to use a specific language), and put them together.
Here's what my code looks like now (note - my UISegmentedControl calls a function in it's view's viewController [when the UISegmentedControl's 'Value Changed' method is triggered] that then calls the toggleLanguage function in the parent viewController):
-(void)toggleLanguage:(NSString *)primaryLanguage secondaryLanguage:(NSString *)secondaryLanguage
{
//set app's primary language
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObjects:primaryLanguage,secondaryLanguage,nil] forKey:@"AppleLanguages"];
[defaults synchronize];
//update UILabel and UIImageViews
[self setLanguageSpecificItems];
}
-(NSString *)languageSelectedStringForKey:(NSString *)key
{
//read primary language
NSArray *appleLanguagesArray = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [appleLanguagesArray objectAtIndex:0];
//get the path to the desired lproj file
NSString *path;
if(currentLanguage==@"en")
{
path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
}
else
if(currentLanguage==@"fr")
{
path = [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];
}
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
//find and return the desired string
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
return str;
}
-(void)setLanguageSpecificItems
{
myUIImageView1.image = [UIImage imageNamed:[self languageSelectedStringForKey:@"myUIImageView1"]];
myLabel1.text = [self languageSelectedStringForKey:@"myLabel1"];
}
Thanks for the help everyone!!!
-Chris Allinson
Problem
I have an app that requires 2 languages, English and French. I have already set up the Localizable.strings files in their respective "en.lproj" and "fr.lproj" folders ... and when I change the iPad's language (in the native settings app), then launch my app, it does in fact load the correct text (i.e. either English copy or French copy). I need to have a UISegmentedControl toggle between the 2 languages without having to restart the app. How do I get the app to change the (current) language so that when I call a method that (re)sets all the UILabels' text and UIImageViews' images they read from the opposite .lproj folder's Localizable.strings file?!? I know how to use UISegmentedControl, and that is not my question. I'm looking more for a line of code that sets the application's bundle language or locale or something (as I'm quite new to internationalization.localization). Example of how I set the image for a UIImageView: ``` myUIImageView1.image = [UIImage imageNamed:NSLocalizedString(@"myUIImageView1", @"comment for the translator")]; ``` Example of how I set the text of a UILabel: ``` myLabel1.text = NSLocalizedString(@"myLabel1", @"comment for the translator"); ```