我正在尝试将文本到语音的语言代码(在BCP 47中)转换为描述性字符串,以选择在应用程序中具有文本到语音功能的语言。
预期-
阿拉伯语(沙特阿拉伯)-ar-SA 中文(中国)-zh-CN 中文(中国香港特别行政区)-zh-HK 中文(台湾)-zh-TW 捷克文(捷克共和国)-cs-CZ
实际-
ar-SA(固定) cs-CZ(固定)
class func getListOfSupportedLanguages() {
for voice in (AVSpeechSynthesisVoice.speechVoices()){
print(voice.language)
let language = Locale.init(identifier: voice.language)
print(language.description)
}
}
我正在关注这个有用的网站,并获得了不同的结果。
https://useyourloaf.com/blog/synthesized-speech-from-text/
@property (strong, nonatomic) NSArray *languageCodes;
@property (strong, nonatomic) NSDictionary *languageDictionary;
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
- (NSArray *)languageCodes
{
if (!_languageCodes)
{
_languageCodes = [self.languageDictionary keysSortedByValueUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
return _languageCodes;
}
// Map between language codes and locale specific display name
- (NSDictionary *)languageDictionary
{
if (!_languageDictionary)
{
NSArray *voices = [AVSpeechSynthesisVoice speechVoices];
NSArray *languages = [voices valueForKey:@"language"];
NSLocale *currentLocale = [NSLocale autoupdatingCurrentLocale];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSString *code in languages)
{
dictionary[code] = [currentLocale displayNameForKey:NSLocaleIdentifier value:code];
}
_languageDictionary = dictionary;
}
return _languageDictionary;
}
答案 0 :(得分:0)
多亏了Matt,我可以正常使用-NSLocale具有displayName,而Locale没有。根据下面的帖子
NSLocale Swift 3
这是代码-
class func getListOfSupportedLanguages() {
let current = Locale.current.languageCode
for voice in (AVSpeechSynthesisVoice.speechVoices()){
let language = NSLocale.init(localeIdentifier: current!)
print(language.displayName(forKey: NSLocale.Key.identifier, value: voice.language)?.description as! String)
}
}
输出-
English (Australia)
English (United Kingdom)
English (Ireland)
English (United States)
English (South Africa)