Java Code Examples for android.content.res.Configuration#setLocale()
The following examples show how to use
android.content.res.Configuration#setLocale() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocaleManager.java From LanguageTest with MIT License | 8 votes |
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; }
Example 2
Source File: LocaleUtils.java From prayer-times-android with Apache License 2.0 | 6 votes |
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()); }
Example 3
Source File: MyApplication.java From Weather with GNU General Public License v3.0 | 6 votes |
@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()); }
Example 4
Source File: LocaleUtils.java From prayer-times-android with Apache License 2.0 | 6 votes |
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()); }
Example 5
Source File: Locales.java From focus-android with Mozilla Public License 2.0 | 6 votes |
/** * 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(); }
Example 6
Source File: AlbumUtils.java From Album with Apache License 2.0 | 5 votes |
/** * 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; } }
Example 7
Source File: VRBrowserApplication.java From FirefoxReality with Mozilla Public License 2.0 | 5 votes |
@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); }
Example 8
Source File: AppHelper.java From mvvm-template with GNU General Public License v3.0 | 5 votes |
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); }
Example 9
Source File: LanguageUtil.java From switch_language_sample with Apache License 2.0 | 5 votes |
@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); }
Example 10
Source File: LocaleHelper.java From Change-Language-AtRuntime with MIT License | 5 votes |
@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); }
Example 11
Source File: MyApplication.java From Weather with GNU General Public License v3.0 | 5 votes |
@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; } }
Example 12
Source File: LocalManageUtil.java From smart-farmer-android with Apache License 2.0 | 5 votes |
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; }
Example 13
Source File: AppUtils.java From OpenHub with GNU General Public License v3.0 | 5 votes |
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); }
Example 14
Source File: LanguageUtil.java From WanAndroid with Apache License 2.0 | 5 votes |
/** * 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); }
Example 15
Source File: LocaleUtil.java From zap-android with MIT License | 5 votes |
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; }
Example 16
Source File: LangHelper.java From AppOpsX with MIT License | 5 votes |
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); }
Example 17
Source File: LanguageUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 修改系统语言 ( 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; }
Example 18
Source File: PkgUtil.java From NanoIconPack with Apache License 2.0 | 4 votes |
@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; }
Example 19
Source File: BaseActivity.java From MusicPlayer with GNU General Public License v3.0 | 4 votes |
@TargetApi(Build.VERSION_CODES.N) private Context updateResourcesLocale(Context context, Locale locale) { Configuration configuration = context.getResources().getConfiguration(); configuration.setLocale(locale); return context.createConfigurationContext(configuration); }
Example 20
Source File: LocaleHelper.java From xmrwallet with Apache License 2.0 | 3 votes |
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); }