diff --git a/.gitattributes b/.gitattributes index e7824d23bc0..19228d8c9b0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -251,6 +251,7 @@ icu4j/main/classes/core/.project -text icu4j/main/classes/core/.settings/org.eclipse.core.resources.prefs -text icu4j/main/classes/core/.settings/org.eclipse.jdt.core.prefs -text icu4j/main/classes/core/manifest.stub -text +icu4j/main/classes/core/src/com/ibm/icu/text/DisplayContext.java -text icu4j/main/classes/core/src/com/ibm/icu/util/PersianCalendar.java -text icu4j/main/classes/currdata/.externalToolBuilders/copy-data-currdata.launch -text icu4j/main/classes/currdata/.settings/org.eclipse.core.resources.prefs -text diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleDisplayNamesImpl.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleDisplayNamesImpl.java index 85e9ea7ffdc..36c6ac9e797 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleDisplayNamesImpl.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/LocaleDisplayNamesImpl.java @@ -11,6 +11,7 @@ import java.util.Locale; import com.ibm.icu.lang.UScript; import com.ibm.icu.text.LocaleDisplayNames; +import com.ibm.icu.text.DisplayContext; import com.ibm.icu.text.MessageFormat; import com.ibm.icu.util.ULocale; import com.ibm.icu.util.UResourceBundle; @@ -18,6 +19,7 @@ import com.ibm.icu.util.UResourceBundle; public class LocaleDisplayNamesImpl extends LocaleDisplayNames { private final ULocale locale; private final DialectHandling dialectHandling; + private final DisplayContext capitalization; private final DataTable langData; private final DataTable regionData; private final Appender appender; @@ -32,8 +34,36 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames { } } + public static LocaleDisplayNames getInstance(ULocale locale, DisplayContext... contexts) { + synchronized (cache) { + return cache.get(locale, contexts); + } + } + public LocaleDisplayNamesImpl(ULocale locale, DialectHandling dialectHandling) { + this(locale, (dialectHandling==DialectHandling.STANDARD_NAMES)? DisplayContext.STANDARD_NAMES: DisplayContext.DIALECT_NAMES, + DisplayContext.CAPITALIZATION_NONE); + } + + public LocaleDisplayNamesImpl(ULocale locale, DisplayContext... contexts) { + DialectHandling dialectHandling = DialectHandling.STANDARD_NAMES; + DisplayContext capitalization = DisplayContext.CAPITALIZATION_NONE; + 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; + default: + break; + } + } + this.dialectHandling = dialectHandling; + this.capitalization = capitalization; this.langData = LangDataTables.impl.get(locale); this.regionData = RegionDataTables.impl.get(locale); this.locale = ULocale.ROOT.equals(langData.getLocale()) ? regionData.getLocale() : @@ -72,6 +102,23 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames { return dialectHandling; } + @Override + public DisplayContext getContext(DisplayContext.Type type) { + DisplayContext result; + switch (type) { + case DIALECT_HANDLING: + result = (dialectHandling==DialectHandling.STANDARD_NAMES)? DisplayContext.STANDARD_NAMES: DisplayContext.DIALECT_NAMES; + break; + case CAPITALIZATION: + result = capitalization; + break; + default: + result = DisplayContext.STANDARD_NAMES; // hmm, we should do something else here + break; + } + return result; + } + @Override public String localeDisplayName(ULocale locale) { return localeDisplayNameInternal(locale); @@ -87,6 +134,7 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames { return localeDisplayNameInternal(new ULocale(localeId)); } + // TOTO: implement use of capitalization private String localeDisplayNameInternal(ULocale locale) { // lang // lang (script, country, variant, keyword=value, ...) @@ -346,14 +394,40 @@ public class LocaleDisplayNamesImpl extends LocaleDisplayNames { private static class Cache { private ULocale locale; private DialectHandling dialectHandling; + private DisplayContext capitalization; private LocaleDisplayNames cache; public LocaleDisplayNames get(ULocale locale, DialectHandling dialectHandling) { - if (!(dialectHandling == this.dialectHandling && locale.equals(this.locale))) { + if (!(dialectHandling == this.dialectHandling && DisplayContext.CAPITALIZATION_NONE == this.capitalization && locale.equals(this.locale))) { this.locale = locale; this.dialectHandling = dialectHandling; + this.capitalization = DisplayContext.CAPITALIZATION_NONE; this.cache = new LocaleDisplayNamesImpl(locale, dialectHandling); } return cache; } + public LocaleDisplayNames get(ULocale locale, DisplayContext... contexts) { + DialectHandling dialectHandlingIn = DialectHandling.STANDARD_NAMES; + DisplayContext capitalizationIn = DisplayContext.CAPITALIZATION_NONE; + 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; + default: + break; + } + } + if (!(dialectHandlingIn == this.dialectHandling && capitalizationIn == this.capitalization && locale.equals(this.locale))) { + this.locale = locale; + this.dialectHandling = dialectHandlingIn; + this.capitalization = capitalizationIn; + this.cache = new LocaleDisplayNamesImpl(locale, contexts); + } + return cache; + } } } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DisplayContext.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DisplayContext.java new file mode 100644 index 00000000000..df9b8f7144f --- /dev/null +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DisplayContext.java @@ -0,0 +1,113 @@ +/* + ******************************************************************************* + * Copyright (C) 2012, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* + */ +package com.ibm.icu.text; + +/** + * Display context settings. + * Note, the specific numeric values are internal and may change. + * @internal ICU 50 technology preview + */ +public enum DisplayContext { + /** + * ================================ + * Settings for DIALECT_HANDLING (use one) + */ + /** + * A possible setting for DIALECT_HANDLING: + * use standard names when generating a locale name, + * e.g. en_GB displays as 'English (United Kingdom)'. + * @internal ICU 50 technology preview + */ + STANDARD_NAMES(Type.DIALECT_HANDLING, 0), + /** + * A possible setting for DIALECT_HANDLING: + * use dialect names, when generating a locale name, + * e.g. en_GB displays as 'British English'. + * @internal ICU 50 technology preview + */ + DIALECT_NAMES(Type.DIALECT_HANDLING, 1), + /** + * ================================ + * Settings for CAPITALIZATION (use one) + */ + /** + * A possible setting for CAPITALIZATION: + * The capitalization context to be used is unknown (this is the default value). + * @internal ICU 50 technology preview + */ + CAPITALIZATION_NONE(Type.CAPITALIZATION, 0), + /** + * A possible setting for CAPITALIZATION: + * The capitalization context if a date, date symbol or display name is to be + * formatted with capitalization appropriate for the middle of a sentence. + * @internal ICU 50 technology preview + */ + CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE(Type.CAPITALIZATION, 1), + /** + * A possible setting for CAPITALIZATION: + * The capitalization context if a date, date symbol or display name is to be + * formatted with capitalization appropriate for the beginning of a sentence. + * @internal ICU 50 technology preview + */ + CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE(Type.CAPITALIZATION, 2), + /** + * A possible setting for CAPITALIZATION: + * The capitalization context if a date, date symbol or display name is to be + * formatted with capitalization appropriate for a user-interface list or menu item. + * @internal ICU 50 technology preview + */ + CAPITALIZATION_FOR_UI_LIST_OR_MENU(Type.CAPITALIZATION, 3), + /** + * A possible setting for CAPITALIZATION: + * The capitalization context if a date, date symbol or display name is to be + * formatted with capitalization appropriate for stand-alone usage such as an + * isolated name on a calendar page. + * @internal ICU 50 technology preview + */ + CAPITALIZATION_FOR_STANDALONE(Type.CAPITALIZATION, 4); + + /** + * Type values for DisplayContext + * @internal ICU 50 technology preview + */ + public enum Type { + /** + * DIALECT_HANDLING can be set to STANDARD_NAMES or DIALECT_NAMES. + * @internal ICU 50 technology preview + */ + DIALECT_HANDLING, + /** + * CAPITALIZATION can be set to one of CAPITALIZATION_NONE through + * CAPITALIZATION_FOR_STANDALONE. + * @internal ICU 50 technology preview + */ + CAPITALIZATION + } + + private final Type type; + private final int value; + DisplayContext(Type type, int value) { + this.type = type; + this.value = value; + } + /** + * Get the Type part of the enum item + * (e.g. CAPITALIZATION) + * @internal ICU 50 technology preview + */ + public Type type() { + return type; + } + /** + * Get the value part of the enum item + * (e.g. CAPITALIZATION_FOR_STANDALONE) + * @internal ICU 50 technology preview + */ + public int value() { + return value; + } +} diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/LocaleDisplayNames.java b/icu4j/main/classes/core/src/com/ibm/icu/text/LocaleDisplayNames.java index becff45938e..7d5cef068a4 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/LocaleDisplayNames.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/LocaleDisplayNames.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2009-2011, International Business Machines Corporation and * + * Copyright (C) 2009-2012, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -61,6 +61,19 @@ public abstract class LocaleDisplayNames { return LocaleDisplayNamesImpl.getInstance(locale, dialectHandling); } + /** + * Returns an instance of LocaleDisplayNames that returns names formatted for the provided locale, + * using the provided DisplayContext settings + * @param locale the display locale + * @param contexts one or more context settings (e.g. for dialect + * handling, capitalization, etc. + * @return a LocaleDisplayNames instance + * @internal ICU 50 technology preview + */ + public static LocaleDisplayNames getInstance(ULocale locale, DisplayContext... contexts) { + return LocaleDisplayNamesImpl.getInstance(locale, contexts); + } + // getters for state /** * Returns the locale used to determine the display names. This is not necessarily the same @@ -77,6 +90,14 @@ public abstract class LocaleDisplayNames { */ public abstract DialectHandling getDialectHandling(); + /** + * Returns the current value for a specified DisplayContext.Type. + * @param type the DisplayContext.Type whose value to return + * @return the current DisplayContext setting for the specified type + * @internal ICU 50 technology preview + */ + public abstract DisplayContext getContext(DisplayContext.Type type); + // names for entire locales /** * Returns the display name of the provided ulocale.