iOS国际化的语言和App内容本地化多语言切换

1、国际化语言显示,是根据设备的语言环境进行对应语言显示,例如:手机的配置的语言是英语,App的名字、访问相机摄像头提示多语言、访问麦克风多语言提示等等会根据手机设备进行更换,下面是实现国际化语言的步骤:

(1)、点击项目-->PROJECT-->Loaclizations如图所示:

1.jpg

(2)、新建一个文件名字叫做:InfoPlist.strings 一定是要这个名字。

2.jpg

(3)、在InfoPlist.strings添加多语言:

3.jpg

(4)、在InfoPlist.strings添加多语言的字段:

    ///App名字
CFBundleName = "My App";
///访问相册的提示
NSPhotoLibraryUsageDescription = "Open Photo Library";
///访问相机的提示
NSCameraUsageDescription = "Open Camera Usage";

(4)、获取InfoPlist.strings里面对应的字段方法是在项目的Info.plist里面右键--->Open As-->Source Code中的字段,例如:NSPhotoLibraryUsageDescription字段是对应访问相册的提示:

5.jpg
4.jpg

(4)、看看最终效果:

6.jpg
7.jpg

2、App内容本地化多语言切换:

(1)新建一个名字叫做Localizable.strings的文件,然后在里面添加多语言的内容:

8.jpg
"世界你好" = "Hello World!";

(2)我这里封装一个工具专门用于App内容设置以及获取多语言内容:

//多语言类型
enum LanguageManagerType : String
{
    //中国
    case CHINA = "zh-Hans"
    //英语
    case ENGLISH = "en"
    //马来西亚
    case MELAYU = "ms"
    //泰国
    case THAILAND = "th"
    //越南
    case VIETNAM = "vi"
    //菲律宾
    case THEPHILIPPINES = "fil"
    //印尼
    case INDONESIA = "id"
}

///获取多语言
func HSZLanguage(key : String) -> String{
    let languageVaule : String = LanguageManagerTool.getString(key: key)
    return languageVaule
}

class LanguageManagerTool: NSObject {
    ///存储关键字
    let UserLanguage : String = "UserLanguage"
      ///存储关键字
    let AppleLanguages : String = "AppleLanguages"
    ///单例
    static let shareInstance = LanguageManagerTool()
   ///用UserDefaults存储设置的语言
    let def = UserDefaults.standard
    var bundle : Bundle?
    
    /* 根据关键字获取内容*/
    class func getString(key:String) -> String{
        let bundle = LanguageManagerTool.shareInstance.bundle
        let str = bundle?.localizedString(forKey: key, value: nil, table: nil)
        return str!
    }
        
    /// 设置语言样式
    func setLanguage(langeuageType :LanguageManagerType) {
        let langeuage : String = langeuageType.rawValue
        let path = Bundle.main.path(forResource:langeuage , ofType: "lproj")
        bundle = Bundle(path: path!)
        def.set(langeuage, forKey: UserLanguage)
        def.synchronize()
    }
    
    ///根据本地语言设置默认语言
    func setDefaultLanguage() {
        //获取本地app的语言
        let languages = def.object(forKey: AppleLanguages) as? NSArray
        if languages?.count != 0 {
            ///本地设备使用的语言
            let languageArray : Array = UserDefaults.standard.object(forKey: "AppleLanguages") as! Array
            var localLanguageString : String = ""
            if languageArray.count > 0 {
                localLanguageString = languageArray[0]
            }
            ///如果是中文就是转成zh-Hans
            if localLanguageString.range(of: "zh") != nil
            {
                localLanguageString = "zh-Hans"
            }
            if localLanguageString.range(of: "en") != nil {
                localLanguageString = "en"
            }
            ///设置语言类型
            let path = Bundle.main.path(forResource:localLanguageString, ofType: "lproj")
            print("pathFJDFID----\(path ?? "")")
            bundle = Bundle(path: path!)
            def.set(localLanguageString, forKey: UserLanguage)
            def.synchronize()
        }
    }
}

(3)、使用多语言

let language =   HSZLanguage(key: "世界你好")

这是具体的Demo,有帮助点个星星✨ 源码Demo

你可能感兴趣的:(iOS国际化的语言和App内容本地化多语言切换)