下面列出了android.os.LocaleList#size ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@RequiresApi(api = N)
private void setLocaleForApi24(Configuration config, Locale target) {
Set<Locale> set = new LinkedHashSet<>();
// bring the target locale to the front of the list
set.add(target);
LocaleList all = LocaleList.getDefault();
for (int i = 0; i < all.size(); i++) {
// append other locales supported by the user
set.add(all.get(i));
}
Locale[] locales = set.toArray(new Locale[0]);
config.setLocales(new LocaleList(locales));
}
/**
* 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;
}
@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;
}
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();
}
/**
* 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);
}
@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;
}
@RequiresApi(api = N)
private void setLocaleForApi24(Configuration config, Locale target) {
Set<Locale> set = new LinkedHashSet<>();
// bring the target locale to the front of the list
set.add(target);
LocaleList all = LocaleList.getDefault();
for (int i = 0; i < all.size(); i++) {
// append other locales supported by the user
set.add(all.get(i));
}
Locale[] locales = set.toArray(new Locale[0]);
config.setLocales(new LocaleList(locales));
}
/**
* 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);
}
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;
}
/**
* 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;
}
@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;
}
/**
*
* @hide
*/
public static String localesToResourceQualifier(LocaleList locs) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < locs.size(); i++) {
final Locale loc = locs.get(i);
final int l = loc.getLanguage().length();
if (l == 0) {
continue;
}
final int s = loc.getScript().length();
final int c = loc.getCountry().length();
final int v = loc.getVariant().length();
// We ignore locale extensions, since they are not supported by AAPT
if (sb.length() != 0) {
sb.append(",");
}
if (l == 2 && s == 0 && (c == 0 || c == 2) && v == 0) {
// Traditional locale format: xx or xx-rYY
sb.append(loc.getLanguage());
if (c == 2) {
sb.append("-r").append(loc.getCountry());
}
} else {
sb.append("b+");
sb.append(loc.getLanguage());
if (s != 0) {
sb.append("+");
sb.append(loc.getScript());
}
if (c != 0) {
sb.append("+");
sb.append(loc.getCountry());
}
if (v != 0) {
sb.append("+");
sb.append(loc.getVariant());
}
}
}
return sb.toString();
}
/**
* Logs the type, position and args of spans which attach to given text, but only if log priority
* is equal to Log.VERBOSE. Format is {type 'spanned text' extra-data} {type 'other text'
* extra-data} ..."
*
* @param text Text to be logged
*/
public static String spansToStringForLogging(CharSequence text) {
if (!LogUtils.shouldLog(Log.VERBOSE)) {
return null;
}
if (isEmptyOrNotSpannableStringType(text)) {
return null;
}
Spanned spanned = (Spanned) text;
ParcelableSpan[] spans = spanned.getSpans(0, text.length(), ParcelableSpan.class);
if (spans.length == 0) {
return null;
}
StringBuilder stringBuilder = new StringBuilder();
for (ParcelableSpan span : spans) {
stringBuilder.append("{");
// Span type.
stringBuilder.append(span.getClass().getSimpleName());
// Span text.
int start = spanned.getSpanStart(span);
int end = spanned.getSpanEnd(span);
if (start < 0 || end < 0 || start == end) {
stringBuilder.append(" invalid index:[");
stringBuilder.append(start);
stringBuilder.append(",");
stringBuilder.append(end);
stringBuilder.append("]}");
continue;
} else {
stringBuilder.append(" '");
stringBuilder.append(spanned, start, end);
stringBuilder.append("'");
}
// Extra data.
if (span instanceof LocaleSpan) {
LocaleSpan localeSpan = (LocaleSpan) span;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
Locale locale = localeSpan.getLocale();
if (locale != null) {
stringBuilder.append(" locale=");
stringBuilder.append(locale);
}
} else {
LocaleList localeList = localeSpan.getLocales();
int size = localeList.size();
if (size > 0) {
stringBuilder.append(" locale=[");
for (int i = 0; i < size - 1; i++) {
stringBuilder.append(localeList.get(i));
stringBuilder.append(",");
}
stringBuilder.append(localeList.get(size - 1));
stringBuilder.append("]");
}
}
} else if (span instanceof TtsSpan) {
TtsSpan ttsSpan = (TtsSpan) span;
stringBuilder.append(" ttsType=");
stringBuilder.append(ttsSpan.getType());
PersistableBundle bundle = ttsSpan.getArgs();
Set<String> keys = bundle.keySet();
if (!keys.isEmpty()) {
for (String key : keys) {
stringBuilder.append(" ");
stringBuilder.append(key);
stringBuilder.append("=");
stringBuilder.append(bundle.get(key));
}
}
} else if (span instanceof URLSpan) {
URLSpan urlSpan = (URLSpan) span;
stringBuilder.append(" url=");
stringBuilder.append(urlSpan.getURL());
}
stringBuilder.append("}");
}
return stringBuilder.toString();
}