android.icu.text.DisplayContext Java Examples

The following examples show how to use android.icu.text.DisplayContext. 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: ULocaleCollationTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestIllformedLocale() {
    ULocale french = ULocale.FRENCH; 
    Collator collator = Collator.getInstance(french); 
    LocaleDisplayNames names = LocaleDisplayNames.getInstance(french,  
            DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU); 
    for (String malformed : Arrays.asList("en-a", "$", "ü--a", "en--US")) {
        try {
            Set<ULocale> supported = Collections.singleton(new ULocale(malformed));
            names.getUiList(supported, false, collator);
            assertNull("Failed to detect bogus locale «" + malformed + "»", supported);
        } catch (IllformedLocaleException e) {
            logln("Successfully detected ill-formed locale «" + malformed + "»:" + e.getMessage());
        } 
    }
}
 
Example #2
Source File: DatePickerCalendarDelegate.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
protected void onLocaleChanged(Locale locale) {
    final TextView headerYear = mHeaderYear;
    if (headerYear == null) {
        // Abort, we haven't initialized yet. This method will get called
        // again later after everything has been set up.
        return;
    }

    // Update the date formatter.
    mMonthDayFormat = DateFormat.getInstanceForSkeleton("EMMMd", locale);
    mMonthDayFormat.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
    mYearFormat = DateFormat.getInstanceForSkeleton("y", locale);

    // Update the header text.
    onCurrentDateChanged(false);
}
 
Example #3
Source File: DatePickerCalendarDelegate.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
@Override
protected void onLocaleChanged(Locale locale) {
    final TextView headerYear = mHeaderYear;
    if (headerYear == null) {
        // Abort, we haven't initialized yet. This method will get called
        // again later after everything has been set up.
        return;
    }

    // Update the date formatter.
    final String datePattern = DateFormatFix.getBestDateTimePattern(mContext, locale, "EMMMd");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        mMonthDayFormat = new SimpleDateFormat(datePattern, locale);
        ((SimpleDateFormat) mMonthDayFormat).setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
        mYearFormat = new SimpleDateFormat("y", locale);
    } else {
        mMonthDayFormat = new java.text.SimpleDateFormat(datePattern, locale);
        mYearFormat = new java.text.SimpleDateFormat("y", locale);
    }

    // Clear out the lazily-initialized accessibility event formatter.
    mAccessibilityEventFormat = null;

    // Update the header text.
    onCurrentDateChanged(false);
}
 
Example #4
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestSidewaysDataLoading() {
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("en_GB"),
            null,
            RelativeDateTimeFormatter.Style.NARROW,
            DisplayContext.CAPITALIZATION_NONE);
    String s = fmt.format(3.0, Direction.NEXT, RelativeUnit.MONTHS);
    assertEquals("narrow: in 3 months", "in 3 mo", s);
    String t = fmt.format(1.0, Direction.LAST, RelativeUnit.QUARTERS);
    assertEquals("narrow: 1 qtr ago", "1 qtr ago", t);
    // Check for fallback to SHORT
    String u = fmt.format(3.0, Direction.LAST, RelativeUnit.QUARTERS);
    assertEquals("narrow: 3 qtr ago", "3 qtr ago", u);
    // Do not expect fall back to SHORT
    String v = fmt.format(1.0, Direction.LAST, RelativeUnit.QUARTERS);
    assertEquals("narrow: 1 qtr ago", "1 qtr ago", v);
    String w = fmt.format(6.0, Direction.NEXT, RelativeUnit.QUARTERS);
    assertEquals("narrow: in 6 qtr", "in 6 qtr", w);
}
 
Example #5
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
public void put(UResource.Key key, UResource.Value value, boolean noFallback) {
    UResource.Table contextsTable = value.getTable();
    for (int i = 0; contextsTable.getKeyAndValue(i, key, value); ++i) {

        CapitalizationContextUsage usage = contextUsageTypeMap.get(key.toString());
        if (usage == null) { continue; };

        int[] intVector = value.getIntVector();
        if (intVector.length < 2) { continue; }

        int titlecaseInt = (capitalization == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU)
                ? intVector[0] : intVector[1];
        if (titlecaseInt == 0) { continue; }

        capitalizationUsage[usage.ordinal()] = true;
        hasCapitalizationUsage = true;
    }
}
 
Example #6
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestGetters() {
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("en_US"),
            null,
            RelativeDateTimeFormatter.Style.SHORT,
            DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE);
    assertEquals("", RelativeDateTimeFormatter.Style.SHORT, fmt.getFormatStyle());
    assertEquals(
            "",
            DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE,
            fmt.getCapitalizationContext());

    // test the no-arguments getInstance();
    RelativeDateTimeFormatter fmt_default = RelativeDateTimeFormatter.getInstance();
    assertEquals("", RelativeDateTimeFormatter.Style.LONG, fmt_default.getFormatStyle());
    assertEquals(
            "",
            DisplayContext.CAPITALIZATION_NONE,
            fmt_default.getCapitalizationContext());
}
 
Example #7
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@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;
    case DISPLAY_LENGTH:
        result = nameLength;
        break;
    case SUBSTITUTE_HANDLING:
        result = substituteHandling;
        break;
    default:
        result = DisplayContext.STANDARD_NAMES; // hmm, we should do something else here
        break;
    }
    return result;
}
 
