iosnslocaleavspeechsynthesizer

Speech Synthezis API : which locale is used by NSLocalizedString?


I want to use iOS 7 new speech synthezis API, and my application is localized in french & english.

For this to work, 2 things have to be localized :

Class instanciation method is AVSpeechSynthesisVoice voiceWithLanguage:(NSString *)lang. I'm currently using [NSLocale currentLocale].localeIdentifier as parameter for this method.

Problem : if user's device language is Portuguese, [NSLocale currentLocale] select portuguese prononciation, while text resolved by NSLocalizedString is english.

How can I know which locale is currently read by NSLocalizedString ?


Solution

  • Ok, I finally managed to make sense of Apple APIs :

    So this is what my final code looks like

     // 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;
    

    This way, a user from NewZealand, Australien, GreatBritain, or Canada will get the voice that correspond most to his usual settings.