添加多语言
This commit is contained in:
61
keyBoard/Class/Home/VC/KBLangTestVC.m
Normal file
61
keyBoard/Class/Home/VC/KBLangTestVC.m
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// KBLangTestVC.m
|
||||
// 多语言测试页:点击切换语言(在 zh-Hans 与 en 之间),演示运行时生效。
|
||||
//
|
||||
|
||||
#import "KBLangTestVC.h"
|
||||
|
||||
@interface KBLangTestVC ()
|
||||
@property (nonatomic, strong) UILabel *label;
|
||||
@property (nonatomic, strong) UIButton *toggleBtn;
|
||||
@end
|
||||
|
||||
@implementation KBLangTestVC
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
self.view.backgroundColor = UIColor.whiteColor;
|
||||
[self buildUI];
|
||||
[self refreshTexts];
|
||||
|
||||
// 监听语言变更,实时刷新
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTexts) name:KBLocalizationDidChangeNotification object:nil];
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
}
|
||||
|
||||
- (void)buildUI {
|
||||
CGFloat w = UIScreen.mainScreen.bounds.size.width;
|
||||
|
||||
self.label = [[UILabel alloc] initWithFrame:CGRectMake(24, 120, w - 48, 60)];
|
||||
self.label.textAlignment = NSTextAlignmentCenter;
|
||||
self.label.numberOfLines = 0;
|
||||
[self.view addSubview:self.label];
|
||||
|
||||
self.toggleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
self.toggleBtn.frame = CGRectMake(40, CGRectGetMaxY(self.label.frame) + 24, w - 80, 48);
|
||||
self.toggleBtn.layer.cornerRadius = 8;
|
||||
self.toggleBtn.backgroundColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1];
|
||||
[self.toggleBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
|
||||
[self.toggleBtn addTarget:self action:@selector(onToggle) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self.view addSubview:self.toggleBtn];
|
||||
}
|
||||
|
||||
- (void)refreshTexts {
|
||||
self.title = KBLocalized(@"lang_test_title");
|
||||
NSString *code = [KBLocalizationManager shared].currentLanguageCode ?: @"";
|
||||
NSString *fmt = KBLocalized(@"current_lang");
|
||||
self.label.text = [NSString stringWithFormat:fmt.length ? fmt : @"当前:%@", code];
|
||||
[self.toggleBtn setTitle:KBLocalized(@"lang_toggle") forState:UIControlStateNormal];
|
||||
}
|
||||
|
||||
- (void)onToggle {
|
||||
KBLocalizationManager *mgr = [KBLocalizationManager shared];
|
||||
NSString *next = [mgr.currentLanguageCode.lowercaseString hasPrefix:@"zh"] ? @"en" : @"zh-Hans";
|
||||
[mgr setCurrentLanguageCode:next persist:YES];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user