Created
October 27, 2019 17:46
-
-
Save Koze/8fd9a680d2ae5ff9ab749dfc1db7e3a0 to your computer and use it in GitHub Desktop.
Workaround for that AVSpeechSynthesisVoice currentLanguageCode returns application language.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AVSpeechSynthesisVoice { | |
class func currentLanguageCodeWorkaround() -> String { | |
if #available(iOS 13, *) { | |
for preferredLanguage in Locale.preferredLanguages { | |
let locale = Locale(identifier: preferredLanguage) | |
if let languageCode = locale.languageCode { | |
var voiceLanguage = languageCode | |
if let regionCode = locale.regionCode { | |
voiceLanguage.append("-\(regionCode)") | |
} | |
if let avVoice = AVSpeechSynthesisVoice(language: voiceLanguage) { | |
return avVoice.language | |
} | |
} | |
} | |
return "en-US" | |
} | |
else { | |
return currentLanguageCode() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment