实际上我只需要自己做这件事。这是一种痛苦,尤其是对于较新的设备。
首先,您需要一个语言环境助手:
package za.co.overtake.onlinetrucks.utils;
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.preference.PreferenceManager;
import java.util.Locale;
/**
* This class is used to change your application locale and persist this change for the next time
* that your app is going to be used.
* <p/>
* You can also change the locale of your application on the fly by using the setLocale method.
* <p/>
* Created by gunhansancar on 07/10/15.
*/
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);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
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;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration,
resources.getDisplayMetrics());
return context;
}
}
这负责切换语言以及使用已弃用或未弃用的方法。
然后在每个活动中,您需要重写此方法(我建议您只创建一个每个活动扩展的基本活动):
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base));
}
这应该可以解决大多数情况。但是,我使用 ViewModels,由于某种原因,当我执行 getApplication.getResources().getString() 时,有时会返回错误的字符串。在这种情况下,我做了一个实用方法来解决这个问题:
public static Resources getResources(Context context) {
return LocaleHelper.onAttach(context).getResources();
}
所以现在我可以使用这个方法而不是通常的 getResources()。
这是一个真正的痛苦,但这是我可以在高于 android 8 和低于 android8 的设备上进行翻译的唯一方法。
我稍后会发布其中一些内容的链接......
编辑:此外,就像之前的答案所述,您需要重新创建更改语言的活动。
Links: https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758 https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758
https://gunhansancar.com/change-language-programmatically-in-android/ https://gunhansancar.com/change-language-programmatically-in-android/