示例#1
0
        /// <summary>
        /// Gets the text for the specified chrono, field, locale and style
        /// for the purpose of formatting.
        /// <para>
        /// The text associated with the value is returned.
        /// The null return value should be used if there is no applicable text, or
        /// if the text would be a numeric representation of the value.
        ///
        /// </para>
        /// </summary>
        /// <param name="chrono">  the Chronology to get text for, not null </param>
        /// <param name="field">  the field to get text for, not null </param>
        /// <param name="value">  the field value to get text for, not null </param>
        /// <param name="style">  the style to get text for, not null </param>
        /// <param name="locale">  the locale to get text for, not null </param>
        /// <returns> the text for the field value, null if no text found </returns>
        public virtual String GetText(Chronology chrono, TemporalField field, long value, TextStyle style, Locale locale)
        {
            if (chrono == IsoChronology.INSTANCE || !(field is ChronoField))
            {
                return(GetText(field, value, style, locale));
            }

            int fieldIndex;
            int fieldValue;

            if (field == ERA)
            {
                fieldIndex = DateTime.ERA;
                if (chrono == JapaneseChronology.INSTANCE)
                {
                    if (value == -999)
                    {
                        fieldValue = 0;
                    }
                    else
                    {
                        fieldValue = (int)value + 2;
                    }
                }
                else
                {
                    fieldValue = (int)value;
                }
            }
            else if (field == MONTH_OF_YEAR)
            {
                fieldIndex = DateTime.MONTH;
                fieldValue = (int)value - 1;
            }
            else if (field == DAY_OF_WEEK)
            {
                fieldIndex = DateTime.DAY_OF_WEEK;
                fieldValue = (int)value + 1;
                if (fieldValue > 7)
                {
                    fieldValue = DayOfWeek.Sunday;
                }
            }
            else if (field == AMPM_OF_DAY)
            {
                fieldIndex = DateTime.AM_PM;
                fieldValue = (int)value;
            }
            else
            {
                return(null);
            }
            return(CalendarDataUtility.retrieveJavaTimeFieldValueName(chrono.CalendarType, fieldIndex, fieldValue, style.toCalendarStyle(), locale));
        }
示例#2
0
        private Object CreateStore(TemporalField field, Locale locale)
        {
            IDictionary <TextStyle, IDictionary <Long, String> > styleMap = new Dictionary <TextStyle, IDictionary <Long, String> >();

            if (field == ERA)
            {
                foreach (TextStyle textStyle in TextStyle.values())
                {
                    if (textStyle.Standalone)
                    {
                        // Stand-alone isn't applicable to era names.
                        continue;
                    }
                    IDictionary <String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", DateTime.ERA, textStyle.toCalendarStyle(), locale);
                    if (displayNames != null)
                    {
                        IDictionary <Long, String> map = new Dictionary <Long, String>();
                        foreach (Map_Entry <String, Integer> entry in displayNames)
                        {
                            map[(long)entry.Value] = entry.Key;
                        }
                        if (map.Count > 0)
                        {
                            styleMap[textStyle] = map;
                        }
                    }
                }
                return(new LocaleStore(styleMap));
            }

            if (field == MONTH_OF_YEAR)
            {
                foreach (TextStyle textStyle in TextStyle.values())
                {
                    IDictionary <String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", DateTime.MONTH, textStyle.toCalendarStyle(), locale);
                    IDictionary <Long, String>    map          = new Dictionary <Long, String>();
                    if (displayNames != null)
                    {
                        foreach (Map_Entry <String, Integer> entry in displayNames)
                        {
                            map[(long)(entry.Value + 1)] = entry.Key;
                        }
                    }
                    else
                    {
                        // Narrow names may have duplicated names, such as "J" for January, Jun, July.
                        // Get names one by one in that case.
                        for (int month = 1; month <= 12; month++)
                        {
                            String name;
                            name = CalendarDataUtility.retrieveJavaTimeFieldValueName("gregory", DateTime.MONTH, month, textStyle.toCalendarStyle(), locale);
                            if (name == null)
                            {
                                break;
                            }
                            map[(long)(month + 1)] = name;
                        }
                    }
                    if (map.Count > 0)
                    {
                        styleMap[textStyle] = map;
                    }
                }
                return(new LocaleStore(styleMap));
            }

            if (field == DAY_OF_WEEK)
            {
                foreach (TextStyle textStyle in TextStyle.values())
                {
                    IDictionary <String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", DateTime.DAY_OF_WEEK, textStyle.toCalendarStyle(), locale);
                    IDictionary <Long, String>    map          = new Dictionary <Long, String>();
                    if (displayNames != null)
                    {
                        foreach (Map_Entry <String, Integer> entry in displayNames)
                        {
                            map[(long)ToWeekDay(entry.Value)] = entry.Key;
                        }
                    }
                    else
                    {
                        // Narrow names may have duplicated names, such as "S" for Sunday and Saturday.
                        // Get names one by one in that case.
                        for (int wday = DayOfWeek.Sunday; wday <= DayOfWeek.Saturday; wday++)
                        {
                            String name;
                            name = CalendarDataUtility.retrieveJavaTimeFieldValueName("gregory", DateTime.DAY_OF_WEEK, wday, textStyle.toCalendarStyle(), locale);
                            if (name == null)
                            {
                                break;
                            }
                            map[(long)ToWeekDay(wday)] = name;
                        }
                    }
                    if (map.Count > 0)
                    {
                        styleMap[textStyle] = map;
                    }
                }
                return(new LocaleStore(styleMap));
            }

            if (field == AMPM_OF_DAY)
            {
                foreach (TextStyle textStyle in TextStyle.values())
                {
                    if (textStyle.Standalone)
                    {
                        // Stand-alone isn't applicable to AM/PM.
                        continue;
                    }
                    IDictionary <String, Integer> displayNames = CalendarDataUtility.retrieveJavaTimeFieldValueNames("gregory", DateTime.AM_PM, textStyle.toCalendarStyle(), locale);
                    if (displayNames != null)
                    {
                        IDictionary <Long, String> map = new Dictionary <Long, String>();
                        foreach (Map_Entry <String, Integer> entry in displayNames)
                        {
                            map[(long)entry.Value] = entry.Key;
                        }
                        if (map.Count > 0)
                        {
                            styleMap[textStyle] = map;
                        }
                    }
                }
                return(new LocaleStore(styleMap));
            }

            if (field == IsoFields.QUARTER_OF_YEAR)
            {
                // The order of keys must correspond to the TextStyle.values() order.
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String[] keys = { "QuarterNames", "standalone.QuarterNames", "QuarterAbbreviations", "standalone.QuarterAbbreviations", "QuarterNarrows", "standalone.QuarterNarrows"};
                String[] keys = new String[] { "QuarterNames", "standalone.QuarterNames", "QuarterAbbreviations", "standalone.QuarterAbbreviations", "QuarterNarrows", "standalone.QuarterNarrows" };
                for (int i = 0; i < keys.Length; i++)
                {
                    String[] names = GetLocalizedResource(keys[i], locale);
                    if (names != null)
                    {
                        IDictionary <Long, String> map = new Dictionary <Long, String>();
                        for (int q = 0; q < names.Length; q++)
                        {
                            map[(long)(q + 1)] = names[q];
                        }
                        styleMap[TextStyle.values()[i]] = map;
                    }
                }
                return(new LocaleStore(styleMap));
            }

            return("");            // null marker for map
        }