Skip to content

Commit 39268ff

Browse files
committed
Format default locale the same way as those stored in prefs.
Also explicitly disallow locales with empty countries. This is required to match them against the set of engine supported locales. bug:5309930 Change-Id: Ie9714fdc09d3081081a2393d97c31e3a42bca294
1 parent 840b8a6 commit 39268ff

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

core/java/android/speech/tts/TtsEngines.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,10 @@ private String getV1Locale() {
344344
String v1Locale = lang;
345345
if (!TextUtils.isEmpty(country)) {
346346
v1Locale += LOCALE_DELIMITER + country;
347+
} else {
348+
return v1Locale;
347349
}
350+
348351
if (!TextUtils.isEmpty(variant)) {
349352
v1Locale += LOCALE_DELIMITER + variant;
350353
}
@@ -355,8 +358,28 @@ private String getV1Locale() {
355358
private String getDefaultLocale() {
356359
final Locale locale = Locale.getDefault();
357360

358-
return locale.getISO3Language() + LOCALE_DELIMITER + locale.getISO3Country() +
359-
LOCALE_DELIMITER + locale.getVariant();
361+
// Note that the default locale might have an empty variant
362+
// or language, and we take care that the construction is
363+
// the same as {@link #getV1Locale} i.e no trailing delimiters
364+
// or spaces.
365+
String defaultLocale = locale.getISO3Language();
366+
if (TextUtils.isEmpty(defaultLocale)) {
367+
Log.w(TAG, "Default locale is empty.");
368+
return "";
369+
}
370+
371+
if (!TextUtils.isEmpty(locale.getISO3Country())) {
372+
defaultLocale += LOCALE_DELIMITER + locale.getISO3Country();
373+
} else {
374+
// Do not allow locales of the form lang--variant with
375+
// an empty country.
376+
return defaultLocale;
377+
}
378+
if (!TextUtils.isEmpty(locale.getVariant())) {
379+
defaultLocale += LOCALE_DELIMITER + locale.getVariant();
380+
}
381+
382+
return defaultLocale;
360383
}
361384

362385
/**

0 commit comments

Comments
 (0)