【问题标题】:Speech Synthezis API : which locale is used by NSLocalizedString?Speech Synthezis API:NSLocalizedString 使用哪个语言环境?
【发布时间】:2018-06-26 21:01:58
【问题描述】:

我想使用 iOS 7 新的语音合成 API,我的应用程序已本地化为法语和英语。

为此,必须本地化两件事:

  • 语音文本:我把它放在通常的localizable.string文件中,然后使用NSLocalizedString宏在代码中检索它。

  • 语音语言:AVSpeechSynthesisVoice 必须选择对应的语言。

类实例化方法是AVSpeechSynthesisVoice voiceWithLanguage:(NSString *)lang。 我目前使用[NSLocale currentLocale].localeIdentifier 作为此方法的参数。

问题:如果用户的设备语言是葡萄牙语,[NSLocale currentLocale] 选择葡萄牙语发音,而NSLocalizedString 解析的文本是英语。

我如何知道NSLocalizedString 当前读取的是哪个语言环境?

【问题讨论】:

    标签: ios nslocale avspeechsynthesizer


    【解决方案1】:

    好的,我终于理解了 Apple API:

    • [NSLocale currentLocale]:不返回用户在设置>常规>国际中选择的当前语言,但返回用户在同一屏幕中选择的区域代码。

    • [NSLocale preferredLanguages] : 此列表提供设备语言,它是此列表中的第一个字符串

    • [[NSBundle mainBundle] preferredLocalizations] 返回由应用程序解析的语言包。我想这就是NSLocalizedString 使用的。在我的情况下它只有 1 个对象,但我想知道在哪些情况下它可以有多个。

    • [AVSpeechSynthesisVoice currentLanguageCode]返回系统预定义的语言代码。

    • [AVSpeechSynthesisVoice voiceWithLanguage:] 类实例化方法需要完整的语言代码:语言和区域。 (例如:将@"en" 传递给它会返回nil 对象,它需要@"en-US" 或@"en-GB"...)

    • [AVSpeechSynthesisVoice currentLanguageCode] 提供默认语音,由操作系统确定。

    这就是我的最终代码的样子

     // current user locale (language & region)
        NSString *voiceLangCode = [AVSpeechSynthesisVoice currentLanguageCode];
        NSString *defaultAppLang = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
    
        // nil voice will use default system voice
        AVSpeechSynthesisVoice *voice = nil;
    
        // is default voice language compatible with our application language ?
        if ([voiceLangCode rangeOfString:defaultAppLang].location == NSNotFound) {
            // if not, select voice from application language
            NSString *pickedVoiceLang = nil;
            if ([defaultAppLang isEqualToString:@"en"]) {
                pickedVoiceLang = @"en-US";
            } else {
                pickedVoiceLang = @"fr-FR";
            }
            voice = [AVSpeechSynthesisVoice voiceWithLanguage:pickedVoiceLang];
        }
    
    
        AVSpeechUtterance *mySpeech = [[AVSpeechUtterance alloc] initWithString:NSLocalizedString(@"MY_SPEECH_LOCALIZED_KEY", nil)];
        frontPicUtterance.voice = voice;
    

    这样,来自新西兰、澳大利亚、英国或加拿大的用户将获得最符合他通常设置的声音。

    【讨论】:

    • 我认为 [NSLocale preferredLanguages] 更好,因为该应用可能未本地化为外来语言,但 TTS 在设备上可用
    【解决方案2】:

    Vinzzz 的回答是一个很好的开始——我已经将它概括为适用于任何语言:

    NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
    NSString *voiceLangCode = [AVSpeechSynthesisVoice currentLanguageCode];
    if (![voiceLangCode hasPrefix:language]) {
        // the default voice can't speak the language the text is localized to;
        // switch to a compatible voice:
        NSArray *speechVoices = [AVSpeechSynthesisVoice speechVoices];
        for (AVSpeechSynthesisVoice *speechVoice in speechVoices) {
            if ([speechVoice.language hasPrefix:language]) {
                self.voice = speechVoice;
                break;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      相关资源
      最近更新 更多