diff options
author | Fabio Falsini <falsinsoft@gmail.com> | 2020-07-24 21:47:45 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2020-08-16 06:59:34 +0000 |
commit | b138b48ca54d0cb6abbd4437300c2237e259108f (patch) | |
tree | c04e5a1206a5aa10a190de44e703eb7945d1f1df | |
parent | f02333202ab28282b4f5393e656ad83ba43ef6a0 (diff) |
Wrong tts language parsing on some Android devices
Current implementation for getting the list of supported tts languages
in Android gets the output of toString() to create a QLocale object.
However there is no guarantee that these calls will return two-digit
codes as required by the QLocale constructor. For example, a Samsung s10e
device returns three-digit codes ("eng" instead of "en") which causes
the instantiated QLocale object to return a generic "C" language instead
of the real locale.
This patch checks if the language and country code is two-digit or three-digit
and, in the second case, convert the three-digit code into two-digit code to
allow QLocale to be initialized correctly.
Change-Id: I02ebe57b2475f7fe74f2830b80f275ddb9b42762
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit a9931b56420d381ed7ce534a89e1f5b19eb57383)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/plugins/tts/android/jar/src/org/qtproject/qt5/android/speech/QtTextToSpeech.java | 44 | ||||
-rw-r--r-- | src/plugins/tts/android/src/qtexttospeech_android.cpp | 7 |
2 files changed, 48 insertions, 3 deletions
diff --git a/src/plugins/tts/android/jar/src/org/qtproject/qt5/android/speech/QtTextToSpeech.java b/src/plugins/tts/android/jar/src/org/qtproject/qt5/android/speech/QtTextToSpeech.java index ed14347..b50d748 100644 --- a/src/plugins/tts/android/jar/src/org/qtproject/qt5/android/speech/QtTextToSpeech.java +++ b/src/plugins/tts/android/jar/src/org/qtproject/qt5/android/speech/QtTextToSpeech.java @@ -52,6 +52,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.List; import java.util.ArrayList; +import java.util.Set; public class QtTextToSpeech { @@ -229,7 +230,22 @@ public class QtTextToSpeech { if (mInitialized && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { //Log.d("QtTextToSpeech", "Locales: " + mTts.getAvailableLanguages()); - return new ArrayList<Locale>(mTts.getAvailableLanguages()); + final Set<Locale> languages = mTts.getAvailableLanguages(); + ArrayList<Locale> locales = new ArrayList<Locale>(); + + for (Locale language : languages) { + String languageCode = language.getLanguage(); + String countryCode = language.getCountry(); + + if (languageCode.equals(language.getISO3Language())) + languageCode = convertLanguageCodeThreeDigitToTwoDigit(languageCode); + if (countryCode.equals(language.getISO3Country())) + countryCode = convertCountryCodeThreeDigitToTwoDigit(countryCode); + + locales.add(new Locale(languageCode, countryCode)); + } + + return locales; } return new ArrayList<Locale>(); } @@ -266,4 +282,30 @@ public class QtTextToSpeech } return false; } + + private String convertLanguageCodeThreeDigitToTwoDigit(String iso3Language) + { + final String[] isoLanguages = Locale.getISOLanguages(); + + for (String isoLanguage : isoLanguages) { + if (iso3Language.equals(new Locale(isoLanguage).getISO3Language())) { + return isoLanguage; + } + } + + return iso3Language; + } + + private String convertCountryCodeThreeDigitToTwoDigit(String iso3Country) + { + final String[] isoCountries = Locale.getISOCountries(); + + for (String isoCountry : isoCountries) { + if (iso3Country.equals(new Locale("en", isoCountry).getISO3Country())) { + return isoCountry; + } + } + + return iso3Country; + } } diff --git a/src/plugins/tts/android/src/qtexttospeech_android.cpp b/src/plugins/tts/android/src/qtexttospeech_android.cpp index b4bb006..02c57b1 100644 --- a/src/plugins/tts/android/src/qtexttospeech_android.cpp +++ b/src/plugins/tts/android/src/qtexttospeech_android.cpp @@ -256,8 +256,11 @@ QVector<QLocale> QTextToSpeechEngineAndroid::availableLocales() const result.reserve(count); for (int i = 0; i < count; ++i) { auto locale = locales.callObjectMethod("get", "(I)Ljava/lang/Object;", i); - auto localeName = locale.callObjectMethod<jstring>("toString").toString(); - result << QLocale(localeName); + auto localeLanguage = locale.callObjectMethod<jstring>("getLanguage").toString(); + auto localeCountry = locale.callObjectMethod<jstring>("getCountry").toString(); + if (!localeCountry.isEmpty()) + localeLanguage += QString("_%1").arg(localeCountry).toUpper(); + result << QLocale(localeLanguage); } return result; } |