Example #8
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 #9
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestRelativeDateWithQuantitySrFallback() {
    Object[][] data = {
            {0.0, Direction.NEXT, RelativeUnit.MONTHS, "\u0437\u0430 0 \u043C."},
            {1.2, Direction.NEXT, RelativeUnit.MONTHS, "\u0437\u0430 1,2 \u043C."},
            {21.0, Direction.NEXT, RelativeUnit.MONTHS, "\u0437\u0430 21 \u043C."},
    };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("sr"),
            null,
            RelativeDateTimeFormatter.Style.NARROW,
            DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format(
                ((Double) row[0]).doubleValue(), (Direction) row[1], (RelativeUnit) row[2]);
        assertEquals("Relative date with quantity fallback", row[3], actual);
    }
}
 
Example #10
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private String keyValueDisplayName(String key, String value, boolean skipAdjust) {
    String keyValueName = null;

    if (key.equals("currency")) {
        keyValueName = currencyDisplayInfo.getName(AsciiUtil.toUpperString(value));
        if (keyValueName == null) {
            keyValueName = value;
        }
    } else {
        if (nameLength == DisplayContext.LENGTH_SHORT) {
            String tmp = langData.get("Types%short", key, value);
            if (tmp != null && !tmp.equals(value)) {
                keyValueName = tmp;
            }
        }
        if (keyValueName == null) {
            keyValueName = langData.get("Types", key, value);
        }
    }

    return skipAdjust? keyValueName: adjustForUsageAndContext(CapitalizationContextUsage.KEYVALUE, keyValueName);
}
 
Example #11
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public LocaleDisplayNames get(ULocale locale, DialectHandling dialectHandling) {
    if (!(dialectHandling == this.dialectHandling && DisplayContext.CAPITALIZATION_NONE == this.capitalization &&
            DisplayContext.LENGTH_FULL == this.nameLength && DisplayContext.SUBSTITUTE == this.substituteHandling &&
            locale.equals(this.locale))) {
        this.locale = locale;
        this.dialectHandling = dialectHandling;
        this.capitalization = DisplayContext.CAPITALIZATION_NONE;
        this.nameLength = DisplayContext.LENGTH_FULL;
        this.substituteHandling = DisplayContext.SUBSTITUTE;
        this.cache = new LocaleDisplayNamesImpl(locale, dialectHandling);
    }
    return cache;
}
 
Example #12
Source File: ULocaleTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestUldnWithGarbage(){
    LocaleDisplayNames ldn = LocaleDisplayNames.getInstance(Locale.US, DisplayContext.DIALECT_NAMES);
    String badLocaleID = "english (United States) [w";
    String expectedResult = "english [united states] [w"; // case changed from input
    String result = ldn.localeDisplayName(badLocaleID);
    if (result.compareTo(expectedResult) != 0) {
        errln("FAIL: LocaleDisplayNames.localeDisplayName(String) for bad locale ID \"" + badLocaleID + "\", expected \"" + expectedResult + "\", got \"" + result + "\"");
    }
    ULocale badLocale = new ULocale(badLocaleID);
    result = ldn.localeDisplayName(badLocale);
    if (result.compareTo(expectedResult) != 0) {
        errln("FAIL: LocaleDisplayNames.localeDisplayName(ULocale) for bad locale ID \"" + badLocaleID + "\", expected \"" + expectedResult + "\", got \"" + result + "\"");
    }
}
 
