Java 类android.os.LocaleList 实例源码

项目:inventum    文件:InventumContextWrapper.java   
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String lang = prefs.getString(Constants.PREF_APP_LANGUAGE, "");
    Locale locale = StringUtils.getLocale(lang);
    locale = (locale == null) ? Locale.getDefault() : locale;
    Configuration configuration = context.getResources().getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        Locale.setDefault(locale);
        configuration.setLocale(locale);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
        context = context.createConfigurationContext(configuration);
    } else {
        configuration.locale = locale;
        context.getResources().updateConfiguration(configuration, null);
    }

    return new InventumContextWrapper(context);
}
项目:microMathematics    文件:AppLocale.java   
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, Locale newLocale)
{
    final Resources res = context.getResources();
    final Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    {
        configuration.setLocale(newLocale);
        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);
    }
    else
    {
        configuration.locale = newLocale;
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }
    return new ContextWrapper(context);
}
项目:wallpaperboard    文件:LocaleHelper.java   
public static void setLocale(@NonNull Context context) {
    Locale locale = Preferences.get(context).getCurrentLocale();
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList.setDefault(new LocaleList(locale));
        configuration.setLocales(new LocaleList(locale));
        configuration.setLocale(locale);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
    } else {
        configuration.locale = locale;
    }

    //Todo:
    // Find out a solution to use context.createConfigurationContext(configuration);
    // It breaks onConfigurationChanged()
    // Still can't find a way to fix that
    // No other options, better use deprecated code for now
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
项目:Show_Chat    文件:ShowChatContextWrapper.java   
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String lang = prefs.getString(Constants.PREF_APP_LANGUAGE, "");
    Locale locale = StringUtils.getLocale(lang);
    locale = (locale == null) ? Locale.getDefault() : locale;
    Configuration configuration = context.getResources().getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        Locale.setDefault(locale);
        configuration.setLocale(locale);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
        context = context.createConfigurationContext(configuration);
    } else {
        configuration.locale = locale;
        context.getResources().updateConfiguration(configuration, null);
    }

    return new ShowChatContextWrapper(context);
}
项目:candybar-library    文件:LocaleHelper.java   
public static void setLocale(@NonNull Context context) {
    Locale locale = Preferences.get(context).getCurrentLocale();
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList.setDefault(new LocaleList(locale));
        configuration.setLocales(new LocaleList(locale));
        configuration.setLocale(locale);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
    } else {
        configuration.locale = locale;
    }

    //Todo:
    // Find out a solution to use context.createConfigurationContext(configuration);
    // It breaks onConfigurationChanged()
    // Still can't find a way to fix that
    // No other options, better use deprecated code for now
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
项目:isu    文件:CustomContextWrapper.java   
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {

    Locale newLocale;
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Tools.getBoolean("forceenglish", false, context))
        newLocale = new Locale("en_US");
    else
        newLocale = new Locale(Tools.sysLocale());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);
        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);
    } else { //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);
    }

    return new ContextWrapper(context);
}
项目:SecretBrand    文件:LocaleUtils.java   
public static List<Locale> getDefault(Context context, boolean getAllIfPossible) {
    List<Locale> list = new ArrayList();
    if (VERSION.SDK_INT >= 24) {
        LocaleList locales = context.getResources().getConfiguration().getLocales();
        for (int i = 0; i < locales.size(); i++) {
            Locale locale = locales.get(i);
            if (!getAllIfPossible) {
                list.add(locale);
                break;
            }
            list.add(locale);
        }
    } else {
        list.add(context.getResources().getConfiguration().locale);
    }
    return list;
}
项目:MediaMonkey    文件:PluginPkgVo.java   
CharSequence getDateAsText() {
    if (isDirectory) {
        return "";
    }

    Locale currentLocale;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        @SuppressWarnings("deprecation")
        Locale locale = MediaMonkeyApplication.getInstance().getResources().getConfiguration().locale;
        currentLocale = locale;
    } else {
        Resources res = MediaMonkeyApplication.getInstance().getResources();
        LocaleList locales = res.getConfiguration().getLocales();
        currentLocale = locales.getFirstMatch(res.getAssets().getLocales());
    }

    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, currentLocale);
    return format.format(new Date(file.lastModified()));
}
项目:DietDiaryApp    文件:ResourcesHelper.java   
@NonNull
public static Resources getResources(@NonNull final Context context, @NonNull final Locale locale) {
    Resources standardResources = context.getResources();

    AssetManager assets = standardResources.getAssets();
    DisplayMetrics metrics = standardResources.getDisplayMetrics();
    Configuration config = new Configuration(standardResources.getConfiguration());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocales(LocaleList.forLanguageTags(Locale.ENGLISH.toLanguageTag()));
    } else {
        //noinspection deprecation
        config.locale = locale;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return context.createConfigurationContext(config).getResources();
    } else {
        //noinspection deprecation
        return new Resources(assets, metrics, config);
    }
}
项目:KA27    文件:CustomContextWrapper.java   
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {

    Locale newLocale;
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Utils.getBoolean("forceenglish", false, context))
        newLocale = new Locale("en_US");
    else
        newLocale = new Locale(Utils.sysLocale());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);
        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);
    } else { //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);
    }

    return new ContextWrapper(context);
}
项目:Android-LocalizationActivity    文件:LocalizationUtility.java   
public static Context applyLocalizationContext(Context baseContext) {
    Locale baseLocale = LocalizationUtility.getLocaleFromConfiguration(baseContext.getResources().getConfiguration());
    Locale currentLocale = LanguageSetting.getLanguage(baseContext);
    if (!baseLocale.toString().equalsIgnoreCase(currentLocale.toString())) {
        LocalizationContext context = new LocalizationContext(baseContext);
        Configuration config = context.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocale(currentLocale);
            LocaleList localeList = new LocaleList(currentLocale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            return context.createConfigurationContext(config);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(currentLocale);
            return context.createConfigurationContext(config);
        } else {
            return context;
        }
    } else {
        return baseContext;
    }
}
项目:Android-LocalizationActivity    文件:LocalizationActivityDelegate.java   
public Context attachBaseContext(Context context) {
    Locale locale = LanguageSetting.getLanguage(context);
    Configuration config = context.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        return context.createConfigurationContext(config);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
        return context.createConfigurationContext(config);
    } else {
        return context;
    }
}
项目:LaunchEnr    文件:AlphabeticIndexCompat.java   
AlphabeticIndexVN(Context context) {
    LocaleList locales = context.getResources().getConfiguration().getLocales();
    int localeCount = locales.size();

    Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH : locales.get(0);
    AlphabeticIndex indexBuilder = new AlphabeticIndex(primaryLocale);
    for (int i = 1; i < localeCount; i++) {
        indexBuilder.addLabels(locales.get(i));
    }
    indexBuilder.addLabels(Locale.ENGLISH);

    mAlphabeticIndex = indexBuilder.buildImmutableIndex();
}
项目:olami-android-client-sdk    文件:MainActivity.java   
protected String getSystemLanguage() {
    Locale locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = LocaleList.getDefault().get(0);
    } else {
        locale = Locale.getDefault();
    }

    String language = locale.getLanguage() + "-" + locale.getCountry();
    return language;
}
项目:LocaleChanger    文件:SystemLocaleRetriever.java   
public static List<Locale> retrieve() {
    List<Locale> systemLocales;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        systemLocales = mapToListOfLocales(LocaleList.getDefault());
    } else {
        systemLocales = Collections.singletonList(Locale.getDefault());
    }
    return systemLocales;
}
项目:LocaleChanger    文件:SystemLocaleRetriever.java   
@RequiresApi(api = Build.VERSION_CODES.N)
private static List<Locale> mapToListOfLocales(LocaleList localeList) {
    List<Locale> locales = new ArrayList<>();
    for (int i = 0; i < localeList.size(); i++) {
        locales.add(localeList.get(i));
    }
    return locales;
}
项目:spanner    文件:Spans.java   
/**
 * @see android.text.style.LocaleSpan#LocaleSpan(LocaleList)
 */
