LT: get languages via API

This commit is contained in:
M66B
2022-11-08 09:01:19 +01:00
parent dcec107d05
commit 7f5fa181d3
2 changed files with 35 additions and 322 deletions

View File

@@ -53,6 +53,8 @@ public class LanguageTool {
static final String LT_URI_PLUS = "https://api.languagetoolplus.com/v2/";
private static final int LT_TIMEOUT = 20; // seconds
private static JSONArray jlanguages = null;
static boolean isEnabled(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean("lt_enabled", false);
@@ -65,6 +67,37 @@ public class LanguageTool {
return (lt_enabled && lt_auto);
}
static JSONArray getLanguages(Context context) throws IOException, JSONException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String lt_uri = prefs.getString("lt_uri", LT_URI_PLUS);
// https://languagetool.org/http-api/swagger-ui/#!/default/get_words
Uri uri = Uri.parse(lt_uri).buildUpon().appendPath("languages").build();
Log.i("LT uri=" + uri);
URL url = new URL(uri.toString());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(false);
connection.setReadTimeout(LT_TIMEOUT * 1000);
connection.setConnectTimeout(LT_TIMEOUT * 1000);
ConnectionHelper.setUserAgent(context, connection);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
try {
checkStatus(connection);
String response = Helper.readStream(connection.getInputStream());
Log.i("LT response=" + response);
return new JSONArray(response);
} finally {
connection.disconnect();
}
}
static List<Suggestion> getSuggestions(Context context, CharSequence text) throws IOException, JSONException {
if (TextUtils.isEmpty(text))
return new ArrayList<>();
@@ -82,11 +115,8 @@ public class LanguageTool {
.appendQueryParameter("language", "auto");
// curl -X GET --header 'Accept: application/json' 'https://api.languagetool.org/v2/languages'
JSONArray jlanguages;
try (InputStream is = context.getAssets().open("lt.json")) {
String json = Helper.readStream(is);
jlanguages = new JSONArray(json);
}
if (jlanguages == null)
jlanguages = getLanguages(context);
List<Locale> locales = new ArrayList<>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)