@Override protected CharSequence computeText(String text, boolean annotateAsLink, String language) { CharSequence charSequence = super.computeText(text, annotateAsLink, language); if (!language.isEmpty() && !language.equals(mSystemLanguageTag)) { SpannableString spannable; if (charSequence instanceof SpannableString) { spannable = (SpannableString) charSequence; } else { spannable = new SpannableString(charSequence); } Locale locale = Locale.forLanguageTag(language); spannable.setSpan(new LocaleSpan(locale), 0, spannable.length(), 0); return spannable; } return charSequence; }
@Test public void locale_should_add_only_one_span() { spanBuilder.locale(range, Locale.US) .apply(); verify((SpanEZ) spanBuilder, times(1)) .addSpan(isA(TargetRange.class), isA(LocaleSpan.class)); }
/** * @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); } }); }
/** * @param text * @param locale Locale.CHINESE * @return */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public AndroidSpan drawLocaleSpan(String text, Locale locale) { LocaleSpan span = new LocaleSpan(locale); drawSpan(text, span); return this; }
/** * @param locale Locale.CHINESE * @return */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public SpanOptions addLocaleSpan(Locale locale) { LocaleSpan span = new LocaleSpan(locale); listSpan.add(span); return this; }