Example #13
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestBadDisplayContext() {
    try {
        RelativeDateTimeFormatter.getInstance(
                new ULocale("en_US"),
                null,
                RelativeDateTimeFormatter.Style.LONG,
                DisplayContext.STANDARD_NAMES);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
}
 
Example #14
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 #15
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 #16
Source File: InputMethodSubtype.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @param displayLocale {@link Locale} to be used to display {@code localeToDisplay}
 * @param localeToDisplay {@link Locale} to be displayed in {@code displayLocale}
 * @param displayContext context parameter to be used to display {@code localeToDisplay} in
 * {@code displayLocale}
 * @return Returns the name of the {@code localeToDisplay} in the user's current locale.
 */
@NonNull
private static String getLocaleDisplayName(
        @Nullable Locale displayLocale, @Nullable Locale localeToDisplay,
        final DisplayContext displayContext) {
    if (localeToDisplay == null) {
        return "";
    }
    final Locale nonNullDisplayLocale =
            displayLocale != null ? displayLocale : Locale.getDefault();
    return LocaleDisplayNames
            .getInstance(nonNullDisplayLocale, displayContext)
            .localeDisplayName(localeToDisplay);
}
 
Example #17
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public String scriptDisplayName(String script) {
    String str = langData.get("Scripts%stand-alone", script);
    if (str == null || str.equals(script)) {
        if (nameLength == DisplayContext.LENGTH_SHORT) {
            str = langData.get("Scripts%short", script);
            if (str != null && !str.equals(script)) {
                return adjustForUsageAndContext(CapitalizationContextUsage.SCRIPT, str);
            }
        }
        str = langData.get("Scripts", script);
    }
    return adjustForUsageAndContext(CapitalizationContextUsage.SCRIPT, str);
}
 
Example #18
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public String languageDisplayName(String lang) {
    // Special case to eliminate non-languages, which pollute our data.
    if (lang.equals("root") || lang.indexOf('_') != -1) {
        return substituteHandling == DisplayContext.SUBSTITUTE ? lang : null;
    }
    if (nameLength == DisplayContext.LENGTH_SHORT) {
        String langName = langData.get("Languages%short", lang);
        if (langName != null && !langName.equals(lang)) {
            return adjustForUsageAndContext(CapitalizationContextUsage.LANGUAGE, langName);
        }
    }
    return adjustForUsageAndContext(CapitalizationContextUsage.LANGUAGE, langData.get("Languages", lang));
}
 
Example #19
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private String localeIdName(String localeId) {
    if (nameLength == DisplayContext.LENGTH_SHORT) {
        String locIdName = langData.get("Languages%short", localeId);
        if (locIdName != null && !locIdName.equals(localeId)) {
            return locIdName;
        }
    }
    return langData.get("Languages", localeId);
}
 
Example #20
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private UiListItem newRow(ULocale modified, DisplayContext capContext) {
    ULocale minimized = ULocale.minimizeSubtags(modified, ULocale.Minimize.FAVOR_SCRIPT);
    String tempName = modified.getDisplayName(locale);
    boolean titlecase = capContext == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU;
    String nameInDisplayLocale =  titlecase ? UCharacter.toTitleFirst(locale, tempName) : tempName;
    tempName = modified.getDisplayName(modified);
    String nameInSelf = capContext == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU ? UCharacter.toTitleFirst(modified, tempName) : tempName;
    return new UiListItem(minimized, modified, nameInDisplayLocale, nameInSelf);
}
 
Example #21
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestRelativeDateWithoutQuantityCaps() {
    Object[][] data = {
            {Direction.NEXT_2, AbsoluteUnit.DAY, null},

            {Direction.NEXT, AbsoluteUnit.DAY, "Tomorrow"},
            {Direction.NEXT, AbsoluteUnit.WEEK, "Next week"},
            {Direction.NEXT, AbsoluteUnit.MONTH, "Next month"},
            {Direction.NEXT, AbsoluteUnit.YEAR, "Next year"},

            {Direction.NEXT, AbsoluteUnit.MONDAY, "Next Monday"},
            {Direction.NEXT, AbsoluteUnit.TUESDAY, "Next Tuesday"},
            {Direction.NEXT, AbsoluteUnit.WEDNESDAY, "Next Wednesday"},
            {Direction.NEXT, AbsoluteUnit.THURSDAY, "Next Thursday"},
            {Direction.NEXT, AbsoluteUnit.FRIDAY, "Next Friday"},
            {Direction.NEXT, AbsoluteUnit.SATURDAY, "Next Saturday"},
            {Direction.NEXT, AbsoluteUnit.SUNDAY, "Next Sunday"},

            {Direction.LAST_2, AbsoluteUnit.DAY, null},

            {Direction.LAST, AbsoluteUnit.DAY, "Yesterday"},
            {Direction.LAST, AbsoluteUnit.WEEK, "Last week"},
            {Direction.LAST, AbsoluteUnit.MONTH, "Last month"},
            {Direction.LAST, AbsoluteUnit.YEAR, "Last year"},
            {Direction.LAST, AbsoluteUnit.MONDAY, "Last Monday"},
            {Direction.LAST, AbsoluteUnit.TUESDAY, "Last Tuesday"},
            {Direction.LAST, AbsoluteUnit.WEDNESDAY, "Last Wednesday"},
            {Direction.LAST, AbsoluteUnit.THURSDAY, "Last Thursday"},
            {Direction.LAST, AbsoluteUnit.FRIDAY, "Last Friday"},
            {Direction.LAST, AbsoluteUnit.SATURDAY, "Last Saturday"},
            {Direction.LAST, AbsoluteUnit.SUNDAY, "Last Sunday"},

            {Direction.THIS, AbsoluteUnit.DAY, "Today"},
            {Direction.THIS, AbsoluteUnit.WEEK, "This week"},
            {Direction.THIS, AbsoluteUnit.MONTH, "This month"},
            {Direction.THIS, AbsoluteUnit.YEAR, "This year"},
            {Direction.THIS, AbsoluteUnit.MONDAY, "This Monday"},
            {Direction.THIS, AbsoluteUnit.TUESDAY, "This Tuesday"},
            {Direction.THIS, AbsoluteUnit.WEDNESDAY, "This Wednesday"},
            {Direction.THIS, AbsoluteUnit.THURSDAY, "This Thursday"},
            {Direction.THIS, AbsoluteUnit.FRIDAY, "This Friday"},
            {Direction.THIS, AbsoluteUnit.SATURDAY, "This Saturday"},
            {Direction.THIS, AbsoluteUnit.SUNDAY, "This Sunday"},

            {Direction.PLAIN, AbsoluteUnit.DAY, "Day"},
            {Direction.PLAIN, AbsoluteUnit.WEEK, "Week"},
            {Direction.PLAIN, AbsoluteUnit.MONTH, "Month"},
            {Direction.PLAIN, AbsoluteUnit.YEAR, "Year"},
            {Direction.PLAIN, AbsoluteUnit.MONDAY, "Monday"},
            {Direction.PLAIN, AbsoluteUnit.TUESDAY, "Tuesday"},
            {Direction.PLAIN, AbsoluteUnit.WEDNESDAY, "Wednesday"},
            {Direction.PLAIN, AbsoluteUnit.THURSDAY, "Thursday"},
            {Direction.PLAIN, AbsoluteUnit.FRIDAY, "Friday"},
            {Direction.PLAIN, AbsoluteUnit.SATURDAY, "Saturday"},
            {Direction.PLAIN, AbsoluteUnit.SUNDAY, "Sunday"},

            {Direction.PLAIN, AbsoluteUnit.NOW, "Now"},

    };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("en_US"),
            null,
            RelativeDateTimeFormatter.Style.LONG,
            DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE);
    for (Object[] row : data) {
        String actual = fmt.format((Direction) row[0], (AbsoluteUnit) row[1]);
        assertEquals("Relative date without quantity caps", row[2], actual);
    }
}
 
