From f455a466c3fe57b40979c8df93245de26443c5d0 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Wed, 16 Jan 2013 18:42:33 +0000 Subject: [PATCH] Do not add a voice if the Locale assigns a blank ISO3 language code. --- .../espeak/test/SpeechSynthesisTest.java | 4 +- .../espeak/test/TextToSpeechTest.java | 5 +- .../com/reecedunn/espeak/SpeechSynthesis.java | 89 ++++++++++--------- 3 files changed, 52 insertions(+), 46 deletions(-) diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/SpeechSynthesisTest.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/SpeechSynthesisTest.java index cb37af67..a8ee8d87 100644 --- a/android/eSpeakTests/src/com/reecedunn/espeak/test/SpeechSynthesisTest.java +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/SpeechSynthesisTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 Reece H. Dunn + * Copyright (C) 2012-2013 Reece H. Dunn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -206,7 +206,7 @@ public class SpeechSynthesisTest extends AndroidTestCase public void testRemovedVoices() { getVoices(); // Ensure that the voice data has been populated. - assertThat(mRemoved.toString(), is("[]")); + assertThat(mRemoved.toString(), is("[nci, prs]")); } public void testVoiceData() diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java index 72bf441c..350ffba9 100644 --- a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 Reece H. Dunn + * Copyright (C) 2012-2013 Reece H. Dunn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -93,6 +93,9 @@ public class TextToSpeechTest extends AndroidTestCase { try { + // Skip the voice if the language code is not supported by Android: + if (data.javaLanguage.equals("")) continue; + final Locale iana1 = new Locale(data.ianaLanguage, data.ianaCountry, data.variant); final Locale iana2 = new Locale(data.ianaLanguage, data.ianaCountry, "test"); final Locale iana3 = new Locale(data.ianaLanguage, "VU", data.variant); diff --git a/android/src/com/reecedunn/espeak/SpeechSynthesis.java b/android/src/com/reecedunn/espeak/SpeechSynthesis.java index 336ba05c..a9afba84 100644 --- a/android/src/com/reecedunn/espeak/SpeechSynthesis.java +++ b/android/src/com/reecedunn/espeak/SpeechSynthesis.java @@ -109,9 +109,51 @@ public class SpeechSynthesis { final String identifier = results[i + 1]; final int gender = Integer.parseInt(results[i + 2]); final int age = Integer.parseInt(results[i + 3]); - final Voice voice = new Voice(name, identifier, gender, age); + final Locale locale; + if (name.equals("en-sc")) { + // 'SC' is not a country code. + locale = new Locale("en", "GB", "scotland"); + } else if (name.equals("en-wi")) { + // 'WI' is not a country code. + locale = new Locale("en", "029"); + } else if (name.equals("es-la")) { + // 'LA' is the country code for Laos, not Latin America. + locale = new Locale("es", "419"); + } else if (name.equals("hy-west")) { + // 'west' is not a country code. + locale = new Locale("hy", "", "arevmda"); + } else if (name.equals("zh-yue")) { + // Android/Java does not support macrolanguages. + locale = new Locale("zh", "HK"); + } else { + String[] parts = name.split("-"); + switch (parts.length) { + case 1: // language + locale = new Locale(parts[0]); + break; + case 2: // language-country + if (parts[1].equals("uk")) { + // 'uk' is the language code for Ukranian, not Great Britain. + parts[1] = "GB"; + } + locale = new Locale(parts[0], parts[1]); + break; + case 3: // language-country-variant + if (parts[1].equals("uk")) { + // 'uk' is the language code for Ukranian, not Great Britain. + parts[1] = "GB"; + } + locale = new Locale(parts[0], parts[1], parts[2]); + break; + default: + locale = null; + } + } - voices.add(voice); + if (locale != null && !locale.getISO3Language().equals("")) { + final Voice voice = new Voice(name, identifier, gender, age, locale); + voices.add(voice); + } } return voices; @@ -225,51 +267,12 @@ public class SpeechSynthesis { public final int age; public final Locale locale; - public Voice(String name, String identifier, int gender, int age) { + public Voice(String name, String identifier, int gender, int age, Locale locale) { this.name = name; this.identifier = identifier; this.gender = gender; this.age = age; - - if (name.equals("en-sc")) { - // 'SC' is not a country code. - locale = new Locale("en", "GB", "scotland"); - } else if (name.equals("en-wi")) { - // 'WI' is not a country code. - locale = new Locale("en", "029"); - } else if (name.equals("es-la")) { - // 'LA' is the country code for Laos, not Latin America. - locale = new Locale("es", "419"); - } else if (name.equals("hy-west")) { - // 'west' is not a country code. - locale = new Locale("hy", "", "arevmda"); - } else if (name.equals("zh-yue")) { - // Android/Java does not support macrolanguages. - locale = new Locale("zh", "HK"); - } else { - String[] parts = name.split("-"); - switch (parts.length) { - case 1: // language - locale = new Locale(parts[0]); - break; - case 2: // language-country - if (parts[1].equals("uk")) { - // 'uk' is the language code for Ukranian, not Great Britain. - parts[1] = "GB"; - } - locale = new Locale(parts[0], parts[1]); - break; - case 3: // language-country-variant - if (parts[1].equals("uk")) { - // 'uk' is the language code for Ukranian, not Great Britain. - parts[1] = "GB"; - } - locale = new Locale(parts[0], parts[1], parts[2]); - break; - default: - locale = null; - } - } + this.locale = locale; } /**