android.content.res.Configuration#setLocale()源码实例Demo

下面列出了android.content.res.Configuration#setLocale() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: LanguageTest   文件: LocaleManager.java
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;
}
 
源代码2 项目: focus-android   文件: Locales.java
/**
 * 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();
}
 
源代码3 项目: prayer-times-android   文件: LocaleUtils.java
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());
}
 
源代码4 项目: Weather   文件: MyApplication.java
@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());
}
 
源代码5 项目: prayer-times-android   文件: LocaleUtils.java
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());
}
 
源代码6 项目: FirefoxReality   文件: VRBrowserApplication.java
@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);
}
 
源代码7 项目: DevUtils   文件: LanguageUtils.java
/**
 * 修改系统语言 ( 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;
}
 
源代码8 项目: AppOpsX   文件: LangHelper.java
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);
  }
 
源代码9 项目: zap-android   文件: LocaleUtil.java
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;
}
 
源代码10 项目: WanAndroid   文件: LanguageUtil.java
/**
 * 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);
}
 
源代码11 项目: OpenHub   文件: AppUtils.java
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);
}
 
源代码12 项目: smart-farmer-android   文件: LocalManageUtil.java
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;
}
 
源代码13 项目: Weather   文件: MyApplication.java
@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;
    }
}
 
源代码14 项目: Change-Language-AtRuntime   文件: LocaleHelper.java
@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);
}
 
源代码15 项目: switch_language_sample   文件: LanguageUtil.java
@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);
}
 
源代码16 项目: mvvm-template   文件: AppHelper.java
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);
}
 
源代码17 项目: Album   文件: AlbumUtils.java
/**
 * 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;
    }
}
 
源代码18 项目: NanoIconPack   文件: PkgUtil.java
@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;
}
 
源代码19 项目: MusicPlayer   文件: BaseActivity.java
@TargetApi(Build.VERSION_CODES.N)
private Context updateResourcesLocale(Context context, Locale locale) {
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    return context.createConfigurationContext(configuration);
}
 
源代码20 项目: xmrwallet   文件: LocaleHelper.java
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);
}