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

@ -1,317 +0,0 @@
[
{
"name": "Arabic",
"code": "ar",
"longCode": "ar"
},
{
"name": "Asturian",
"code": "ast",
"longCode": "ast-ES"
},
{
"name": "Belarusian",
"code": "be",
"longCode": "be-BY"
},
{
"name": "Breton",
"code": "br",
"longCode": "br-FR"
},
{
"name": "Catalan",
"code": "ca",
"longCode": "ca-ES"
},
{
"name": "Catalan (Valencian)",
"code": "ca",
"longCode": "ca-ES-valencia"
},
{
"name": "Chinese",
"code": "zh",
"longCode": "zh-CN"
},
{
"name": "Danish",
"code": "da",
"longCode": "da-DK"
},
{
"name": "Dutch",
"code": "nl",
"longCode": "nl"
},
{
"name": "Dutch",
"code": "nl",
"longCode": "nl"
},
{
"name": "Dutch (Belgium)",
"code": "nl",
"longCode": "nl-BE"
},
{
"name": "English",
"code": "en",
"longCode": "en"
},
{
"name": "English",
"code": "en",
"longCode": "en"
},
{
"name": "English (Australian)",
"code": "en",
"longCode": "en-AU"
},
{
"name": "English (Australian)",
"code": "en",
"longCode": "en-AU"
},
{
"name": "English (Canadian)",
"code": "en",
"longCode": "en-CA"
},
{
"name": "English (Canadian)",
"code": "en",
"longCode": "en-CA"
},
{
"name": "English (GB)",
"code": "en",
"longCode": "en-GB"
},
{
"name": "English (GB)",
"code": "en",
"longCode": "en-GB"
},
{
"name": "English (New Zealand)",
"code": "en",
"longCode": "en-NZ"
},
{
"name": "English (New Zealand)",
"code": "en",
"longCode": "en-NZ"
},
{
"name": "English (South African)",
"code": "en",
"longCode": "en-ZA"
},
{
"name": "English (South African)",
"code": "en",
"longCode": "en-ZA"
},
{
"name": "English (US)",
"code": "en",
"longCode": "en-US"
},
{
"name": "English (US)",
"code": "en",
"longCode": "en-US"
},
{
"name": "Esperanto",
"code": "eo",
"longCode": "eo"
},
{
"name": "French",
"code": "fr",
"longCode": "fr"
},
{
"name": "French",
"code": "fr",
"longCode": "fr"
},
{
"name": "Galician",
"code": "gl",
"longCode": "gl-ES"
},
{
"name": "German",
"code": "de",
"longCode": "de"
},
{
"name": "German",
"code": "de",
"longCode": "de"
},
{
"name": "German (Austria)",
"code": "de",
"longCode": "de-AT"
},
{
"name": "German (Austria)",
"code": "de",
"longCode": "de-AT"
},
{
"name": "German (Germany)",
"code": "de",
"longCode": "de-DE"
},
{
"name": "German (Germany)",
"code": "de",
"longCode": "de-DE"
},
{
"name": "German (Swiss)",
"code": "de",
"longCode": "de-CH"
},
{
"name": "German (Swiss)",
"code": "de",
"longCode": "de-CH"
},
{
"name": "Greek",
"code": "el",
"longCode": "el-GR"
},
{
"name": "Irish",
"code": "ga",
"longCode": "ga-IE"
},
{
"name": "Italian",
"code": "it",
"longCode": "it"
},
{
"name": "Japanese",
"code": "ja",
"longCode": "ja-JP"
},
{
"name": "Khmer",
"code": "km",
"longCode": "km-KH"
},
{
"name": "Norwegian (Bokmål)",
"code": "nb",
"longCode": "nb"
},
{
"name": "Norwegian (Bokmål)",
"code": "no",
"longCode": "no"
},
{
"name": "Persian",
"code": "fa",
"longCode": "fa"
},
{
"name": "Polish",
"code": "pl",
"longCode": "pl-PL"
},
{
"name": "Portuguese",
"code": "pt",
"longCode": "pt"
},
{
"name": "Portuguese (Angola preAO)",
"code": "pt",
"longCode": "pt-AO"
},
{
"name": "Portuguese (Brazil)",
"code": "pt",
"longCode": "pt-BR"
},
{
"name": "Portuguese (Moçambique preAO)",
"code": "pt",
"longCode": "pt-MZ"
},
{
"name": "Portuguese (Portugal)",
"code": "pt",
"longCode": "pt-PT"
},
{
"name": "Romanian",
"code": "ro",
"longCode": "ro-RO"
},
{
"name": "Russian",
"code": "ru",
"longCode": "ru-RU"
},
{
"name": "Simple German",
"code": "de-DE-x-simple-language",
"longCode": "de-DE-x-simple-language"
},
{
"name": "Slovak",
"code": "sk",
"longCode": "sk-SK"
},
{
"name": "Slovenian",
"code": "sl",
"longCode": "sl-SI"
},
{
"name": "Spanish",
"code": "es",
"longCode": "es"
},
{
"name": "Spanish",
"code": "es",
"longCode": "es"
},
{
"name": "Spanish (voseo)",
"code": "es",
"longCode": "es-AR"
},
{
"name": "Swedish",
"code": "sv",
"longCode": "sv"
},
{
"name": "Tagalog",
"code": "tl",
"longCode": "tl-PH"
},
{
"name": "Tamil",
"code": "ta",
"longCode": "ta-IN"
},
{
"name": "Ukrainian",
"code": "uk",
"longCode": "uk-UA"
}
]

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)