问题
Is there a way to change the application language during runtime?
So, after the change NSLocalizedString
immediately returns the string for the new language.
What I'm doing now is changing the language using the code below:
- (void)onChangeLanguage: (id)sender
{
NSArray *lang = [NSArray arrayWithObjects:((InfoWhatever *)sender).language, nil];
[[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"AppleLanguages"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
NSLog(@"Current language: %@", currentLanguage);
}
The language will change but only after restarting the app.
回答1:
I doubt you can do this, even the Settings app cannot do it.
(When you change the language in the Settings app, the screen goes black, and displays "setting language..." and a progress wheel. After a long wait, you are back in Springboard. It almost looks like the phone reboots.)
回答2:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* languages = [userDefaults objectForKey:@"AppleLanguages"];
[languages insertObject:@"de" atIndex:0]; // ISO639-1
[[NSUserDefaults standardUserDefaults] synchronize];
回答3:
The trick to use specific language by selecting it from the app is to force the NSLocalizedString to use specific bundle depending on the selected language ,
here is the post i have written for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html
and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps
回答4:
You Can do it . Here is the way http://aggressive-mediocrity.blogspot.com/2010/03/custom-localization-system-for-your.html
In Brief Download and add 2 files to the project
http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.h
http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.m
2#import "LocalizationSystem.h"
3
- (IBAction)btnEnglishClicked:(id)sender {
LocalizationSetLanguage(@"en");
}
4 After you set the language as above
AMLocalizedString(@"Key", nil)
Thats it.
回答5:
I came up with a solution that allows you to use NSLocalizedString
. I create a category of NSBundle
call NSBundle+RunTimeLanguage
. The interface is like this.
// NSBundle+RunTimeLanguage.h
#import <Foundation/Foundation.h>
@interface NSBundle (RunTimeLanguage)
#define NSLocalizedString(key, comment) [[NSBundle mainBundle] runTimeLocalizedStringForKey:(key) value:@"" table:nil]
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName;
@end
The implementation is like this.
// NSBundle+RunTimeLanguage.m
#import "NSBundle+RunTimeLanguage.h"
#import "AppDelegate.h"
@implementation NSBundle (RunTimeLanguage)
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *path= [[NSBundle mainBundle] pathForResource:[appDelegate languageCode] ofType:@"lproj"];
NSBundle *languageBundle = [NSBundle bundleWithPath:path];
NSString *localizedString=[languageBundle localizedStringForKey:key value:key table:nil];
return localizedString;
}
@end
Than just add import NSBundle+RunTimeLanguage.h
into the files that use NSLocalizedString
.
As you can see I store my languageCode in a property of AppDelegate
. This could be stored anywhere you'd like.
This only thing I don't like about it is a Warning that NSLocalizedString
marco redefined. Perhaps someone could help me fix this part.
回答6:
Simply add these lines:
#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]
1. NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@[@"en"] forKey:@"AppleLanguages"]; [defaults
synchronize];
2. _label.text = NSLocalizedStringFromTableInBundle(@"Key", nil, currentLanguageBundle, @"");
回答7:
try this: object_setClass([NSBundle mainBundle],[MyBundle class]);
https://github.com/maximbilan/ios_language_manager/blob/master/README.md
来源:https://stackoverflow.com/questions/1576904/how-to-change-iphone-app-language-during-runtime