mirror of
https://github.com/FatttSnake/OxygenToolbox.git
synced 2026-04-06 12:31:27 +08:00
Added multi-language switch
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.fatapp.oxygentoolbox.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.LocaleList;
|
||||
import android.text.TextUtils;
|
||||
import android.util.DisplayMetrics;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.appcompat.view.ContextThemeWrapper;
|
||||
import androidx.core.os.ConfigurationCompat;
|
||||
import androidx.core.os.LocaleListCompat;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class MultiLanguageUtils {
|
||||
|
||||
public static Context attachBaseContext(Context context) {
|
||||
String locale;
|
||||
if (SharedPreferencesUtils.isNull()) {
|
||||
locale = "default";
|
||||
} else {
|
||||
locale = SharedPreferencesUtils.getLocale();
|
||||
}
|
||||
String language;
|
||||
String country;
|
||||
if (!locale.equals("default")) {
|
||||
language = locale.substring(0, locale.indexOf("_"));
|
||||
country = locale.substring(locale.indexOf("_") + 1);
|
||||
} else {
|
||||
language = ResourceUtil.getSystemLocale().get(0).getLanguage();
|
||||
country = ResourceUtil.getSystemLocale().get(0).getCountry();
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
return createConfigurationContext(context, language, country);
|
||||
} else {
|
||||
return updateConfiguration(context, language, country);
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
private static Context createConfigurationContext(Context context, String language, String country) {
|
||||
Resources resources = context.getResources();
|
||||
Configuration configuration = resources.getConfiguration();
|
||||
Locale locale = new Locale(language, country);
|
||||
LocaleList localeList = new LocaleList(locale);
|
||||
configuration.setLocales(localeList);
|
||||
return context.createConfigurationContext(configuration);
|
||||
}
|
||||
|
||||
private static Context updateConfiguration(Context context, String language, String country) {
|
||||
Resources resources = context.getResources();
|
||||
Configuration configuration = resources.getConfiguration();
|
||||
Locale locale = new Locale(language, country);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
configuration.setLocales(new LocaleList(locale));
|
||||
} else {
|
||||
configuration.locale = locale;
|
||||
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
|
||||
resources.updateConfiguration(configuration, displayMetrics);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,12 @@ package com.fatapp.oxygentoolbox.util;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.Window;
|
||||
|
||||
import androidx.annotation.AttrRes;
|
||||
@@ -13,19 +16,20 @@ import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.StyleRes;
|
||||
import androidx.appcompat.view.ContextThemeWrapper;
|
||||
import androidx.core.os.ConfigurationCompat;
|
||||
import androidx.core.os.LocaleListCompat;
|
||||
|
||||
import com.google.android.material.color.MaterialColors;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class ResourceUtil {
|
||||
|
||||
private static Application sApp;
|
||||
private static Resources sRes;
|
||||
|
||||
public static void init(Application app) {
|
||||
sApp = app;
|
||||
sRes = app.getResources();
|
||||
}
|
||||
|
||||
public static Application getApplication() {
|
||||
@@ -33,15 +37,23 @@ public final class ResourceUtil {
|
||||
}
|
||||
|
||||
public static Resources getResources() {
|
||||
return sRes;
|
||||
return sApp.getResources();
|
||||
}
|
||||
|
||||
public static Configuration getConfiguration() {
|
||||
return sApp.getResources().getConfiguration();
|
||||
}
|
||||
|
||||
public static DisplayMetrics getDisplayMetrics() {
|
||||
return sApp.getResources().getDisplayMetrics();
|
||||
}
|
||||
|
||||
public static String getString(int resId) {
|
||||
return sRes.getString(resId);
|
||||
return sApp.getResources().getString(resId);
|
||||
}
|
||||
|
||||
public static int getColor(int resId) {
|
||||
return sRes.getColor(resId);
|
||||
return sApp.getResources().getColor(resId);
|
||||
}
|
||||
|
||||
@ColorInt
|
||||
@@ -82,4 +94,19 @@ public final class ResourceUtil {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static Locale getAppLocale() {
|
||||
Locale local;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
local = getConfiguration().getLocales().get(0);
|
||||
} else {
|
||||
local = getConfiguration().locale;
|
||||
}
|
||||
return local;
|
||||
}
|
||||
|
||||
public static LocaleListCompat getSystemLocale() {
|
||||
Configuration configuration = Resources.getSystem().getConfiguration();
|
||||
return ConfigurationCompat.getLocales(configuration);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.fatapp.oxygentoolbox.util;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
public class SharedPreferencesUtils {
|
||||
private static SharedPreferences preferences;
|
||||
|
||||
public static void init(Application app) {
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(app);
|
||||
}
|
||||
|
||||
public static String getLocale() {
|
||||
return preferences.getString("app_language", "default");
|
||||
}
|
||||
|
||||
public static boolean isNull() {
|
||||
return preferences == null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user