Improved localization of help files

pull/184/head
M66B 4 years ago
parent b1ad9a542f
commit b3c6c3ea7d

@ -21,8 +21,11 @@ package eu.faircode.email;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.LocaleList;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
@ -33,6 +36,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@ -72,28 +76,48 @@ public class FragmentDialogMarkdown extends FragmentDialogBase {
@Override
protected Spanned onExecute(Context context, Bundle args) throws Throwable {
String name = args.getString("name");
String[] c = name.split("\\.");
if (name == null || !name.contains("."))
throw new IllegalArgumentException(name);
List<String> names = new ArrayList<>();
String[] c = name.split("\\.");
List<String> assets = Arrays.asList(getResources().getAssets().list(""));
String country = Locale.getDefault().getCountry();
String language = Locale.getDefault().getLanguage();
String localized = c[0] + "-" + language + "-r" + country + "." + c[1];
Log.i("Checking " + localized);
if (assets.contains(localized))
name = localized;
List<Locale> locales = new ArrayList<>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
locales.add(Locale.getDefault());
else {
String prefix = c[0] + "-" + language;
LocaleList ll = context.getResources().getConfiguration().getLocales();
for (int i = 0; i < ll.size(); i++)
locales.add(ll.get(i));
}
for (Locale locale : locales) {
String language = locale.getLanguage();
String country = locale.getCountry();
if ("en".equals(language) && "US".equals(country))
names.add(name);
else {
String localized = c[0] + "-" + language + "-r" + country + "." + c[1];
if (assets.contains(localized))
names.add(localized);
}
}
for (Locale locale : locales) {
String prefix = c[0] + "-" + locale.getLanguage();
for (String asset : assets)
if (asset.startsWith(prefix)) {
name = asset;
break;
}
if (asset.startsWith(prefix))
names.add(asset);
}
Log.i("Using " + name);
try (InputStream is = context.getAssets().open(name)) {
names.add(name);
String asset = names.get(0);
Log.i("Using " + asset +
" of " + TextUtils.join(",", names) +
" (" + TextUtils.join(",", locales) + ")");
try (InputStream is = context.getAssets().open(asset)) {
byte[] buffer = new byte[is.available()];
is.read(buffer);
Markwon markwon = Markwon.create(context);

Loading…
Cancel
Save