Example #22
Source File: ULocaleCollationTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestNameList() { 
    String[][][] tests = { 
            /* name in French, name in self, minimized, modified */
            {{"fr-Cyrl-BE", "fr-Cyrl-CA"}, 
                {"Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE"}, 
                {"Français (cyrillique, Canada)", "Français (cyrillique, Canada)", "fr_Cyrl_CA", "fr_Cyrl_CA"}, 
            }, 
            {{"en", "de", "fr", "zh"}, 
                {"Allemand", "Deutsch", "de", "de"}, 
                {"Anglais", "English", "en", "en"}, 
                {"Chinois", "中文", "zh", "zh"}, 
                {"Français", "Français", "fr", "fr"}, 
            }, 
            // some non-canonical names
            {{"iw", "iw-US", "no", "no-Cyrl", "in", "in-YU"}, 
                {"Hébreu (États-Unis)", "עברית (ארצות הברית)", "iw_US", "iw_US"}, 
                {"Hébreu (Israël)", "עברית (ישראל)", "iw", "iw_IL"}, 
                {"Indonésien (Indonésie)", "Indonesia (Indonesia)", "in", "in_ID"}, 
                {"Indonésien (Serbie)", "Indonesia (Serbia)", "in_YU", "in_YU"}, 
                {"Norvégien (cyrillique)", "Norsk (kyrillisk)", "no_Cyrl", "no_Cyrl"}, 
                {"Norvégien (latin)", "Norsk (latinsk)", "no", "no_Latn"}, 
            }, 
            {{"zh-Hant-TW", "en", "en-gb", "fr", "zh-Hant", "de", "de-CH", "zh-TW"}, 
                {"Allemand (Allemagne)", "Deutsch (Deutschland)", "de", "de_DE"}, 
                {"Allemand (Suisse)", "Deutsch (Schweiz)", "de_CH", "de_CH"}, 
                {"Anglais (États-Unis)", "English (United States)", "en", "en_US"}, 
                {"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"}, 
                {"Chinois (traditionnel)", "中文(繁體)", "zh_Hant", "zh_Hant"}, 
                {"Français", "Français", "fr", "fr"}, 
            }, 
            {{"zh", "en-gb", "en-CA", "fr-Latn-FR"}, 
                {"Anglais (Canada)", "English (Canada)", "en_CA", "en_CA"}, 
                {"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"}, 
                {"Chinois", "中文", "zh", "zh"}, 
                {"Français", "Français", "fr", "fr"}, 
            }, 
            {{"en-gb", "fr", "zh-Hant", "zh-SG", "sr", "sr-Latn"}, 
                {"Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB"}, 
                {"Chinois (simplifié, Singapour)", "中文(简体,新加坡)", "zh_SG", "zh_Hans_SG"}, 
                {"Chinois (traditionnel, Taïwan)", "中文(繁體,台灣)", "zh_Hant", "zh_Hant_TW"}, 
                {"Français", "Français", "fr", "fr"}, 
                {"Serbe (cyrillique)", "Српски (ћирилица)", "sr", "sr_Cyrl"}, 
                {"Serbe (latin)", "Srpski (latinica)", "sr_Latn", "sr_Latn"}, 
            }, 
            {{"fr-Cyrl", "fr-Arab"}, 
                {"Français (arabe)", "Français (arabe)", "fr_Arab", "fr_Arab"}, 
                {"Français (cyrillique)", "Français (cyrillique)", "fr_Cyrl", "fr_Cyrl"}, 
            }, 
            {{"fr-Cyrl-BE", "fr-Arab-CA"}, 
                {"Français (arabe, Canada)", "Français (arabe, Canada)", "fr_Arab_CA", "fr_Arab_CA"}, 
                {"Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE"}, 
            } 
    }; 
    ULocale french = ULocale.FRENCH; 
    LocaleDisplayNames names = LocaleDisplayNames.getInstance(french,  
            DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU); 
    for (Type type : DisplayContext.Type.values()) { 
        logln("Contexts: " + names.getContext(type).toString()); 
    } 
    Collator collator = Collator.getInstance(french); 

    for (String[][] test : tests) { 
        Set<ULocale> list = new LinkedHashSet<ULocale>(); 
        List<UiListItem> expected = new ArrayList<UiListItem>(); 
        for (String item : test[0]) { 
            list.add(new ULocale(item)); 
        } 
        for (int i = 1; i < test.length; ++i) { 
            String[] rawRow = test[i]; 
            expected.add(new UiListItem(new ULocale(rawRow[2]), new ULocale(rawRow[3]), rawRow[0], rawRow[1])); 
        } 
        List<UiListItem> newList = names.getUiList(list, false, collator); 
        if (!expected.equals(newList)) { 
            if (expected.size() != newList.size()) { 
                errln(list.toString() + ": wrong size" + expected + ", " + newList); 
            } else { 
                errln(list.toString()); 
                for (int i = 0; i < expected.size(); ++i) { 
                    assertEquals(i+"", expected.get(i), newList.get(i)); 
                } 
            } 
        } else { 
            assertEquals(list.toString(), expected, newList); 
        } 
    } 
}
 
