locales method

Future<List<LocaleName>> locales()

Returns the list of speech locales available on the device or those supported by the speech recognizer on the device.

Being on this list does not guarantee that the device will be able to recognize the locale. It is just a list of locales that the device can recognize if the language is installed. You may have to advise users of your application to install their desired language on their device.

This method is useful to find the identifier to use for the listen method, it is the localeId member of the LocaleName.

Each LocaleName in the returned list has the identifier for the locale as well as a name for display. The name is localized for the system locale on the device.

Android: The list of languages is based on the locales supported by the on device recognizer. This list may not be the complete list of languages available for online recognition. Unfortunately there is no way to get the list of languages supported by the online recognizer.

Implementation

Future<List<LocaleName>> locales() async {
  final locales = await SpeechToTextPlatform.instance.locales();
  var filteredLocales = locales
      .map((locale) {
        var components = locale.split(':');
        if (components.length != 2) {
          return null;
        }
        return LocaleName(components[0], components[1]);
      })
      .where((item) => item != null)
      .toList()
      .cast<LocaleName>();
  if (filteredLocales.isNotEmpty) {
    _systemLocale = filteredLocales.first;
  } else {
    _systemLocale = null;
  }
  filteredLocales.sort((ln1, ln2) => ln1.name.compareTo(ln2.name));
  return filteredLocales;
}