JohnPix
JohnPix

Reputation: 1843

Change default language for Speech recognition in my app

I make an app in English. My app uses Speech recognition. But if I install this app on device with another system language, French or Russian for example. My speech recognition doesn't work. It works only for language which by default in system. How can I make English language for Speech recognition by default for my app?

I found this method but it doesn't work

Locale myLocale;
    myLocale = new Locale("English (US)", "en_US");
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

Upvotes: 2

Views: 4375

Answers (1)

Rajan Bhavsar
Rajan Bhavsar

Reputation: 1857

You can try with this code:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

Also, your app can query for the list of supported languages by sending a RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS ordered broadcast like so:

 Intent detailsIntent =  new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
sendOrderedBroadcast(
        detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);

where LanguageDetailsChecker is something like this:

public class LanguageDetailsChecker extends BroadcastReceiver
{
private List<String> supportedLanguages;

private String languagePreference;

@Override
public void onReceive(Context context, Intent intent)
{
    Bundle results = getResultExtras(true);
    if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE))
    {
        languagePreference =
                results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
    }
    if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
    {
        supportedLanguages =
                results.getStringArrayList(
                        RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
    }
}
}

You can also check out the complete code for that at here:https://github.com/gast-lib

Upvotes: 5

Related Questions