Example #23
Source File: SimpleMonthView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void updateMonthYearLabel() {
    final String format = DateFormat.getBestDateTimePattern(mLocale, MONTH_YEAR_FORMAT);
    final SimpleDateFormat formatter = new SimpleDateFormat(format, mLocale);
    formatter.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
    mMonthYearLabel = formatter.format(mCalendar.getTime());
}
 
Example #24
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static LocaleDisplayNames getInstance(ULocale locale, DisplayContext... contexts) {
    synchronized (cache) {
        return cache.get(locale, contexts);
    }
}
 
Example #25
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public LocaleDisplayNamesImpl(ULocale locale, DialectHandling dialectHandling) {
    this(locale, (dialectHandling==DialectHandling.STANDARD_NAMES)? DisplayContext.STANDARD_NAMES: DisplayContext.DIALECT_NAMES,
            DisplayContext.CAPITALIZATION_NONE);
}
 
Example #26
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 #27
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestRelativeDateWithoutQuantityNarrow() {
    Object[][] data = {
            {Direction.NEXT_2, AbsoluteUnit.DAY, null},

            {Direction.NEXT, AbsoluteUnit.DAY, "tomorrow"},
            {Direction.NEXT, AbsoluteUnit.WEEK, "next wk."},

            {Direction.NEXT, AbsoluteUnit.MONTH, "next mo."},
            {Direction.NEXT, AbsoluteUnit.YEAR, "next yr."},

            {Direction.NEXT, AbsoluteUnit.MONDAY, "next M"},

            {Direction.NEXT, AbsoluteUnit.TUESDAY, "next Tu"},
            {Direction.NEXT, AbsoluteUnit.WEDNESDAY, "next W"},
            {Direction.NEXT, AbsoluteUnit.THURSDAY, "next Th"},
            {Direction.NEXT, AbsoluteUnit.FRIDAY, "next F"},
            {Direction.NEXT, AbsoluteUnit.SATURDAY, "next Sa"},
            {Direction.NEXT, AbsoluteUnit.SUNDAY, "next Su"},

            {Direction.LAST_2, AbsoluteUnit.DAY, null},

            {Direction.LAST, AbsoluteUnit.DAY, "yesterday"},
            {Direction.LAST, AbsoluteUnit.WEEK, "last wk."},
            {Direction.LAST, AbsoluteUnit.MONTH, "last mo."},
            {Direction.LAST, AbsoluteUnit.YEAR, "last yr."},
            {Direction.LAST, AbsoluteUnit.MONDAY, "last M"},
            {Direction.LAST, AbsoluteUnit.TUESDAY, "last Tu"},
            {Direction.LAST, AbsoluteUnit.WEDNESDAY, "last W"},
            {Direction.LAST, AbsoluteUnit.THURSDAY, "last Th"},
            {Direction.LAST, AbsoluteUnit.FRIDAY, "last F"},
            {Direction.LAST, AbsoluteUnit.SATURDAY, "last Sa"},
            {Direction.LAST, AbsoluteUnit.SUNDAY, "last Su"},

            {Direction.THIS, AbsoluteUnit.DAY, "today"},
            {Direction.THIS, AbsoluteUnit.WEEK, "this wk."},
            {Direction.THIS, AbsoluteUnit.MONTH, "this mo."},
            {Direction.THIS, AbsoluteUnit.YEAR, "this yr."},
            {Direction.THIS, AbsoluteUnit.MONDAY, "this M"},
            {Direction.THIS, AbsoluteUnit.TUESDAY, "this Tu"},
            {Direction.THIS, AbsoluteUnit.WEDNESDAY, "this W"},
            {Direction.THIS, AbsoluteUnit.THURSDAY, "this Th"},

            {Direction.THIS, AbsoluteUnit.FRIDAY, "this F"},

            {Direction.THIS, AbsoluteUnit.SATURDAY, "this Sa"},
            {Direction.THIS, AbsoluteUnit.SUNDAY, "this Su"},

            {Direction.PLAIN, AbsoluteUnit.DAY, "day"},
            {Direction.PLAIN, AbsoluteUnit.WEEK, "wk."},
            {Direction.PLAIN, AbsoluteUnit.MONTH, "mo."},
            {Direction.PLAIN, AbsoluteUnit.YEAR, "yr."},
            {Direction.PLAIN, AbsoluteUnit.MONDAY, "M"},
            {Direction.PLAIN, AbsoluteUnit.TUESDAY, "T"},
            {Direction.PLAIN, AbsoluteUnit.WEDNESDAY, "W"},
            {Direction.PLAIN, AbsoluteUnit.THURSDAY, "T"},
            {Direction.PLAIN, AbsoluteUnit.FRIDAY, "F"},
            {Direction.PLAIN, AbsoluteUnit.SATURDAY, "S"},
            {Direction.PLAIN, AbsoluteUnit.SUNDAY, "S"},

            {Direction.PLAIN, AbsoluteUnit.NOW, "now"},

    };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("en_US"),
            null,
            RelativeDateTimeFormatter.Style.NARROW,
            DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format((Direction) row[0], (AbsoluteUnit) row[1]);
        assertEquals("Relative date without quantity narrow", row[2], actual);
    }
}
 
