Java Code Examples for android.icu.text.DisplayContext#CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE

The following examples show how to use android.icu.text.DisplayContext#CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE . 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: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private String adjustForUsageAndContext(CapitalizationContextUsage usage, String name) {
    if (name != null && name.length() > 0 && UCharacter.isLowerCase(name.codePointAt(0)) &&
            (capitalization==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
            (capitalizationUsage != null && capitalizationUsage[usage.ordinal()]) )) {
        // Note, won't have capitalizationUsage != null && capitalizationUsage[usage.ordinal()]
        // unless capitalization is CAPITALIZATION_FOR_UI_LIST_OR_MENU or CAPITALIZATION_FOR_STANDALONE
        synchronized (this) {
            if (capitalizationBrkIter == null) {
                // should only happen when deserializing, etc.
                capitalizationBrkIter = BreakIterator.getSentenceInstance(locale);
            }
            return UCharacter.toTitleCase(locale, name, capitalizationBrkIter,
                    UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
        }
    }
    return name;
}
 
Example 2
Source File: RelativeDateFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public void setContext(DisplayContext context) {
    super.setContext(context);
    if (!capitalizationInfoIsSet &&
          (context==DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU || context==DisplayContext.CAPITALIZATION_FOR_STANDALONE)) {
        initCapitalizationContextInfo(fLocale);
        capitalizationInfoIsSet = true;
    }
    if (capitalizationBrkIter == null && (context==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
          (context==DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU && capitalizationOfRelativeUnitsForListOrMenu) ||
          (context==DisplayContext.CAPITALIZATION_FOR_STANDALONE && capitalizationOfRelativeUnitsForStandAlone) )) {
        capitalizationBrkIter = BreakIterator.getSentenceInstance(fLocale);
    }
}
 
Example 3
Source File: InputMethodSubtype.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a display name for this subtype.
 *
 * <p>If {@code subtypeNameResId} is specified (!= 0) text generated from that resource will
 * be returned. The localized string resource of the label should be capitalized for inclusion
 * in UI lists. The string resource may contain at most one {@code %s}. If present, the
 * {@code %s} will be replaced with the display name of the subtype locale in the user's locale.
 *
 * <p>If {@code subtypeNameResId} is not specified (== 0) the framework returns the display name
 * of the subtype locale, as capitalized for use in UI lists, in the user's locale.
 *
 * @param context {@link Context} will be used for getting {@link Locale} and
 * {@link android.content.pm.PackageManager}.
 * @param packageName The package name of the input method.
 * @param appInfo The {@link ApplicationInfo} of the input method.
 * @return a display name for this subtype.
 */
