android.os.LocaleList Java Examples

The following examples show how to use android.os.LocaleList. 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: PictureLangUtils.java    From PicturePicker with Apache License 2.0 6 votes vote down vote up
public static Context setLanguage(Context context, Locale locale) {

        Resources resources = context.getResources();
        Configuration config = resources.getConfiguration();
        config.setLocale(locale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            return context.createConfigurationContext(config);
        } else {
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            context.getResources().updateConfiguration(config, displayMetrics);
            return context;
        }
    }
 
Example #2
Source File: EditorInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Used to package this object into a {@link Parcel}.
 *
 * @param dest The {@link Parcel} to be written.
 * @param flags The flags used for parceling.
 */
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(inputType);
    dest.writeInt(imeOptions);
    dest.writeString(privateImeOptions);
    TextUtils.writeToParcel(actionLabel, dest, flags);
    dest.writeInt(actionId);
    dest.writeInt(initialSelStart);
    dest.writeInt(initialSelEnd);
    dest.writeInt(initialCapsMode);
    TextUtils.writeToParcel(hintText, dest, flags);
    TextUtils.writeToParcel(label, dest, flags);
    dest.writeString(packageName);
    dest.writeInt(fieldId);
    dest.writeString(fieldName);
    dest.writeBundle(extras);
    if (hintLocales != null) {
        hintLocales.writeToParcel(dest, flags);
    } else {
        LocaleList.getEmptyLocaleList().writeToParcel(dest, flags);
    }
    dest.writeStringArray(contentMimeTypes);
}
 
Example #3
Source File: LauncherApplication.java    From SoloPi with Apache License 2.0 6 votes vote down vote up
/**
 * 设置应用默认语言
 */
public void setApplicationLanguage() {
    Resources resources = getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getLanguageLocale();
    config.locale = locale;
    Locale.setDefault(locale);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        getApplicationContext().createConfigurationContext(config);
    }
    resources.updateConfiguration(config, dm);
}
 
Example #4
Source File: KeyboardWidget.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
private void setDefaultKeyboard() {
    KeyboardInterface keyboard = getKeyboardForLocale(SettingsStore.getInstance(getContext()).getKeyboardLocale());
    if (keyboard != null) {
        handleLanguageChange(keyboard);
        return;
    }

    // If the user has not selected any keyboard, find the best match from system locales.
    LocaleList localeList = getResources().getConfiguration().getLocales();
    String[] supportedLocales = new String[mKeyboards.size()];
    for (int i = 0; i < mKeyboards.size(); ++i) {
        supportedLocales[i] = mKeyboards.get(i).getLocale().toLanguageTag();
    }
    Locale bestMatch = localeList.getFirstMatch(supportedLocales);
    keyboard = getKeyboardForLocale(bestMatch);
    if (keyboard == null) {
        // Fall back to english.
        keyboard = getKeyboardForLocale(Locale.ENGLISH);
    }
    handleLanguageChange(keyboard);
}
 
Example #5
Source File: QuickNoteContextWrapper.java    From QuickNote with Apache License 2.0 6 votes vote down vote up
public static Context wrap(Context context, Locale locale) {
    Context newContext = context;
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//8.0及以上系统
        config.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        newContext = context.createConfigurationContext(config);//对于8.0系统必须先调用该语句,否则后面的updateConfiguration不起作用
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4及以上系统
        config.setLocale(locale);
        newContext = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
    }
    return new QuickNoteContextWrapper(newContext);
}
 
Example #6
Source File: LocaleHelper.java    From candybar-library with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #7
Source File: EditorInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public EditorInfo createFromParcel(Parcel source) {
    EditorInfo res = new EditorInfo();
    res.inputType = source.readInt();
    res.imeOptions = source.readInt();
    res.privateImeOptions = source.readString();
    res.actionLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    res.actionId = source.readInt();
    res.initialSelStart = source.readInt();
    res.initialSelEnd = source.readInt();
    res.initialCapsMode = source.readInt();
    res.hintText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    res.label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    res.packageName = source.readString();
    res.fieldId = source.readInt();
    res.fieldName = source.readString();
    res.extras = source.readBundle();
    LocaleList hintLocales = LocaleList.CREATOR.createFromParcel(source);
    res.hintLocales = hintLocales.isEmpty() ? null : hintLocales;
    res.contentMimeTypes = source.readStringArray();
    return res;
}
 