Example #28
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestRelativeDateWithoutQuantityShort() {
    Object[][] data = {
            {Direction.NEXT_2, AbsoluteUnit.DAY, null},

            {Direction.NEXT, AbsoluteUnit.DAY, "tomorrow"},
            {Direction.NEXT, AbsoluteUnit.WEEK, "next wk."},

            {Direction.NEXT, AbsoluteUnit.MONTH, "next mo."},
            {Direction.NEXT, AbsoluteUnit.YEAR, "next yr."},

            {Direction.NEXT, AbsoluteUnit.MONDAY, "next Mon."},

            {Direction.NEXT, AbsoluteUnit.TUESDAY, "next Tue."},
            {Direction.NEXT, AbsoluteUnit.WEDNESDAY, "next Wed."},
            {Direction.NEXT, AbsoluteUnit.THURSDAY, "next Thu."},
            {Direction.NEXT, AbsoluteUnit.FRIDAY, "next Fri."},
            {Direction.NEXT, AbsoluteUnit.SATURDAY, "next Sat."},
            {Direction.NEXT, AbsoluteUnit.SUNDAY, "next Sun."},

            {Direction.LAST_2, AbsoluteUnit.DAY, null},

            {Direction.LAST, AbsoluteUnit.DAY, "yesterday"},
            {Direction.LAST, AbsoluteUnit.WEEK, "last wk."},
            {Direction.LAST, AbsoluteUnit.MONTH, "last mo."},
            {Direction.LAST, AbsoluteUnit.YEAR, "last yr."},
            {Direction.LAST, AbsoluteUnit.MONDAY, "last Mon."},
            {Direction.LAST, AbsoluteUnit.TUESDAY, "last Tue."},
            {Direction.LAST, AbsoluteUnit.WEDNESDAY, "last Wed."},
            {Direction.LAST, AbsoluteUnit.THURSDAY, "last Thu."},

            {Direction.LAST, AbsoluteUnit.FRIDAY, "last Fri."},

            {Direction.LAST, AbsoluteUnit.SATURDAY, "last Sat."},
            {Direction.LAST, AbsoluteUnit.SUNDAY, "last Sun."},

            {Direction.THIS, AbsoluteUnit.DAY, "today"},
            {Direction.THIS, AbsoluteUnit.WEEK, "this wk."},
            {Direction.THIS, AbsoluteUnit.MONTH, "this mo."},
            {Direction.THIS, AbsoluteUnit.YEAR, "this yr."},
            {Direction.THIS, AbsoluteUnit.MONDAY, "this Mon."},
            {Direction.THIS, AbsoluteUnit.TUESDAY, "this Tue."},
            {Direction.THIS, AbsoluteUnit.WEDNESDAY, "this Wed."},
            {Direction.THIS, AbsoluteUnit.THURSDAY, "this Thu."},
            {Direction.THIS, AbsoluteUnit.FRIDAY, "this Fri."},
            {Direction.THIS, AbsoluteUnit.SATURDAY, "this Sat."},
            {Direction.THIS, AbsoluteUnit.SUNDAY, "this Sun."},

            {Direction.PLAIN, AbsoluteUnit.DAY, "day"},
            {Direction.PLAIN, AbsoluteUnit.WEEK, "wk."},
            {Direction.PLAIN, AbsoluteUnit.MONTH, "mo."},
            {Direction.PLAIN, AbsoluteUnit.YEAR, "yr."},
            {Direction.PLAIN, AbsoluteUnit.MONDAY, "Mo"},
            {Direction.PLAIN, AbsoluteUnit.TUESDAY, "Tu"},
            {Direction.PLAIN, AbsoluteUnit.WEDNESDAY, "We"},
            {Direction.PLAIN, AbsoluteUnit.THURSDAY, "Th"},
            {Direction.PLAIN, AbsoluteUnit.FRIDAY, "Fr"},
            {Direction.PLAIN, AbsoluteUnit.SATURDAY, "Sa"},
            {Direction.PLAIN, AbsoluteUnit.SUNDAY, "Su"},

            {Direction.PLAIN, AbsoluteUnit.NOW, "now"},

    };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("en_US"),
            null,
            RelativeDateTimeFormatter.Style.SHORT,
            DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format((Direction) row[0], (AbsoluteUnit) row[1]);
        assertEquals("Relative date without quantity short", row[2], actual);
    }
}
 