@NonNull
public CharSequence getDisplayName(
        Context context, String packageName, ApplicationInfo appInfo) {
    if (mSubtypeNameResId == 0) {
        return getLocaleDisplayName(getLocaleFromContext(context), getLocaleObject(),
                DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
    }

    final CharSequence subtypeName = context.getPackageManager().getText(
            packageName, mSubtypeNameResId, appInfo);
    if (TextUtils.isEmpty(subtypeName)) {
        return "";
    }
    final String subtypeNameString = subtypeName.toString();
    String replacementString;
    if (containsExtraValueKey(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)) {
        replacementString = getExtraValueOf(
                EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME);
    } else {
        final DisplayContext displayContext;
        if (TextUtils.equals(subtypeNameString, "%s")) {
            displayContext = DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU;
        } else if (subtypeNameString.startsWith("%s")) {
            displayContext = DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE;
        } else {
            displayContext = DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE;
        }
        replacementString = getLocaleDisplayName(getLocaleFromContext(context),
                getLocaleObject(), displayContext);
    }
    if (replacementString == null) {
        replacementString = "";
    }
    try {
        return String.format(subtypeNameString, replacementString);
    } catch (IllegalFormatException e) {
        Slog.w(TAG, "Found illegal format in subtype name("+ subtypeName + "): " + e);
        return "";
    }
}
 
Example 4
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public LocaleDisplayNamesImpl(ULocale locale, DisplayContext... contexts) {
    DialectHandling dialectHandling = DialectHandling.STANDARD_NAMES;
    DisplayContext capitalization = DisplayContext.CAPITALIZATION_NONE;
    DisplayContext nameLength = DisplayContext.LENGTH_FULL;
    DisplayContext substituteHandling = DisplayContext.SUBSTITUTE;
    for (DisplayContext contextItem : contexts) {
        switch (contextItem.type()) {
        case DIALECT_HANDLING:
            dialectHandling = (contextItem.value()==DisplayContext.STANDARD_NAMES.value())?
                    DialectHandling.STANDARD_NAMES: DialectHandling.DIALECT_NAMES;
            break;
        case CAPITALIZATION:
            capitalization = contextItem;
            break;
        case DISPLAY_LENGTH:
            nameLength = contextItem;
            break;
        case SUBSTITUTE_HANDLING:
            substituteHandling = contextItem;
            break;
        default:
            break;
        }
    }

    this.dialectHandling = dialectHandling;
    this.capitalization = capitalization;
    this.nameLength = nameLength;
    this.substituteHandling = substituteHandling;
    this.langData = LangDataTables.impl.get(locale, substituteHandling == DisplayContext.NO_SUBSTITUTE);
    this.regionData = RegionDataTables.impl.get(locale, substituteHandling == DisplayContext.NO_SUBSTITUTE);
    this.locale = ULocale.ROOT.equals(langData.getLocale()) ? regionData.getLocale() :
        langData.getLocale();

    // Note, by going through DataTable, this uses table lookup rather than straight lookup.
    // That should get us the same data, I think.  This way we don't have to explicitly
    // load the bundle again.  Using direct lookup didn't seem to make an appreciable
    // difference in performance.
    String sep = langData.get("localeDisplayPattern", "separator");
    if (sep == null || "separator".equals(sep)) {
        sep = "{0}, {1}";
    }
    StringBuilder sb = new StringBuilder();
    this.separatorFormat = SimpleFormatterImpl.compileToStringMinMaxArguments(sep, sb, 2, 2);

    String pattern = langData.get("localeDisplayPattern", "pattern");
    if (pattern == null || "pattern".equals(pattern)) {
        pattern = "{0} ({1})";
    }
    this.format = SimpleFormatterImpl.compileToStringMinMaxArguments(pattern, sb, 2, 2);
    if (pattern.contains("(")) {
        formatOpenParen = '(';
        formatCloseParen = ')';
        formatReplaceOpenParen = '[';
        formatReplaceCloseParen = ']';
    } else  {
        formatOpenParen = '(';
        formatCloseParen = ')';
        formatReplaceOpenParen = '[';
        formatReplaceCloseParen = ']';
    }

    String keyTypePattern = langData.get("localeDisplayPattern", "keyTypePattern");
    if (keyTypePattern == null || "keyTypePattern".equals(keyTypePattern)) {
        keyTypePattern = "{0}={1}";
    }
    this.keyTypeFormat = SimpleFormatterImpl.compileToStringMinMaxArguments(
            keyTypePattern, sb, 2, 2);

    // Get values from the contextTransforms data if we need them
    // Also check whether we will need a break iterator (depends on the data)
    boolean needBrkIter = false;
    if (capitalization == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU ||
            capitalization == DisplayContext.CAPITALIZATION_FOR_STANDALONE) {
        capitalizationUsage = new boolean[CapitalizationContextUsage.values().length]; // initialized to all false
        ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
        CapitalizationContextSink sink = new CapitalizationContextSink();
        try {
            rb.getAllItemsWithFallback("contextTransforms", sink);
        }
        catch (MissingResourceException e) {
            // Silently ignore.  Not every locale has contextTransforms.
        }
        needBrkIter = sink.hasCapitalizationUsage;
    }
    // Get a sentence break iterator if we will need it
    if (needBrkIter || capitalization == DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
        capitalizationBrkIter = BreakIterator.getSentenceInstance(locale);
    }

    this.currencyDisplayInfo = CurrencyData.provider.getInstance(locale, false);
}
 
Example 5
Source File: RelativeDateFormat.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public StringBuffer format(Calendar cal, StringBuffer toAppendTo,
        FieldPosition fieldPosition) {

    String relativeDayString = null;
    DisplayContext capitalizationContext = getContext(DisplayContext.Type.CAPITALIZATION);

    if (fDateStyle != DateFormat.NONE) {
        // calculate the difference, in days, between 'cal' and now.
        int dayDiff = dayDifference(cal);

        // look up string
        relativeDayString = getStringForDay(dayDiff);
    }

    if (fDateTimeFormat != null) {
        if (relativeDayString != null && fDatePattern != null &&
                (fTimePattern == null || fCombinedFormat == null || combinedFormatHasDateAtStart) ) {
            // capitalize relativeDayString according to context for relative, set formatter no context
            if ( relativeDayString.length() > 0 && UCharacter.isLowerCase(relativeDayString.codePointAt(0)) &&
                 (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
                    (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU && capitalizationOfRelativeUnitsForListOrMenu) ||
                    (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_STANDALONE && capitalizationOfRelativeUnitsForStandAlone) )) {
                if (capitalizationBrkIter == null) {
                    // should only happen when deserializing, etc.
                    capitalizationBrkIter = BreakIterator.getSentenceInstance(fLocale);
                }
                relativeDayString = UCharacter.toTitleCase(fLocale, relativeDayString, capitalizationBrkIter,
                                UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
            }
            fDateTimeFormat.setContext(DisplayContext.CAPITALIZATION_NONE);
        } else {
            // set our context for the formatter
            fDateTimeFormat.setContext(capitalizationContext);
        }
    }

    if (fDateTimeFormat != null && (fDatePattern != null || fTimePattern != null)) {
        // The new way
        if (fDatePattern == null) {
            // must have fTimePattern
            fDateTimeFormat.applyPattern(fTimePattern);
            fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
        } else if (fTimePattern == null) {
            // must have fDatePattern
            if (relativeDayString != null) {
                toAppendTo.append(relativeDayString);
            } else {
                fDateTimeFormat.applyPattern(fDatePattern);
                fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
            }
        } else {
            String datePattern = fDatePattern; // default;
            if (relativeDayString != null) {
                // Need to quote the relativeDayString to make it a legal date pattern
                datePattern = "'" + relativeDayString.replace("'", "''") + "'";
            }
            StringBuffer combinedPattern = new StringBuffer("");
            fCombinedFormat.format(new Object[] {fTimePattern, datePattern}, combinedPattern, new FieldPosition(0));
            fDateTimeFormat.applyPattern(combinedPattern.toString());
            fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
        }
    } else if (fDateFormat != null) {
        // A subset of the old way, for serialization compatibility
        // (just do the date part)
        if (relativeDayString != null) {
            toAppendTo.append(relativeDayString);
        } else {
            fDateFormat.format(cal, toAppendTo, fieldPosition);
        }
    }

    return toAppendTo;
}