Example #8
Source File: BaseActivity.java    From KernelAdiutor with GNU General Public License v3.0 6 votes vote down vote up
public static ContextWrapper wrap(Context context, Locale newLocale) {

        Resources res = context.getResources();
        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);
    }
 
Example #9
Source File: RNI18nModule.java    From imsdk-android with MIT License 6 votes vote down vote up
private WritableArray getLocaleList() {
  WritableArray array = Arguments.createArray();

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    LocaleList locales = getReactApplicationContext()
        .getResources().getConfiguration().getLocales();

    for (int i = 0; i < locales.size(); i++) {
      array.pushString(this.toLanguageTag(locales.get(i)));
    }
  } else {
    array.pushString(this.toLanguageTag(getReactApplicationContext()
        .getResources().getConfiguration().locale));
  }

  return array;
}
 
Example #10
Source File: ContextLocalWrapper.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
public static ContextLocalWrapper wrap(Context context, Locale newLocale) {
  Resources res = context.getResources();
  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 ContextLocalWrapper(context);
}
 
Example #11
Source File: TextClassifierImpl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private TextClassifierImplNative getNative(LocaleList localeList)
        throws FileNotFoundException {
    synchronized (mLock) {
        localeList = localeList == null ? LocaleList.getEmptyLocaleList() : localeList;
        final ModelFile bestModel = findBestModelLocked(localeList);
        if (bestModel == null) {
            throw new FileNotFoundException("No model for " + localeList.toLanguageTags());
        }
        if (mNative == null || mNative.isClosed() || !Objects.equals(mModel, bestModel)) {
            Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel);
            final ParcelFileDescriptor fd = ParcelFileDescriptor.open(
                    new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
            mNative = new TextClassifierImplNative(fd.getFd());
            mNativeCleaner = Cleaner.create(this, new NativeCloser(mNative));
            closeAndLogError(fd);
            mModel = bestModel;
        }
        return mNative;
    }
}
 
Example #12
Source File: LocaleUtils.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
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 #13
Source File: LocalManageUtil.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 设置语言类型
 */
public static void setApplicationLanguage(Context context) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getSetLanguageLocale(context);
    config.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}
 
Example #14
Source File: CommonUtil.java    From QuickNote with Apache License 2.0 6 votes vote down vote up
public static void changeLocalLanguage(Context context, Locale locale) {
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//8.0及以上系统
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocale(locale);
        config.setLocales(localeList);
        context.createConfigurationContext(config);//对于8.0系统必须先调用该语句,否则后面的updateConfiguration不起作用
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4及以上系统
        config.setLocale(locale);
    } else {
        config.locale = locale;
    }
    resources.updateConfiguration(config, dm);//所有的系统都需要调用该API
}
 
Example #15
Source File: AppLocale.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example #16
Source File: LocaleContextWrapper.java    From px-android with MIT License 6 votes vote down vote up
@SuppressWarnings("PMD.AvoidReassigningParameters")
@NonNull
private static Context applyNewLocaleConfig(@NonNull Context context, @NonNull final Locale newLocale) {
    final Configuration newLocaleConfig = context.getResources().getConfiguration();
    final Resources res = context.getResources();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        final LocaleList localeList = new LocaleList(newLocale);
        newLocaleConfig.setLocales(localeList);
        newLocaleConfig.setLocale(newLocale);
        context = context.createConfigurationContext(newLocaleConfig);
    } else {
        newLocaleConfig.locale = newLocale;
    }

    Locale.setDefault(newLocale);
    res.updateConfiguration(newLocaleConfig, res.getDisplayMetrics());
    return context;
}
 
Example #17
Source File: LanguageUtil.java    From FimiX8-RE with MIT License 6 votes vote down vote up
@RequiresApi(api = 24)
public static void changeAppLanguage(Context context, Locale setLocale) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getLocaleByLanguage(setLocale);
    config.locale = locale;
    if (VERSION.SDK_INT >= 24) {
        LocaleList localeList = new LocaleList(new Locale[]{locale});
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}
 
Example #18
Source File: LocaleUtils.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
public static Context wrapContext(Context context) {
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

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

    } else {
        configuration.setLocale(getLocale());
        context = context.createConfigurationContext(configuration);

    }

    return new ContextWrapper(context);
}
 