@RequiresApi(api = Build.VERSION_CODES.N)
public static Span locale(@NonNull final LocaleList localeList) {
    return new Span(new SpanBuilder() {
        @Override
        public Object build() {
            return new LocaleSpan(localeList);
        }
    });
}
项目:FlickLauncher    文件:AlphabeticIndexCompat.java   
public AlphabeticIndexVN(Context context) {
    LocaleList locales = context.getResources().getConfiguration().getLocales();
    int localeCount = locales.size();

    Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH : locales.get(0);
    AlphabeticIndex indexBuilder = new AlphabeticIndex(primaryLocale);
    for (int i = 1; i < localeCount; i++) {
        indexBuilder.addLabels(locales.get(i));
    }
    indexBuilder.addLabels(Locale.ENGLISH);

    mAlphabeticIndex = indexBuilder.buildImmutableIndex();
}
项目:ModuleFrame    文件:LocaleUtil.java   
/**
 * 获取真实系统首选语言
 *
 * @return 语言
 */
public static Locale getLocale() {
    Locale locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = LocaleList.getDefault().get(0);
    } else {
        locale = Locale.getDefault();
    }
    return locale;
}
项目:SimpleUILauncher    文件:AlphabeticIndexCompat.java   
public AlphabeticIndexVN(Context context) {
    LocaleList locales = context.getResources().getConfiguration().getLocales();
    int localeCount = locales.size();

    Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH : locales.get(0);
    AlphabeticIndex indexBuilder = new AlphabeticIndex(primaryLocale);
    for (int i = 1; i < localeCount; i++) {
        indexBuilder.addLabels(locales.get(i));
    }
    indexBuilder.addLabels(Locale.ENGLISH);

    mAlphabeticIndex = indexBuilder.buildImmutableIndex();
}
项目:AppOpsX    文件:LangHelper.java   
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
  Resources resources = context.getResources();
  Locale locale = getLocaleByLanguage(context);

  Configuration configuration = resources.getConfiguration();
  configuration.setLocale(locale);
  configuration.setLocales(new LocaleList(locale));
  return context.createConfigurationContext(configuration);
}
项目:365browser    文件:LocaleUtils.java   
/**
 * Converts LocaleList object to the comma separated BCP 47 compliant string format.
 *
 * @return a well-formed IETF BCP 47 language tag with language and country code that
 *         represents this locale list.
 */