Example #29
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestRelativeDateWithQuantityNarrow() {
    Object[][] data = {
            {0.0, Direction.NEXT, RelativeUnit.SECONDS, "in 0 sec."},
            {0.5, Direction.NEXT, RelativeUnit.SECONDS, "in 0.5 sec."},

            {1.0, Direction.NEXT, RelativeUnit.SECONDS, "in 1 sec."},
            {2.0, Direction.NEXT, RelativeUnit.SECONDS, "in 2 sec."},
            {0.0, Direction.NEXT, RelativeUnit.MINUTES, "in 0 min."},
            {0.5, Direction.NEXT, RelativeUnit.MINUTES, "in 0.5 min."},
            {1.0, Direction.NEXT, RelativeUnit.MINUTES, "in 1 min."},
            {2.0, Direction.NEXT, RelativeUnit.MINUTES, "in 2 min."},
            {0.0, Direction.NEXT, RelativeUnit.HOURS, "in 0 hr."},
            {0.5, Direction.NEXT, RelativeUnit.HOURS, "in 0.5 hr."},
            {1.0, Direction.NEXT, RelativeUnit.HOURS, "in 1 hr."},
            {2.0, Direction.NEXT, RelativeUnit.HOURS, "in 2 hr."},
            {0.0, Direction.NEXT, RelativeUnit.DAYS, "in 0 days"},
            {0.5, Direction.NEXT, RelativeUnit.DAYS, "in 0.5 days"},
            {1.0, Direction.NEXT, RelativeUnit.DAYS, "in 1 day"},
            {2.0, Direction.NEXT, RelativeUnit.DAYS, "in 2 days"},
            {0.0, Direction.NEXT, RelativeUnit.WEEKS, "in 0 wk."},
            {0.5, Direction.NEXT, RelativeUnit.WEEKS, "in 0.5 wk."},
            {1.0, Direction.NEXT, RelativeUnit.WEEKS, "in 1 wk."},
            {2.0, Direction.NEXT, RelativeUnit.WEEKS, "in 2 wk."},
            {0.0, Direction.NEXT, RelativeUnit.MONTHS, "in 0 mo."},
            {0.5, Direction.NEXT, RelativeUnit.MONTHS, "in 0.5 mo."},
            {1.0, Direction.NEXT, RelativeUnit.MONTHS, "in 1 mo."},
            {2.0, Direction.NEXT, RelativeUnit.MONTHS, "in 2 mo."},
            {0.0, Direction.NEXT, RelativeUnit.YEARS, "in 0 yr."},
            {0.5, Direction.NEXT, RelativeUnit.YEARS, "in 0.5 yr."},
            {1.0, Direction.NEXT, RelativeUnit.YEARS, "in 1 yr."},
            {2.0, Direction.NEXT, RelativeUnit.YEARS, "in 2 yr."},

            {0.0, Direction.LAST, RelativeUnit.SECONDS, "0 sec. ago"},
            {0.5, Direction.LAST, RelativeUnit.SECONDS, "0.5 sec. ago"},
            {1.0, Direction.LAST, RelativeUnit.SECONDS, "1 sec. ago"},
            {2.0, Direction.LAST, RelativeUnit.SECONDS, "2 sec. ago"},
            {0.0, Direction.LAST, RelativeUnit.MINUTES, "0 min. ago"},
            {0.5, Direction.LAST, RelativeUnit.MINUTES, "0.5 min. ago"},
            {1.0, Direction.LAST, RelativeUnit.MINUTES, "1 min. ago"},
            {2.0, Direction.LAST, RelativeUnit.MINUTES, "2 min. ago"},
            {0.0, Direction.LAST, RelativeUnit.HOURS, "0 hr. ago"},
            {0.5, Direction.LAST, RelativeUnit.HOURS, "0.5 hr. ago"},
            {1.0, Direction.LAST, RelativeUnit.HOURS, "1 hr. ago"},
            {2.0, Direction.LAST, RelativeUnit.HOURS, "2 hr. ago"},
            {0.0, Direction.LAST, RelativeUnit.DAYS, "0 days ago"},
            {0.5, Direction.LAST, RelativeUnit.DAYS, "0.5 days ago"},
            {1.0, Direction.LAST, RelativeUnit.DAYS, "1 day ago"},
            {2.0, Direction.LAST, RelativeUnit.DAYS, "2 days ago"},
            {0.0, Direction.LAST, RelativeUnit.WEEKS, "0 wk. ago"},
            {0.5, Direction.LAST, RelativeUnit.WEEKS, "0.5 wk. ago"},
            {1.0, Direction.LAST, RelativeUnit.WEEKS, "1 wk. ago"},
            {2.0, Direction.LAST, RelativeUnit.WEEKS, "2 wk. ago"},
            {0.0, Direction.LAST, RelativeUnit.MONTHS, "0 mo. ago"},
            {0.5, Direction.LAST, RelativeUnit.MONTHS, "0.5 mo. ago"},
            {1.0, Direction.LAST, RelativeUnit.MONTHS, "1 mo. ago"},
            {2.0, Direction.LAST, RelativeUnit.MONTHS, "2 mo. ago"},
            {0.0, Direction.LAST, RelativeUnit.YEARS, "0 yr. ago"},
            {0.5, Direction.LAST, RelativeUnit.YEARS, "0.5 yr. ago"},
            {1.0, Direction.LAST, RelativeUnit.YEARS, "1 yr. ago"},
            {2.0, Direction.LAST, RelativeUnit.YEARS, "2 yr. ago"},

    };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("en_US"),
            null,
            RelativeDateTimeFormatter.Style.NARROW,
            DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format(
                ((Double) row[0]).doubleValue(), (Direction) row[1], (RelativeUnit) row[2]);
        assertEquals("Relative date with quantity", row[3], actual);
    }
}
 
