Java Code Examples for com.ibm.icu.lang.UCharacter#toTitleCase()

The following examples show how to use com.ibm.icu.lang.UCharacter#toTitleCase() . 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: RuleBasedNumberFormat.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust capitalization of formatted result for display context
 */
private String adjustForContext(String result) {
    if (result != null && result.length() > 0 && UCharacter.isLowerCase(result.codePointAt(0))) {
        DisplayContext capitalization = getContext(DisplayContext.Type.CAPITALIZATION);
        if (  capitalization==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
              (capitalization == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU && capitalizationForListOrMenu) ||
              (capitalization == DisplayContext.CAPITALIZATION_FOR_STANDALONE && capitalizationForStandAlone) ) {
            if (capitalizationBrkIter == null) {
                // should only happen when deserializing, etc.
                capitalizationBrkIter = BreakIterator.getSentenceInstance(locale);
            }
            return UCharacter.toTitleCase(locale, result, capitalizationBrkIter,
                            UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
        }
    }
    return result;
}
 
Example 2
Source File: LocaleDisplayNamesImpl.java    From fitnotifications 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 3
Source File: RelativeDateTimeFormatter.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private String adjustForContext(String originalFormattedString) {
    if (breakIterator == null || originalFormattedString.length() == 0
            || !UCharacter.isLowerCase(UCharacter.codePointAt(originalFormattedString, 0))) {
        return originalFormattedString;
    }
    synchronized (breakIterator) {
        return UCharacter.toTitleCase(
                locale,
                originalFormattedString,
                breakIterator,
                UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
    }
}
 
Example 4
Source File: TitlecaseTransliterator.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
@Override
public void addSourceTargetSet(UnicodeSet inputFilter, UnicodeSet sourceSet, UnicodeSet targetSet) {
    synchronized (this) {
        if (sourceTargetUtility == null) {
            sourceTargetUtility = new SourceTargetUtility(new Transform<String,String>() {
                @Override
                public String transform(String source) {
                    return UCharacter.toTitleCase(locale, source, null);
                }
            });
        }
    }
    sourceTargetUtility.addSourceTargetSet(this, inputFilter, sourceSet, targetSet);
}
 
Example 5
Source File: TitleCaseConverter.java    From tutorials with MIT License 5 votes vote down vote up
public static String convertToTitleCaseIcu4j(String text) {
    if (text == null || text.isEmpty()) {
        return text;
    }

    return UCharacter.toTitleCase(text, BreakIterator.getTitleInstance());
}
 
Example 6
Source File: RelativeDateFormat.java    From fitnotifications 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;
}
 
Example 7
Source File: Character.java    From juniversal with MIT License 2 votes vote down vote up
/**
 * Returns the title case equivalent for the specified code point if it
 * exists. Otherwise, the specified code point is returned unchanged.
 * 
 * @param codePoint
 *            the code point to convert.
 * @return the title case equivalent of {@code codePoint} if it exists,
 *         otherwise {@code codePoint}.
 */
public static int toTitleCase(int codePoint) {
    return UCharacter.toTitleCase(codePoint);
}