Skip to content

Commit

Permalink
Create LanguageUtils class
Browse files Browse the repository at this point in the history
Moved the localeFromStringIgnoringScriptAndExtensions function
to the LanguageUtils class.
  • Loading branch information
mikunimaru committed Jun 2, 2021
1 parent d9023e2 commit 65e84f9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 66 deletions.
31 changes: 3 additions & 28 deletions AnkiDroid/src/main/java/com/ichi2/anki/JavaScriptTTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import android.content.Context;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import java.util.Locale;

import com.ichi2.anki.LanguageUtils;

/**
* Since it is assumed that only advanced users will use the JavaScript api,
Expand Down Expand Up @@ -63,7 +64,7 @@ public int speak(String text) {
public int setLanguage(String loc) {
// The Int values will be returned
// 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.
return mTts.setLanguage(localeFromStringIgnoringScriptAndExtensions(loc));
return mTts.setLanguage(LanguageUtils.localeFromStringIgnoringScriptAndExtensions(loc));
}


Expand Down Expand Up @@ -98,30 +99,4 @@ public void stop() {
mTts.stop();
}

/**
* Convert a string representation of a locale, in the format returned by Locale.toString(),
* into a Locale object, disregarding any script and extensions fields (i.e. using solely the
* language, country and variant fields).
* <p>
* Returns a Locale object constructed from an empty string if the input string is null, empty
* or contains more than 3 fields separated by underscores.
*/
private static Locale localeFromStringIgnoringScriptAndExtensions(String localeCode) {
if (localeCode == null) {
return new Locale("");
}

String[] fields = localeCode.split("_");
switch (fields.length) {
case 1:
return new Locale(fields[0]);
case 2:
return new Locale(fields[0], fields[1]);
case 3:
return new Locale(fields[0], fields[1], fields[2]);
default:
return new Locale("");
}
}

}
42 changes: 42 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/LanguageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ichi2.anki;

import java.util.Locale;

public class LanguageUtils {

/**
* Convert a string representation of a locale, in the format returned by Locale.toString(),
* into a Locale object, disregarding any script and extensions fields (i.e. using solely the
* language, country and variant fields).
* <p>
* Returns a Locale object constructed from an empty string if the input string is null, empty
* or contains more than 3 fields separated by underscores.
*/
public static Locale localeFromStringIgnoringScriptAndExtensions(String localeCode) {
if (localeCode == null) {
return new Locale("");
}

localeCode = stripScriptAndExtensions(localeCode);

String[] fields = localeCode.split("_");
switch (fields.length) {
case 1:
return new Locale(fields[0]);
case 2:
return new Locale(fields[0], fields[1]);
case 3:
return new Locale(fields[0], fields[1], fields[2]);
default:
return new Locale("");
}
}

private static String stripScriptAndExtensions(String localeCode) {
int hashPos = localeCode.indexOf('#');
if (hashPos >= 0) {
localeCode = localeCode.substring(0, hashPos);
}
return localeCode;
}
}
41 changes: 3 additions & 38 deletions AnkiDroid/src/main/java/com/ichi2/anki/ReadText.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.material.snackbar.Snackbar;
import com.ichi2.libanki.Sound;
import com.ichi2.anki.LanguageUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,7 +57,7 @@ public static Sound.SoundSide getmQuestionAnswer() {
}

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

/**
* Convert a string representation of a locale, in the format returned by Locale.toString(),
* into a Locale object, disregarding any script and extensions fields (i.e. using solely the
* language, country and variant fields).
* <p>
* Returns a Locale object constructed from an empty string if the input string is null, empty
* or contains more than 3 fields separated by underscores.
*/
private static Locale localeFromStringIgnoringScriptAndExtensions(String localeCode) {
if (localeCode == null) {
return new Locale("");
}

localeCode = stripScriptAndExtensions(localeCode);

String[] fields = localeCode.split("_");
switch (fields.length) {
case 1:
return new Locale(fields[0]);
case 2:
return new Locale(fields[0], fields[1]);
case 3:
return new Locale(fields[0], fields[1], fields[2]);
default:
return new Locale("");
}
}

private static String stripScriptAndExtensions(String localeCode) {
int hashPos = localeCode.indexOf('#');
if (hashPos >= 0) {
localeCode = localeCode.substring(0, hashPos);
}
return localeCode;
}

/**
* Returns true if the TTS engine supports the language of the locale represented by localeCode
* (which should be in the format returned by Locale.toString()), false otherwise.
*/
private static boolean isLanguageAvailable(String localeCode) {
return mTts.isLanguageAvailable(localeFromStringIgnoringScriptAndExtensions(localeCode)) >=
return mTts.isLanguageAvailable(LanguageUtils.localeFromStringIgnoringScriptAndExtensions(localeCode)) >=
TextToSpeech.LANG_AVAILABLE;
}

Expand Down

0 comments on commit 65e84f9

Please sign in to comment.