如何将支持的语言设置为iOS应用程序

时间:2018-01-16 11:00:32

标签: ios iphone swift locale nslocale

我有一个要求,我的后端只支持少数语言环境,所以当我上传一些数据时,我想要标记语言环境,所以我应该将支持的语言环境提供给应用程序,我该怎么做?可以有人帮助我

1 个答案:

答案 0 :(得分:1)

我有类似的要求,实施很简单。我们曾经在API请求的标头中传递语言环境以及Auth标头。以下这两个函数都是类AppHelper

的类函数

获取本地化的代码:

class func getLocalisation() -> String? {
        let language = Bundle.main.preferredLocalizations.first
        if let splittedString = language?.split(separator: "-"){
            return String(splittedString[0])
        }
        return nil
    }

使用区域设置并将其附加到标题。

class func getAuthHeaders() -> Dictionary<String, String> {
        var headers = Dictionary<String, String>()
        if  let authkey = AppHelper.getAuthKey() {
            headers[Constant.authKeyHeader] = authkey
        }
        if let localisation = AppHelper.getLocalisation() {
            headers[Constant.localisation] = localisation
        }

        return headers
    }

希望这会有所帮助。快乐的编码。