问题是我们想在运行时切换语言。 我们有一个支持英语和阿拉伯语的应用程序。
我正在做的是更改“AppleLanguages”,然后重置 Root View Controller 。 我使用了以下代码:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* languages = [userDefaults objectForKey"AppleLanguages"];
NSString *language = nil;
if([[languages objectAtIndex:0] isEqual"en"]) {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects"ar", @"en",nil] forKey"AppleLanguages"];
language = @"ar";
} else {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects"en",@"ar", nil] forKey"AppleLanguages"];
language = @"en";
}
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[appDelegate.window setRootViewController:initViewController];
appDelegate.window.rootViewController = [storyBoard instantiateViewControllerWithIdentifier"Home"];
appDelegate.window.backgroundColor = [UIColor whiteColor ];
[appDelegate.window makeKeyAndVisible];
原生本地化发生变化,但必须重新启动应用才能看到对 UI 的影响。
提前致谢。
问题在于语言更改没有反射(reflect)在 bundle 上。 所以我无法更改 UI 以及本地化文本。 我们可以使用以下代码来更改捆绑设置。 以下代码将在运行时切换语言。 需要子类化 NSBundle 类,如下所示:- @implementation BundleEx
- (NSString *)localizedStringForKeyNSString *)key valueNSString *)value tableNSString *)tableName
{
NSBundle *bundle = objc_getAssociatedObject(self, &kBundleKey);
if (bundle) {
return [bundle localizedStringForKey:key value:value table:tableName];
}
else {
return [super localizedStringForKey:key value:value table:tableName];
}
}
@end
@implementation NSBundle (Language)
+ (void)setLanguageNSString *)language
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
object_setClass([NSBundle mainBundle], [BundleEx class]);
});
BOOL appleTextDirection = NO;
BOOL rightToLeftWritingDirection = NO;
if ([language isEqual"ar"]) {
rightToLeftWritingDirection =YES;
appleTextDirection = NO;
if ([[[UIView alloc] init] respondsToSelectorselector(setSemanticContentAttribute]) {
[[UIView appearance] setSemanticContentAttribute:
UISemanticContentAttributeForceRightToLeft];
}
}else {
rightToLeftWritingDirection =NO;
appleTextDirection = YES;
if ([[[UIView alloc] init] respondsToSelectorselector(setSemanticContentAttribute]) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
}
[[NSUserDefaults standardUserDefaults] setBool:appleTextDirection forKey:@"AppleTextDirection"];
[[NSUserDefaults standardUserDefaults] setBool:rightToLeftWritingDirection forKey:@"NSForceRightToLeftWritingDirection"];
[[NSUserDefaults standardUserDefaults] synchronize];
id value = language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil;
objc_setAssociatedObject([NSBundle mainBundle], &kBundleKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
以及用于切换的代码:-
-(void)toggleTheLanguageWithNSString *)identifier{
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray* languages = [userDefaults objectForKey:@"AppleLanguages"];
NSString *language = nil;
if ([[languages objectAtIndex:0] rangeOfString:@"en"].location != NSNotFound) {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ar",@"en",nil] forKey:@"AppleLanguages"];
language = @"ar";
}else if ([[languages objectAtIndex:0] rangeOfString:@"ar"].location != NSNotFound) {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en",@"ar", nil] forKey:@"AppleLanguages"];
language = @"en";
}
[[NSUserDefaults standardUserDefaults]synchronize];
[NSBundle setLanguage:language];
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.window.rootViewController = [mystoryboard instantiateViewControllerWithIdentifier:identifier];
[self.window makeKeyAndVisible];
[UIView transitionWithView:self.window duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
}];
}
关于ios - 从右到左的语言在运行时更改本地化。无法切换 UI 必须重新启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40857870/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |