汽车公司可能用到的知识点

iOS 国际化多语言(上)

2021-09-13  本文已影响0人  卟过尔尔

前言

这里介绍的方法只是根据系统语言变化
demo
iOS 国际化多语言(下)

配置

首先打开project下的info文件 在Localizations里面选择你想要的语言,添加->完成 操作步骤 完成

Info.plist国际化

//英文
//书写方法一 修改APP桌面名称
CFBundleDisplayName = "LanguageManager";
//书写方法二 获取麦克风权限
"NSMicrophoneUsageDescription" = "Need to obtain microphone permission";

//中文
//书写方法一 修改APP桌面名称
CFBundleDisplayName = "国际化语言";
//书写方法二 获取麦克风权限
"NSMicrophoneUsageDescription" = "需要获取麦克风权限";

注意:上述2种书写方法,key不带" "符号是在英文情况下,如果是中文key会识别不出来

获取Info.plist文件中key的方法:选中Info.plist-->右键选中Open As-->Source Code
注意:选中Property List 恢复列表样

步骤 结果一 结果二

代码中语言国际化

//使用方法 NSLocalizedString(@"Chinese", nil)
- (UIButton *)btOne{
    if (!_btOne) {
        _btOne = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_btOne setTitle:NSLocalizedString(@"Chinese", nil) forState:(UIControlStateNormal)];
        [_btOne setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _btOne.titleLabel.font = [UIFont systemFontOfSize:20];
        _btOne.frame = CGRectMake(50, 50, 100, 50);
        [_btOne addTarget:self action:@selector(clickBTone:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_btOne];
    }
    return _btOne;
}

注意: 如果创建.strings文件是自定义的名字的话,如Internation.strings,那么调用的方式为: NSLocalizedStringFromTable(@”English”,@”Internation”, nil) NSLocalizedStringFromTable(@”Authority”,@”Internation”, nil) 当自定义名字后,使用NSLocalizedString的方式不起作用,这里要特别注意,注意,注意!!!(重要的说三遍);

StoryBoard设置

图片设置

//在中文Localizable.strings文件中:
imageName = "ChineseImage";
//在英文Localizable.strings文件中:
imageName = "EnglishImage";

//代码中使用
- (UIImageView *)imageOne{
    if (!_imageOne) {
        _imageOne = [[UIImageView alloc] initWithFrame:CGRectMake(50, 250, 100, 100)];
        _imageOne.image = [UIImage imageNamed:NSLocalizedString(@"imageName", nil)];
        [self.view addSubview:_imageOne];
    }
    return _imageOne;
}

应用内大额数字的国际化

大额数字,在不同的语言中,表现形式可能不一样。比如100w,在英语中是“1,000,000”这样表示;而在西班牙语中表示为“1.000.000”;想实现大额数字显示的国际化,可以按照如下步骤:

// 数字国际化
- (UILabel *)lbThree{
    if (!_lbThree) {
        _lbThree = [[UILabel alloc] init];
        _lbThree.frame = CGRectMake(250, 200, 100, 50);
        _lbThree.font = [UIFont systemFontOfSize:20];
        
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        NSString *numberString = [numberFormatter stringFromNumber:@(-1000000)];
        _lbThree.text = numberString;
        
        [self.view addSubview:_lbThree];
    }
    return _lbThree;
}

目前根据查阅资料只想到了这些,如果还有其它的或更好的方法,希望大家能多留言告知,后续也会更新~~~

上一篇 下一篇

猜你喜欢

热点阅读