下面列出了android.content.res.Configuration#setLocale() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
if (Utility.isAtLeastVersion(N)) {
setLocaleForApi24(config, locale);
context = context.createConfigurationContext(config);
} else if (Utility.isAtLeastVersion(JELLY_BEAN_MR1)) {
config.setLocale(locale);
context = context.createConfigurationContext(config);
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
/**
* Get a Resources instance with the currently selected locale applied.
*/
public static Resources getLocalizedResources(Context context) {
final Resources currentResources = context.getResources();
final Locale currentLocale = LocaleManager.getInstance().getCurrentLocale(context);
@SuppressWarnings("deprecation") final Locale viewLocale = currentResources.getConfiguration().locale;
if (currentLocale == null || viewLocale == null) {
return currentResources;
}
if (currentLocale.toLanguageTag().equals(viewLocale.toLanguageTag())) {
return currentResources;
}
final Configuration configuration = new Configuration(currentResources.getConfiguration());
configuration.setLocale(currentLocale);
return context.createConfigurationContext(configuration).getResources();
}
private static void initLocale(Context c) {
Crashlytics.setString("lang", Preferences.LANGUAGE.get());
Crashlytics.setString("digits", Preferences.DIGITS.get());
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = getLocales();
LocaleList.setDefault(localeList);
config.setLocales(localeList);
} else {
Locale locale = getLocale();
config.setLocale(locale);
Locale.setDefault(locale);
}
c.getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
c.getApplicationContext().getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
}
@Override
public void onCreate() {
super.onCreate();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = preferences.getString(getString(R.string.pref_language) , "en");
locale = new Locale(lang);
config.setLocale(locale);
Log.i("Locale" , lang);
Locale.setDefault(locale);
updateConfiguration(config);
setSystemLocale(config , locale);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
LanguageUtil.setLanguage(this, new Prefs(this).getLanguage());
}
private static void initLocale(Context c) {
Crashlytics.setString("lang", Preferences.LANGUAGE.get());
Crashlytics.setString("digits", Preferences.DIGITS.get());
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = getLocales();
LocaleList.setDefault(localeList);
config.setLocales(localeList);
} else {
Locale locale = getLocale();
config.setLocale(locale);
Locale.setDefault(locale);
}
c.getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
c.getApplicationContext().getResources().updateConfiguration(config, c.getResources().getDisplayMetrics());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Context context = LocaleUtils.init(this);
Language language = LocaleUtils.getDisplayLanguage(context);
newConfig.setLocale(language.getLocale());
getApplicationContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
super.onConfigurationChanged(newConfig);
}
/**
* 修改系统语言 ( APP 多语言, 单独改变 APP 语言 )
* @param context {@link Context} - Activity
* @param locale {@link Locale}
* @return {@code true} success, {@code false} fail
*/
public static boolean applyLanguage(final Context context, final Locale locale) {
if (context != null && locale != null) {
try {
// 获取 res 资源对象
Resources resources = context.getResources();
// 获取设置对象
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// apply locale
config.setLocale(locale);
context.createConfigurationContext(config);
} else {
// updateConfiguration
// 获取屏幕参数: 主要是分辨率, 像素等
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
config.locale = locale;
// 更新语言
resources.updateConfiguration(config, displayMetrics);
}
return true;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "applyLanguage");
}
}
return false;
}
public static void updateLanguage(Context context) {
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(getLocaleByLanguage(context));
DisplayMetrics dm = resources.getDisplayMetrics();
resources.updateConfiguration(config, dm);
}
private static Context updateResources(Context context, String languageCode) {
Locale locale = new Locale(languageCode);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
res.updateConfiguration(config, res.getDisplayMetrics());
return context;
}
/**
* android7.0之后,需要set local 到 configuration
* @param context 要set的Context
* @param language 要替换的语言
* @return 一个通过configuration创建的新的Context
*/
@TargetApi(Build.VERSION_CODES.N)
private static Context createConfigurationContext(Context context, String language) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale locale = getSupportLanguage(language);
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
private static void updateResources(Context context, String language) {
Locale locale = getLocale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
context.createConfigurationContext(configuration);
}
private static Context updateResources(Context context, Locale locale) {
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
context = context.createConfigurationContext(config);
return context;
}
@SuppressWarnings("deprecation")
private static void setSystemLocale(Configuration config, Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
}
@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);
return context.createConfigurationContext(configuration);
}
@TargetApi(Build.VERSION_CODES.N)
private static Context createConfigurationResources(Context context, String language) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale locale;
if (TextUtils.isEmpty(language)) {//如果没有指定语言使用系统首选语言
locale = SupportLanguageUtil.getSystemPreferredLanguage();
} else {//指定了语言使用指定语言,没有则使用首选语言
locale = SupportLanguageUtil.getSupportLanguage(language);
}
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
private static void updateResources(Context context, String language) {
Locale locale = getLocale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
context.createConfigurationContext(configuration);
}
/**
* Setting {@link Locale} for {@link Context}.
*
* @param context to set the specified locale context.
* @param locale locale.
*/
@NonNull
public static Context applyLanguageForContext(@NonNull Context context, @NonNull Locale locale) {
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
return context.createConfigurationContext(config);
} else {
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
return context;
}
}
@TargetApi(17)
public static String getAppLabelEn(Context context, String pkgName, String def) {
if (context == null || TextUtils.isEmpty(pkgName)) {
return def;
}
String result = def;
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = packageManager.getPackageInfo(pkgName, 0).applicationInfo;
Configuration configuration = new Configuration();
// It's better, I think, to use Locale.ENGLISH
// instead of Locale.ROOT (although I want to do).
if (C.SDK >= 17) {
configuration.setLocale(Locale.ENGLISH);
} else {
configuration.locale = Locale.ENGLISH;
}
// The result is a value in disorder maybe if using:
// packageManager.getResourcesForApplication(PACKAGE_NAME)
Resources resources = packageManager.getResourcesForApplication(applicationInfo);
resources.updateConfiguration(configuration,
context.getResources().getDisplayMetrics());
int labelResId = applicationInfo.labelRes;
if (labelResId != 0) {
// If the localized label is not added, the default is returned.
// NOTICE!!!If the default were empty, Resources$NotFoundException would be called.
result = resources.getString(labelResId);
}
/*
* NOTICE!!!
* We have to restore the locale.
* On the one hand,
* it will influence the label of Activity, etc..
* On the other hand,
* the got "resources" equals the one "this.getResources()" if the current .apk file
* happens to be this APK Checker(com.by_syk.apkchecker).
* We need to restore the locale, or the language of APK Checker will change to English.
*/
if (C.SDK >= 17) {
configuration.setLocale(Locale.getDefault());
} else {
configuration.locale = Locale.getDefault();
}
resources.updateConfiguration(configuration, context.getResources().getDisplayMetrics());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@TargetApi(Build.VERSION_CODES.N)
private Context updateResourcesLocale(Context context, Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
public static Context setLocale(Context context, String locale) {
setPreferredLocale(context, locale);
Locale newLocale = (locale.isEmpty()) ? SYSTEM_DEFAULT_LOCALE : Locale.forLanguageTag(locale);
Configuration configuration = context.getResources().getConfiguration();
Locale.setDefault(newLocale);
configuration.setLocale(newLocale);
configuration.setLayoutDirection(newLocale);
return context.createConfigurationContext(configuration);
}