@TargetApi(Build.VERSION_CODES.N)
public static String toLanguageTags(LocaleList localeList) {
    ArrayList<String> newLocaleList = new ArrayList<>();
    for (int i = 0; i < localeList.size(); i++) {
        Locale locale = getUpdatedLocaleForChromium(localeList.get(i));
        newLocaleList.add(toLanguageTag(locale));
    }
    return TextUtils.join(",", newLocaleList);
}
项目:365browser    文件:LocaleUtils.java   
/**
 * @return a comma separated language tags string that represents a default locale or locales.
 *         Each language tag is well-formed IETF BCP 47 language tag with language and country
 *         code.
 */
public static String getDefaultLocaleListString() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return toLanguageTags(LocaleList.getDefault());
    }
    return getDefaultLocaleString();
}
项目:365browser    文件:DateTimePickerDialog.java   
private Locale getLocale() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList locales = getConfiguration().getLocales();
        if (locales.size() > 0) {
            return locales.get(0);
        }
    }
    return getConfiguration().locale;
}
项目:Dashchan    文件:LocaleManager.java   
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N)
public void apply(Activity activity) {
    if (previousLocales != null && !previousLocales.isEmpty()) {
        Resources resources = activity.getResources();
        Configuration configuration = resources.getConfiguration();
        if (C.API_NOUGAT) {
            configuration.setLocales(new LocaleList(CommonUtils.toArray(previousLocales, Locale.class)));
        } else {
            configuration.locale = previousLocales.get(0);
        }
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }
}
项目:Dashchan    文件:LocaleManager.java   
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N)
private ArrayList<Locale> list(Configuration configuration) {
    ArrayList<Locale> arrayList = new ArrayList<>();
    if (C.API_NOUGAT) {
        LocaleList localeList = configuration.getLocales();
        for (int i = 0; i < localeList.size(); i++) {
            arrayList.add(localeList.get(i));
        }
    } else {
        arrayList.add(configuration.locale);
    }
    return arrayList;
}
项目:android-galaxyzoo    文件:Singleton.java   
private static LocaleDetails getLocaleDetails(final Context context) {
    final Configuration config = context.getResources().getConfiguration();
    if (config == null) {
        return null;
    }

    Locale locale = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        final LocaleList locales = config.getLocales();
        if (locales == null || locales.isEmpty()) {
            return null;
        }

        locale = locales.get(0);
    } else {
        //noinspection deprecation
        locale = config.locale;
    }

    if (locale == null) {
        return null;
    }

    final LocaleDetails result = new LocaleDetails();
    result.language = locale.getLanguage();

    //The Galaxy zoo files, such as ch_cn.json are lowercase, instead of having the
    //country code in uppercase, such as ch_CN, like normal system locales.
    final String country = locale.getCountry();
    if (!TextUtils.isEmpty(country)) {
        result.countryCode = country.toLowerCase(new Locale(Utils.STRING_LANGUAGE));
    }

    return result;
}
项目:CanvasScript    文件:PaintScript.java   
@TargetApi(Build.VERSION_CODES.N)
public PaintScript textLocales(@NonNull @Size(min = 1) LocaleList locales) {
    paint.setTextLocales(locales);
    return this;
}
项目:CanvasScript    文件:CanvasScript.java   
@TargetApi(Build.VERSION_CODES.N)
public CanvasScript textLocales(@NonNull @Size(min = 1) LocaleList locales) {
    createPaintIfNull();
    currentPaint.setTextLocales(locales);
    return this;
}
项目:material-components-android    文件:ViewStructureImpl.java   
@Override
public void setLocaleList(LocaleList localeList) {}