我试图Region Code
在 AVSpeechSynthesisVoice 而不是 eg:中找到en-GB
。
我的目标输出如下
这是我当前的代码,虽然它有效,但我觉得必须有一个更优雅的解决方案,从我读到的内容中,-
可能会产生某些语言的错误
let speechVoices = AVSpeechSynthesisVoice.speechVoices()
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let language = Locale.current.localizedString(forLanguageCode: speechVoices[row].language)!
let languageCode = speechVoices[row].language
var regionCodeArray = languageCode.split(separator: "-") // FIXME: buggy may break
let regionCode = regionCodeArray.removeLast()
// Must be a more elegant way to get RegionCode
let country = Locale.current.localizedString(forRegionCode: String(regionCode))
return "\(language) (\(country ?? ""))"
}
谢谢!