前言
iOS APP 内的本地化切换原理:
- 自定义解析本地化字符串的工具类LanguageManager
- 应用内切换语言生效的技术实现:采用销毁根控制器,重新进入一次
- 本地化字符串指定参数顺序
- demo源码:https://download.csdn.net/download/u011018979/19089505
I 、iOS本地化字符串指定参数顺序
对于在中英文语法的区别,在本地化字符串的时候,我们常常需要调整stringWithFormat的参数顺序。
1.1 原理
原理:在%和@中间加上
数值$
(1$,2$
),数字代表参数的顺序
self.FORMATLab.text = [NSString stringWithFormat:KNLocal(@"FORMAT", nil), @"csdn", @"https://kunnan.blog.csdn.net/article/details/103733872"];
- en.lproj/Localizable.strings
"FORMAT" = "I am %2$@ from '%1$@'";
- zh-Hans.lproj/Localizable.strings
"FORMAT" = "我是来自“%1$@”的%2$@";
1.2 效果
- 中文
英文
1.3 demo
从CSDN下载【iOS APP 内的国际化切换】demo源码:https://download.csdn.net/download/u011018979/19089505
原理:
- 自定义解析本地化字符串的工具类LanguageManager
- 应用内切换语言生效的技术实现:采用销毁根控制器,重新进入一次
- 本地化字符串指定参数顺序
@interface ViewController () /** 中英文lab,用于切换语言:处理切换中英文语言的动作事件 */ @property (weak, nonatomic) UILabel *languageLab; /** 用于测试指定参数顺序: 在%和@中间加上1$,2$等等就可以啦,数字代表参数的顺序。 */ @property (weak, nonatomic) UILabel *FORMATLab; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self languageLab]; self.view.backgroundColor = UIColor.whiteColor; self.FORMATLab.text = [NSString stringWithFormat:KNLocal(@"FORMAT", nil), @"csdn", @"https://kunnan.blog.csdn.net/article/details/103733872"]; if (@available(iOS 13.0, *)) { [self testUIPageControl]; } } - (UILabel *)FORMATLab{ if (!_FORMATLab) { UILabel *tmp = [[UILabel alloc]init]; _FORMATLab = tmp; tmp.textColor = rgb(51,51,51); tmp.font = kBoldFont(15); tmp.numberOfLines = 0; tmp.textAlignment = NSTextAlignmentCenter; [self.view addSubview:tmp]; __weak __typeof__(self) weakSelf = self; [tmp mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(weakSelf.view).offset(kAdjustRatio(-80)); make.centerX.equalTo(weakSelf.view).offset(-kAdjustRatio(0)); make.left.equalTo(weakSelf.view).offset(-kAdjustRatio(10)); make.right.equalTo(weakSelf.view).offset(-kAdjustRatio(10)); // make.size.mas_equalTo(CGSizeMake(kAdjustRatio(100), kAdjustRatio(40))); }]; } return _FORMATLab; }