请按照以下步骤操作:
注意:我假设您在值文件夹中为不同的英语、西班牙语和法语创建了不同的字符串文件。 (即res目录中每个values-en、values-es、values-fr文件夹中的strings.xml文件)
添加此文件:LocaleHelper.java
package com.test;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.preference.PreferenceManager;
import java.util.Locale;
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Configuration configuration = context.getResources().getConfiguration();
LocaleList localeList = new LocaleList(locale);
localeList.setDefault(localeList);
configuration.setLocales(localeList);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
添加文件:LanguageUtil.java
package com.test;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import java.util.Locale;
public class LanguageUtil {
public static void changeLanguageType(Context context, Locale localelanguage) {
Log.i("=======", "context = " + context);
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
// Application user selects language
// reference https://developer.android.com/reference/android/content/res/Configuration.html
if (VersionUtils.isAfter24()) {
config.setLocale(localelanguage);
} else {
config.locale = localelanguage;
resources.updateConfiguration(config, dm);
}
}
public static Locale getLanguageType(Context context) {
Log.i("=======", "context = " + context);
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
// Application user selects language
if (VersionUtils.isAfter24()) {
return config.getLocales().get(0);
} else {
return config.locale;
}
}
}
添加文件:MyContextWrapper.java
package com.test;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import java.util.Locale;
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
@TargetApi(Build.VERSION_CODES.N)
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (VersionUtils.isAfter24()) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (VersionUtils.isAfter17()) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
return new ContextWrapper(context);
}
}
添加文件:VersionUtils.java
package com.test;
import android.os.Build;
import android.support.annotation.RequiresApi;
public final class VersionUtils {
@RequiresApi(Build.VERSION_CODES.N_MR1)
public static boolean isAfter25() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1;
}
@RequiresApi(Build.VERSION_CODES.N)
public static boolean isAfter24() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
}
public static boolean isAfter23() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}
public static boolean isAfter22() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
}
public static boolean isAfter21() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
public static boolean isAfter20() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH;
}
public static boolean isAfter19() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}
public static boolean isAfter18() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
}
public static boolean isAfter17() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
}
public static boolean isAfter16() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
}
public static boolean isAfter14() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
public static boolean isAfter13() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2;
}
public static boolean isAfter11() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}
public static boolean isAfter9() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
}
public static boolean isAfter8() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
}
public static boolean isAfter5() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR;
}
}
现在创建 Prefs 文件:Prefs.kt(这是 Kotlin 类文件)
注意:您可以使用自己的首选项创建和检索代码。
包 com.test
import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
class Prefs private constructor(private val context: Context) {
private val prefranceName = "testPrefs"
companion object {
private fun getPrefs(context: Context): SharedPreferences {
return context.getSharedPreferences(context.packageName, 0)
}
fun clearDefaultShared(key: String, context: Context) {
val e = getPrefs(context).edit()
e.remove(key)
e.apply()
}
fun setString(key: String, sunDefult: String, context: Context) {
val e = getPrefs(context).edit()
e.putString(key, sunDefult)
e.apply()
}
fun getString(key: String, sunDefult: String, context: Context): String? {
return getPrefs(context).getString(key, sunDefult)
}
fun clearAll(context: Context) {
val editor = getPrefs(context).edit()
editor.clear()
editor.commit()
}
}
}
现在创建一个类 TestApp,它扩展了 Application 类。
这将用于保存应用程序重新启动时要应用的最后选择的语言。注意:不要忘记将此应用程序类名“TestApp”添加到 AndroidManifest.xml 标签中。
添加文件:测试应用程序.kt
package com.test
import android.app.Application
import com.test.LanguageUtil
import com.test.Prefs
import java.util.*
class TestApp : Application() {
override fun onCreate() {
super.onCreate()
dbAdapter = DBAdapter(applicationContext)
dbAdapter.openDataBase()
// Set Language Because Local will not be saved every time the app restarts after it is completely logged out
var languageType = Prefs.getString("languageType", "en", this@TestApp).toString()
if (languageType == "fr") {
LanguageUtil.changeLanguageType(this, Locale.FRENCH)
} else if (languageType == "es") {
LanguageUtil.changeLanguageType(this, Locale("es",""))
} else {
LanguageUtil.changeLanguageType(this, Locale.ENGLISH)
}
}
}
现在,在每个活动中,添加以下代码以应用更改后的语言。
这是科特林代码:
override fun attachBaseContext(newBase: Context) {
var languageType = Prefs.getString("languageType", "en", newBase).toString()
super.attachBaseContext(MyContextWrapper.wrap(newBase, Locale(languageType, "")))
}
当您选择不同的语言时,请使用以下代码:
LanguageUtil.changeLanguageType(this@MainActivity, Locale.ENGLISH) // for englsih
LanguageUtil.changeLanguageType(this@MainActivity, Locale("es","")) // for spanish
LanguageUtil.changeLanguageType(this@MainActivity, Locale.FRENCH)// for french
之后还将其存储在共享首选项中。
请记住:如果您更改当前活动中的语言,您将不会看到语言更改,因为您需要重新启动活动才能看到更改,但在当前活动之后打开的活动您将看到更改。
如果你在同一个activity中调用recreate(),它会闪烁屏幕,所以不要使用recreate()函数。
我有一个解决方案(不是正确的解决方案,但我可以说它是补丁并且工作完美)。
为了显示当前活动中的更改,您需要在默认 strings.xml 中创建所有英语、西班牙语和法语字符串,并在用户选择不同语言时在当前活动中将其设置为运行时。