Magisk/app-core/src/main/java/com/topjohnwu/magisk/utils/LocaleManager.java

141 lines
5.0 KiB
Java
Raw Normal View History

2019-01-30 09:10:12 +01:00
package com.topjohnwu.magisk.utils;
2018-07-31 11:35:58 +02:00
2018-12-13 11:53:39 +01:00
import android.content.Context;
2018-07-31 11:35:58 +02:00
import android.content.res.Configuration;
import android.content.res.Resources;
2018-12-13 11:53:39 +01:00
import android.os.Build;
2018-07-31 11:35:58 +02:00
2019-01-30 09:10:12 +01:00
import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.Config;
2018-12-13 11:53:39 +01:00
import com.topjohnwu.superuser.Shell;
2018-07-31 11:35:58 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
2018-09-10 08:27:45 +02:00
import androidx.annotation.StringRes;
2018-07-31 11:35:58 +02:00
public class LocaleManager {
2018-07-31 21:09:44 +02:00
public static Locale locale = Locale.getDefault();
public final static Locale defaultLocale = Locale.getDefault();
2018-07-31 11:35:58 +02:00
public static List<Locale> locales;
2018-12-13 11:53:39 +01:00
public static Locale forLanguageTag(String tag) {
if (Build.VERSION.SDK_INT >= 21) {
return Locale.forLanguageTag(tag);
} else {
String[] tok = tag.split("-");
if (tok.length == 0) {
return new Locale("");
}
String language;
switch (tok[0]) {
case "und":
language = ""; // Undefined
break;
case "fil":
language = "tl"; // Filipino
break;
default:
language = tok[0];
}
if ((language.length() != 2 && language.length() != 3))
return new Locale("");
if (tok.length == 1)
return new Locale(language);
String country = tok[1];
if (country.length() != 2 && country.length() != 3)
return new Locale(language);
return new Locale(language, country);
}
}
2018-12-27 22:15:59 +01:00
public static String toLanguageTag(Locale loc) {
if (Build.VERSION.SDK_INT >= 21) {
return loc.toLanguageTag();
} else {
String language = loc.getLanguage();
String country = loc.getCountry();
String variant = loc.getVariant();
if (language.isEmpty() || !language.matches("\\p{Alpha}{2,8}")) {
language = "und"; // Follow the Locale#toLanguageTag() implementation
} else if (language.equals("iw")) {
language = "he"; // correct deprecated "Hebrew"
} else if (language.equals("in")) {
language = "id"; // correct deprecated "Indonesian"
} else if (language.equals("ji")) {
language = "yi"; // correct deprecated "Yiddish"
}
// ensure valid country code, if not well formed, it's omitted
if (!country.matches("\\p{Alpha}{2}|\\p{Digit}{3}")) {
country = "";
}
// variant subtags that begin with a letter must be at least 5 characters long
if (!variant.matches("\\p{Alnum}{5,8}|\\p{Digit}\\p{Alnum}{3}")) {
variant = "";
}
StringBuilder tag = new StringBuilder(language);
if (!country.isEmpty())
tag.append('-').append(country);
if (!variant.isEmpty())
tag.append('-').append(variant);
return tag.toString();
}
}
public static void setLocale(App app) {
2019-01-21 21:49:03 +01:00
String localeConfig = Config.get(Config.Key.LOCALE);
2018-07-31 21:09:44 +02:00
if (localeConfig.isEmpty()) {
2018-07-31 11:35:58 +02:00
locale = defaultLocale;
} else {
2018-12-13 11:53:39 +01:00
locale = forLanguageTag(localeConfig);
2018-07-31 11:35:58 +02:00
}
2018-08-01 08:16:44 +02:00
Locale.setDefault(locale);
2018-12-13 11:53:39 +01:00
app.setResources(getLocaleContext(locale).getResources());
}
public static Context getLocaleContext(Locale locale) {
Configuration config = new Configuration(App.self.getBaseContext().getResources().getConfiguration());
2018-07-31 11:35:58 +02:00
config.setLocale(locale);
2018-12-13 11:53:39 +01:00
return App.self.createConfigurationContext(config);
2018-07-31 11:35:58 +02:00
}
public static String getString(Locale locale, @StringRes int id) {
2018-12-13 11:53:39 +01:00
return getLocaleContext(locale).getString(id);
2018-07-31 11:35:58 +02:00
}
public static void loadAvailableLocales(@StringRes int compareId) {
2018-12-13 11:53:39 +01:00
Shell.EXECUTOR.execute(() -> {
2018-08-01 08:16:44 +02:00
locales = new ArrayList<>();
HashSet<String> set = new HashSet<>();
Resources res = App.self.getResources();
2018-08-01 08:16:44 +02:00
Locale locale;
2018-07-31 11:35:58 +02:00
2018-08-01 08:16:44 +02:00
// Add default locale
locales.add(Locale.ENGLISH);
set.add(getString(Locale.ENGLISH, compareId));
2018-07-31 11:35:58 +02:00
2018-08-01 08:16:44 +02:00
// Add some special locales
locales.add(Locale.TAIWAN);
set.add(getString(Locale.TAIWAN, compareId));
locale = new Locale("pt", "BR");
locales.add(locale);
set.add(getString(locale, compareId));
2018-07-31 11:35:58 +02:00
2018-08-01 08:16:44 +02:00
// Other locales
for (String s : res.getAssets().getLocales()) {
2018-12-13 11:53:39 +01:00
locale = forLanguageTag(s);
2018-08-01 08:16:44 +02:00
if (set.add(getString(locale, compareId))) {
locales.add(locale);
}
2018-07-31 11:35:58 +02:00
}
2018-08-01 08:16:44 +02:00
Collections.sort(locales, (a, b) -> a.getDisplayName(a).compareTo(b.getDisplayName(b)));
Topic.publish(Topic.LOCALE_FETCH_DONE);
2018-08-01 08:16:44 +02:00
});
2018-07-31 11:35:58 +02:00
}
}