Files
keyboard/keyBoard/Class/Home/VC/FunctionTest/KBLangTestVC.m
2025-11-17 21:35:25 +08:00

96 lines
3.9 KiB
Objective-C

//
// KBLangTestVC.m
//
#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");
NSString *fallback = KBLocalized(@"当前:%@");
self.label.text = [NSString stringWithFormat:fmt.length ? fmt : fallback, code];
[self.toggleBtn setTitle:KBLocalized(@"lang_toggle") forState:UIControlStateNormal];
}
- (void)onToggle {
// 弹出系统提示,确认后再切换语言并重建根控制器
NSString *title = KBLocalized(@"Change Language");
NSString *msg = KBLocalized(@"Changing language will reload the Home screen.");
UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
message:msg
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:KBLocalized(@"Cancel")
style:UIAlertActionStyleCancel
handler:nil];
__weak typeof(self) weakSelf = self;
UIAlertAction *ok = [UIAlertAction actionWithTitle:KBLocalized(@"Confirm")
style:UIAlertActionStyleDefault
handler:^(__unused UIAlertAction * _Nonnull action) {
__strong typeof(weakSelf) self = weakSelf;
if (!self) return;
KBLocalizationManager *mgr = [KBLocalizationManager shared];
NSString *next = [mgr.currentLanguageCode.lowercaseString hasPrefix:@"zh"] ? @"en" : @"zh-Hans";
[mgr setCurrentLanguageCode:next persist:YES];
// 语言切换后重新进入根控制器
dispatch_async(dispatch_get_main_queue(), ^{
id<UIApplicationDelegate> appDelegate = UIApplication.sharedApplication.delegate;
if ([appDelegate respondsToSelector:@selector(setupRootVC)]) {
// 使用 AppDelegate 的统一入口
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[appDelegate performSelector:@selector(setupRootVC)];
#pragma clang diagnostic pop
}
});
}];
[ac addAction:cancel];
[ac addAction:ok];
[self presentViewController:ac animated:YES completion:nil];
}
@end