Example #19
Source File: LocalManageUtil.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
public static void saveSystemCurrentLanguage(Context context) {
    Locale locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = LocaleList.getDefault().get(0);
    } else {
        locale = Locale.getDefault();
    }
    Log.d(TAG, locale.getLanguage());
    SPUtil.getInstance(context).setSystemCurrentLocal(locale);
    String systemLanguage = SPUtils.getString(context, SPKeyGlobal.SYSTEM_LANGUAGE, "");
    if (!TextUtils.isEmpty(systemLanguage) && !TextUtils.equals(locale.getCountry(), systemLanguage)) {
        ActivityContainer.finishAllActivity();
    }
    SPUtils.putString(context, SPKeyGlobal.SYSTEM_LANGUAGE, locale.getCountry());
}
 
Example #20
Source File: LocaleUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @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();
}
 
Example #21
Source File: CompatUtils.java    From enterprise-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the primary locale from the specified {@link Configuration}.
 *
 * @param configuration The current {@link Configuration}.
 * @return The primary locale.
 */
public static Locale getPrimaryLocale(Configuration configuration) {
    if (Build.VERSION.SDK_INT >= 24) {
        final LocaleList locales = configuration.getLocales();
        if (locales.size() > 0) {
            return locales.get(0);
        }
    }
    //noinspection deprecation
    return configuration.locale;
}
 
Example #22
Source File: FormatUtils.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
private static Locale chooseAvailableLocale(@NonNull LocaleList localeList) {
    for (int i = 0; i < localeList.size(); i++) {
        final Locale locale = localeList.get(i);
        if (sEditActionTextFormats.containsKey(locale)) {
            return locale;
        }
        for (Locale loc : sEditActionTextFormats.keySet()) {
            if (loc.getLanguage().equals(locale.getLanguage())) {
                return loc;
            }
        }
    }
    return Locale.ENGLISH;
}
 
Example #23
Source File: LocaleManager.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #24
Source File: SystemLocaleRetriever.java    From LocaleChanger with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #25
Source File: LocalManageUtil.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void saveSystemCurrentLanguage(Context context) {
    Locale locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = LocaleList.getDefault().get(0);
    } else {
        locale = Locale.getDefault();
    }
    SPUtil.getInstance(context).setSystemCurrentLocal(locale);
}
 
Example #26
Source File: Spans.java    From spanner with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
        }
    });
}
 
Example #27
Source File: Configuration.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public void readFromParcel(Parcel source) {
    fontScale = source.readFloat();
    mcc = source.readInt();
    mnc = source.readInt();

    mLocaleList = source.readParcelable(LocaleList.class.getClassLoader());
    locale = mLocaleList.get(0);

    userSetLocale = (source.readInt()==1);
    touchscreen = source.readInt();
    keyboard = source.readInt();
    keyboardHidden = source.readInt();
    hardKeyboardHidden = source.readInt();
    navigation = source.readInt();
    navigationHidden = source.readInt();
    orientation = source.readInt();
    screenLayout = source.readInt();
    colorMode = source.readInt();
    uiMode = source.readInt();
    screenWidthDp = source.readInt();
    screenHeightDp = source.readInt();
    smallestScreenWidthDp = source.readInt();
    densityDpi = source.readInt();
    compatScreenWidthDp = source.readInt();
    compatScreenHeightDp = source.readInt();
    compatSmallestScreenWidthDp = source.readInt();
    windowConfiguration.setTo((WindowConfiguration) source.readValue(null));
    assetsSeq = source.readInt();
    seq = source.readInt();
}
 
Example #28
Source File: LocaleUtils.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private static String getCurrentCountry()
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    {
        return LocaleList.getDefault().get(0).getCountry();
    }
    else
    {
        return Locale.getDefault().getCountry();
    }
}
 
Example #29
Source File: Singleton.java    From android-galaxyzoo with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #30
Source File: KeyboardLayout.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private KeyboardLayout(Parcel source) {
    mDescriptor = source.readString();
    mLabel = source.readString();
    mCollection = source.readString();
    mPriority = source.readInt();
    mLocales = LocaleList.CREATOR.createFromParcel(source);
    mVendorId = source.readInt();
    mProductId = source.readInt();
}