好的,这个任务比我想象的要难得多...
基本上,我正在使用NSBundle上的类别和一种名为 isa-swizzeling 的技术来交换即将被NSLocalizedString(…)
调用的方法。
NSBundle+Language.h
#import <Foundation/Foundation.h>
@interface NSBundle (Language)
+(void)setLanguage:(NSString*)language;
@end
NSBundle+Language.m
#import "NSBundle+Language.h"
#import <objc/runtime.h>
static const char associatedLanguageBundle=0;
@interface PrivateBundle : NSBundle
@end
@implementation PrivateBundle
-(NSString*)localizedStringForKey:(NSString *)key
value:(NSString *)value
table:(NSString *)tableName
{
NSBundle* bundle=objc_getAssociatedObject(self, &associatedLanguageBundle);
return bundle ? [bundle localizedStringForKey:key
value:value
table:tableName] : [super localizedStringForKey:key
value:value
table:tableName];
}
@end
@implementation NSBundle (Language)
+(void)setLanguage:(NSString*)language
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
object_setClass([NSBundle mainBundle],[PrivateBundle class]);
});
objc_setAssociatedObject([NSBundle mainBundle], &associatedLanguageBundle, language ?
[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
AppDelegate会监听 LANGUAGE_WILL_CHANGE
通知,设置语言并广播一个 LANGUAGE_DID_CHANGE
通知。
AppDelegate.m
#import "AppDelegate.h"
#import "NSBundle+Language.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageWillChange:) name:@"LANGUAGE_WILL_CHANGE" object:nil];
NSString *targetLang = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedLanguage"];
[NSBundle setLanguage:targetLang?:@"en"];
return YES;
}
-(void)languageWillChange:(NSNotification *) noti
{
NSString *targetLang = [noti object];
[[NSUserDefaults standardUserDefaults] setObject:targetLang forKey:@"selectedLanguage"];
[NSBundle setLanguage:targetLang];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LANGUAGE_DID_CHANGE" object:targetLang];
}
@end
一个BaseViewController将会发布LANGUAGE_WILL_CHANGE
并监听LANGUAGE_DID_CHANGE
BaseViewController.h
#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController
-(void) languageDidChange;
- (IBAction)switchLanguage:(id)sender;
@end
BaseViewController.m
#import "BaseViewController.h"
@interface BaseViewController ()
@property (weak, nonatomic) IBOutlet UIButton *englishButton;
@property (weak, nonatomic) IBOutlet UIButton *spanishButton;
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageDidChangeNotification:) name:@"LANGUAGE_DID_CHANGE" object:nil];
}
- (IBAction)switchLanguage:(id)sender {
NSString *localString;
if (self.englishButton == sender) {
localString = @"en";
} else if(self.spanishButton == sender){
localString = @"es";
}
if (localString) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"LANGUAGE_WILL_CHANGE" object:localString];
}
}
-(void)languageDidChangeNotification:(NSNotification *)notification
{
[self languageDidChange];
}
-(void)languageDidChange
{
}
@end
现在,任何继承自
BaseViewController
的视图控制器都可以实现
languageDidChange
方法来调用
NSLocalizedString
函数。
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self languageDidChange];
}
-(void)languageDidChange
{
self.label.text = NSLocalizedString(@"Hello World", nil);
self.imageView.image = [UIImage imageNamed:NSLocalizedString(@"image.png", nil)];
}
@end
如您所见,我只是将图片名称本地化,我将图像en_image.png
和es_image.png
添加到图像资源包中,并在本地化字符串中映射它们。
"image.png" = "en_image.png"
并且。
"image.png" = "es_image.png"
结果
![这里输入图片描述](https://istack.dev59.com/4QheI.gif)
您可以在此处找到示例代码:https://github.com/vikingosegundo/ImmidiateLanguageChange