Swift - 如何将文本转换为语音语言作为描述性字符串
Swift - how to get the text to speech languages as descriptive string
我正在尝试将文本转语音语言代码(在 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;
}
感谢 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)
首先要做的是确保将 AVFoundation
库导入到头文件 (.h) 中,该文件将实现 AVSpeechSynthesizer
或 AVSpeechSynthesizer
及其属性和方法不可访问。
例如:#import <AVFoundation/AVFoundation.h>
此 post 中的反馈很好。谢谢大家。
我正在尝试将文本转语音语言代码(在 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;
}
感谢 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)
首先要做的是确保将 AVFoundation
库导入到头文件 (.h) 中,该文件将实现 AVSpeechSynthesizer
或 AVSpeechSynthesizer
及其属性和方法不可访问。
例如:#import <AVFoundation/AVFoundation.h>
此 post 中的反馈很好。谢谢大家。