Example #30
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestRelativeDateWithQuantityShort() {
    Object[][] data = {
            {0.0, Direction.NEXT, RelativeUnit.SECONDS, "in 0 sec."},
            {0.5, Direction.NEXT, RelativeUnit.SECONDS, "in 0.5 sec."},

            {1.0, Direction.NEXT, RelativeUnit.SECONDS, "in 1 sec."},
            {2.0, Direction.NEXT, RelativeUnit.SECONDS, "in 2 sec."},
            {0.0, Direction.NEXT, RelativeUnit.MINUTES, "in 0 min."},
            {0.5, Direction.NEXT, RelativeUnit.MINUTES, "in 0.5 min."},
            {1.0, Direction.NEXT, RelativeUnit.MINUTES, "in 1 min."},
            {2.0, Direction.NEXT, RelativeUnit.MINUTES, "in 2 min."},
            {0.0, Direction.NEXT, RelativeUnit.HOURS, "in 0 hr."},
            {0.5, Direction.NEXT, RelativeUnit.HOURS, "in 0.5 hr."},
            {1.0, Direction.NEXT, RelativeUnit.HOURS, "in 1 hr."},
            {2.0, Direction.NEXT, RelativeUnit.HOURS, "in 2 hr."},
            {0.0, Direction.NEXT, RelativeUnit.DAYS, "in 0 days"},
            {0.5, Direction.NEXT, RelativeUnit.DAYS, "in 0.5 days"},
            {1.0, Direction.NEXT, RelativeUnit.DAYS, "in 1 day"},
            {2.0, Direction.NEXT, RelativeUnit.DAYS, "in 2 days"},
            {0.0, Direction.NEXT, RelativeUnit.WEEKS, "in 0 wk."},
            {0.5, Direction.NEXT, RelativeUnit.WEEKS, "in 0.5 wk."},
            {1.0, Direction.NEXT, RelativeUnit.WEEKS, "in 1 wk."},
            {2.0, Direction.NEXT, RelativeUnit.WEEKS, "in 2 wk."},
            {0.0, Direction.NEXT, RelativeUnit.MONTHS, "in 0 mo."},
            {0.5, Direction.NEXT, RelativeUnit.MONTHS, "in 0.5 mo."},
            {1.0, Direction.NEXT, RelativeUnit.MONTHS, "in 1 mo."},
            {2.0, Direction.NEXT, RelativeUnit.MONTHS, "in 2 mo."},
            {0.0, Direction.NEXT, RelativeUnit.YEARS, "in 0 yr."},
            {0.5, Direction.NEXT, RelativeUnit.YEARS, "in 0.5 yr."},
            {1.0, Direction.NEXT, RelativeUnit.YEARS, "in 1 yr."},
            {2.0, Direction.NEXT, RelativeUnit.YEARS, "in 2 yr."},

            {0.0, Direction.LAST, RelativeUnit.SECONDS, "0 sec. ago"},
            {0.5, Direction.LAST, RelativeUnit.SECONDS, "0.5 sec. ago"},
            {1.0, Direction.LAST, RelativeUnit.SECONDS, "1 sec. ago"},
            {2.0, Direction.LAST, RelativeUnit.SECONDS, "2 sec. ago"},
            {0.0, Direction.LAST, RelativeUnit.MINUTES, "0 min. ago"},
            {0.5, Direction.LAST, RelativeUnit.MINUTES, "0.5 min. ago"},
            {1.0, Direction.LAST, RelativeUnit.MINUTES, "1 min. ago"},
            {2.0, Direction.LAST, RelativeUnit.MINUTES, "2 min. ago"},
            {0.0, Direction.LAST, RelativeUnit.HOURS, "0 hr. ago"},
            {0.5, Direction.LAST, RelativeUnit.HOURS, "0.5 hr. ago"},
            {1.0, Direction.LAST, RelativeUnit.HOURS, "1 hr. ago"},
            {2.0, Direction.LAST, RelativeUnit.HOURS, "2 hr. ago"},
            {0.0, Direction.LAST, RelativeUnit.DAYS, "0 days ago"},
            {0.5, Direction.LAST, RelativeUnit.DAYS, "0.5 days ago"},
            {1.0, Direction.LAST, RelativeUnit.DAYS, "1 day ago"},
            {2.0, Direction.LAST, RelativeUnit.DAYS, "2 days ago"},
            {0.0, Direction.LAST, RelativeUnit.WEEKS, "0 wk. ago"},
            {0.5, Direction.LAST, RelativeUnit.WEEKS, "0.5 wk. ago"},
            {1.0, Direction.LAST, RelativeUnit.WEEKS, "1 wk. ago"},
            {2.0, Direction.LAST, RelativeUnit.WEEKS, "2 wk. ago"},
            {0.0, Direction.LAST, RelativeUnit.MONTHS, "0 mo. ago"},
            {0.5, Direction.LAST, RelativeUnit.MONTHS, "0.5 mo. ago"},
            {1.0, Direction.LAST, RelativeUnit.MONTHS, "1 mo. ago"},
            {2.0, Direction.LAST, RelativeUnit.MONTHS, "2 mo. ago"},
            {0.0, Direction.LAST, RelativeUnit.YEARS, "0 yr. ago"},
            {0.5, Direction.LAST, RelativeUnit.YEARS, "0.5 yr. ago"},
            {1.0, Direction.LAST, RelativeUnit.YEARS, "1 yr. ago"},
            {2.0, Direction.LAST, RelativeUnit.YEARS, "2 yr. ago"},

    };
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(
            new ULocale("en_US"),
            null,
            RelativeDateTimeFormatter.Style.SHORT,
            DisplayContext.CAPITALIZATION_NONE);
    for (Object[] row : data) {
        String actual = fmt.format(
                ((Double) row[0]).doubleValue(), (Direction) row[1], (RelativeUnit) row[2]);
        assertEquals("Relative date with quantity", row[3], actual);
    }
}