Skip to content

Commit 65e84f9

Browse files
committed
Create LanguageUtils class
Moved the localeFromStringIgnoringScriptAndExtensions function to the LanguageUtils class.
1 parent d9023e2 commit 65e84f9

3 files changed

Lines changed: 48 additions & 66 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/JavaScriptTTS.java

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import android.content.Context;
44
import android.os.Bundle;
55
import android.speech.tts.TextToSpeech;
6-
import java.util.Locale;
6+
7+
import com.ichi2.anki.LanguageUtils;
78

89
/**
910
* Since it is assumed that only advanced users will use the JavaScript api,
@@ -63,7 +64,7 @@ public int speak(String text) {
6364
public int setLanguage(String loc) {
6465
// The Int values will be returned
6566
// Code indicating the support status for the locale. See LANG_AVAILABLE, LANG_COUNTRY_AVAILABLE, LANG_COUNTRY_VAR_AVAILABLE, LANG_MISSING_DATA and LANG_NOT_SUPPORTED.
66-
return mTts.setLanguage(localeFromStringIgnoringScriptAndExtensions(loc));
67+
return mTts.setLanguage(LanguageUtils.localeFromStringIgnoringScriptAndExtensions(loc));
6768
}
6869

6970

@@ -98,30 +99,4 @@ public void stop() {
9899
mTts.stop();
99100
}
100101

101-
/**
102-
* Convert a string representation of a locale, in the format returned by Locale.toString(),
103-
* into a Locale object, disregarding any script and extensions fields (i.e. using solely the
104-
* language, country and variant fields).
105-
* <p>
106-
* Returns a Locale object constructed from an empty string if the input string is null, empty
107-
* or contains more than 3 fields separated by underscores.
108-
*/
109-
private static Locale localeFromStringIgnoringScriptAndExtensions(String localeCode) {
110-
if (localeCode == null) {
111-
return new Locale("");
112-
}
113-
114-
String[] fields = localeCode.split("_");
115-
switch (fields.length) {
116-
case 1:
117-
return new Locale(fields[0]);
118-
case 2:
119-
return new Locale(fields[0], fields[1]);
120-
case 3:
121-
return new Locale(fields[0], fields[1], fields[2]);
122-
default:
123-
return new Locale("");
124-
}
125-
}
126-
127102
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.ichi2.anki;
2+
3+
import java.util.Locale;
4+
5+
public class LanguageUtils {
6+
7+
/**
8+
* Convert a string representation of a locale, in the format returned by Locale.toString(),
9+
* into a Locale object, disregarding any script and extensions fields (i.e. using solely the
10+
* language, country and variant fields).
11+
* <p>
12+
* Returns a Locale object constructed from an empty string if the input string is null, empty
13+
* or contains more than 3 fields separated by underscores.
14+
*/
15+
public static Locale localeFromStringIgnoringScriptAndExtensions(String localeCode) {
16+
if (localeCode == null) {
17+
return new Locale("");
18+
}
19+
20+
localeCode = stripScriptAndExtensions(localeCode);
21+
22+
String[] fields = localeCode.split("_");
23+
switch (fields.length) {
24+
case 1:
25+
return new Locale(fields[0]);
26+
case 2:
27+
return new Locale(fields[0], fields[1]);
28+
case 3:
29+
return new Locale(fields[0], fields[1], fields[2]);
30+
default:
31+
return new Locale("");
32+
}
33+
}
34+
35+
private static String stripScriptAndExtensions(String localeCode) {
36+
int hashPos = localeCode.indexOf('#');
37+
if (hashPos >= 0) {
38+
localeCode = localeCode.substring(0, hashPos);
39+
}
40+
return localeCode;
41+
}
42+
}

AnkiDroid/src/main/java/com/ichi2/anki/ReadText.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.afollestad.materialdialogs.MaterialDialog;
3030
import com.google.android.material.snackbar.Snackbar;
3131
import com.ichi2.libanki.Sound;
32+
import com.ichi2.anki.LanguageUtils;
3233

3334
import java.lang.ref.WeakReference;
3435
import java.util.ArrayList;
@@ -56,7 +57,7 @@ public static Sound.SoundSide getmQuestionAnswer() {
5657
}
5758

5859
public static void speak(String text, String loc, int queueMode) {
59-
int result = mTts.setLanguage(localeFromStringIgnoringScriptAndExtensions(loc));
60+
int result = mTts.setLanguage(LanguageUtils.localeFromStringIgnoringScriptAndExtensions(loc));
6061
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
6162
UIUtils.showThemedToast(mReviewer.get(), mReviewer.get().getString(R.string.no_tts_available_message)
6263
+ " (" + loc + ")", false);
@@ -218,48 +219,12 @@ private static void textToSpeech(String text, long did, int ord, Sound.SoundSide
218219
selectTts(mTextToSpeak, mDid, mOrd, mQuestionAnswer);
219220
}
220221

221-
/**
222-
* Convert a string representation of a locale, in the format returned by Locale.toString(),
223-
* into a Locale object, disregarding any script and extensions fields (i.e. using solely the
224-
* language, country and variant fields).
225-
* <p>
226-
* Returns a Locale object constructed from an empty string if the input string is null, empty
227-
* or contains more than 3 fields separated by underscores.
228-
*/
229-
private static Locale localeFromStringIgnoringScriptAndExtensions(String localeCode) {
230-
if (localeCode == null) {
231-
return new Locale("");
232-
}
233-
234-
localeCode = stripScriptAndExtensions(localeCode);
235-
236-
String[] fields = localeCode.split("_");
237-
switch (fields.length) {
238-
case 1:
239-
return new Locale(fields[0]);
240-
case 2:
241-
return new Locale(fields[0], fields[1]);
242-
case 3:
243-
return new Locale(fields[0], fields[1], fields[2]);
244-
default:
245-
return new Locale("");
246-
}
247-
}
248-
249-
private static String stripScriptAndExtensions(String localeCode) {
250-
int hashPos = localeCode.indexOf('#');
251-
if (hashPos >= 0) {
252-
localeCode = localeCode.substring(0, hashPos);
253-
}
254-
return localeCode;
255-
}
256-
257222
/**
258223
* Returns true if the TTS engine supports the language of the locale represented by localeCode
259224
* (which should be in the format returned by Locale.toString()), false otherwise.
260225
*/
261226
private static boolean isLanguageAvailable(String localeCode) {
262-
return mTts.isLanguageAvailable(localeFromStringIgnoringScriptAndExtensions(localeCode)) >=
227+
return mTts.isLanguageAvailable(LanguageUtils.localeFromStringIgnoringScriptAndExtensions(localeCode)) >=
263228
TextToSpeech.LANG_AVAILABLE;
264229
}
265230

0 commit comments

Comments
 (0)