添加多语言

This commit is contained in:
2025-11-03 16:37:28 +08:00
parent e4cebeac85
commit 1673a2f4be
11 changed files with 449 additions and 13 deletions

View File

@@ -7,10 +7,12 @@
#import "HomeVC.h"
#import "KBGuideVC.h"
#import "KBLangTestVC.h"
@interface HomeVC ()
@property (nonatomic, strong) UITextView *textView;
@interface HomeVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITextView *textView; //
@property (nonatomic, strong) UITableView *tableView; //
@property (nonatomic, copy) NSArray<NSString *> *items; //
@end
@implementation HomeVC
@@ -18,14 +20,32 @@
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CGRect frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 200)/2, 150, 200, 200);
//
CGFloat width = [UIScreen mainScreen].bounds.size.width;
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 220)];
CGRect frame = CGRectMake(16, 16, width - 32, 188);
self.textView = [[UITextView alloc] initWithFrame:frame];
self.textView.text = @"测试";
self.textView.text = KBLocalized(@"home_input_placeholder");
self.textView.layer.borderColor = [UIColor blackColor].CGColor;
self.textView.layer.borderWidth = 0.5;
[self.view addSubview:self.textView];
[self.textView becomeFirstResponder];
[header addSubview:self.textView];
//
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleInsetGrouped];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableHeaderView = header;
[self.view addSubview:self.tableView];
//
self.items = @[ KBLocalized(@"home_item_lang_test") ];
// 便
dispatch_async(dispatch_get_main_queue(), ^{ [self.textView becomeFirstResponder]; });
//
[[KBNetworkManager shared] GET:@"app/config" parameters:nil headers:nil completion:^(id _Nullable jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"====");
}];
@@ -33,7 +53,11 @@
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"===");
//
self.title = KBLocalized(@"home_title");
// items cell
self.items = @[ KBLocalized(@"home_item_lang_test") ];
[self.tableView reloadData];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
@@ -44,6 +68,28 @@
// [KBHUD showAllowTapToDismiss:true];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.items.count; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"home.cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; }
cell.textLabel.text = self.items[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
//
KBLangTestVC *vc = [KBLangTestVC new];
[self.navigationController pushViewController:vc animated:YES];
}
}
/*
#pragma mark - Navigation

View File

@@ -0,0 +1,14 @@
//
// KBLangTestVC.h
// 多语言测试页:点击按钮在中英文之间切换,并刷新文案。
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBLangTestVC : UIViewController
@end
NS_ASSUME_NONNULL_END

View 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

View File

@@ -29,6 +29,7 @@
#import "BaseNavigationController.h"
#import "BaseTableView.h"
#import "BaseCell.h"
#import "KBLocalizationManager.h" // 全局多语言封装
//-----------------------------------------------宏定义全局----------------------------------------------------------/

View File

@@ -28,7 +28,7 @@
[self.view addSubview:close];
UILabel *title = [[UILabel alloc] init];
title.text = @"启用输入法";
title.text = [[KBLocalizationManager shared] localizedStringForKey:@"perm_title_enable" table:nil value:@"启用输入法"];
title.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold];
title.textColor = [UIColor blackColor];
title.textAlignment = NSTextAlignmentCenter;
@@ -37,7 +37,8 @@
[self.view addSubview:title];
UILabel *tips = [[UILabel alloc] init];
tips.text = @"1 开启键盘 > 2 允许完全访问";
// Localizable.strings
tips.text = [[KBLocalizationManager shared] localizedStringForKey:@"perm_steps" table:nil value:@"1 开启键盘 > 2 允许完全访问"];
tips.font = [UIFont systemFontOfSize:14];
tips.textColor = [UIColor darkGrayColor];
tips.textAlignment = NSTextAlignmentCenter;
@@ -54,7 +55,7 @@
[self.view addSubview:card];
UIButton *open = [UIButton buttonWithType:UIButtonTypeSystem];
[open setTitle:@"去设置中开启" forState:UIControlStateNormal];
[open setTitle:[[KBLocalizationManager shared] localizedStringForKey:@"perm_open_settings" table:nil value:@"去设置中开启"] forState:UIControlStateNormal];
open.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];
[open setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
open.backgroundColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
@@ -66,7 +67,7 @@
[self.view addSubview:open];
UILabel *help = [[UILabel alloc] init];
help.text = @"没有找到键盘? 请前往 设置 > 通用 > 键盘 > 键盘 > 添加新键盘";
help.text = [[KBLocalizationManager shared] localizedStringForKey:@"perm_help" table:nil value:@"没有找到键盘? 请前往 设置 > 通用 > 键盘 > 键盘 > 添加新键盘"];
help.font = [UIFont systemFontOfSize:12];
help.textColor = [UIColor grayColor];
help.textAlignment = NSTextAlignmentCenter;