Java Code Examples for android.icu.text.DisplayContext#type()

The following examples show how to use android.icu.text.DisplayContext#type() . 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 5 votes vote down vote up
public LocaleDisplayNames get(ULocale locale, DisplayContext... contexts) {
    DialectHandling dialectHandlingIn = DialectHandling.STANDARD_NAMES;
    DisplayContext capitalizationIn = DisplayContext.CAPITALIZATION_NONE;
    DisplayContext nameLengthIn = DisplayContext.LENGTH_FULL;
    DisplayContext substituteHandling = DisplayContext.SUBSTITUTE;
    for (DisplayContext contextItem : contexts) {
        switch (contextItem.type()) {
        case DIALECT_HANDLING:
            dialectHandlingIn = (contextItem.value()==DisplayContext.STANDARD_NAMES.value())?
                    DialectHandling.STANDARD_NAMES: DialectHandling.DIALECT_NAMES;
            break;
        case CAPITALIZATION:
            capitalizationIn = contextItem;
            break;
        case DISPLAY_LENGTH:
            nameLengthIn = contextItem;
            break;
        case SUBSTITUTE_HANDLING:
            substituteHandling = contextItem;
            break;
        default:
            break;
        }
    }
    if (!(dialectHandlingIn == this.dialectHandling && capitalizationIn == this.capitalization &&
            nameLengthIn == this.nameLength && substituteHandling == this.substituteHandling &&
            locale.equals(this.locale))) {
        this.locale = locale;
        this.dialectHandling = dialectHandlingIn;
        this.capitalization = capitalizationIn;
        this.nameLength = nameLengthIn;
        this.substituteHandling = substituteHandling;
        this.cache = new LocaleDisplayNamesImpl(locale, contexts);
    }
    return cache;
}
 
Example 2
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);
}