Exemplo n.º 1
0
        /// <summary>
        /// Creates a DateFormat with the given time and/or date style in the given
        /// locale. </summary>
        /// <param name="timeStyle"> a value from 0 to 3 indicating the time format,
        /// ignored if flags is 2 </param>
        /// <param name="dateStyle"> a value from 0 to 3 indicating the time format,
        /// ignored if flags is 1 </param>
        /// <param name="flags"> either 1 for a time format, 2 for a date format,
        /// or 3 for a date/time format </param>
        /// <param name="loc"> the locale for the format </param>
        private static DateFormat Get(int timeStyle, int dateStyle, int flags, Locale loc)
        {
            if ((flags & 1) != 0)
            {
                if (timeStyle < 0 || timeStyle > 3)
                {
                    throw new IllegalArgumentException("Illegal time style " + timeStyle);
                }
            }
            else
            {
                timeStyle = -1;
            }
            if ((flags & 2) != 0)
            {
                if (dateStyle < 0 || dateStyle > 3)
                {
                    throw new IllegalArgumentException("Illegal date style " + dateStyle);
                }
            }
            else
            {
                dateStyle = -1;
            }

            LocaleProviderAdapter adapter    = LocaleProviderAdapter.getAdapter(typeof(DateFormatProvider), loc);
            DateFormat            dateFormat = Get(adapter, timeStyle, dateStyle, loc);

            if (dateFormat == null)
            {
                dateFormat = Get(LocaleProviderAdapter.forJRE(), timeStyle, dateStyle, loc);
            }
            return(dateFormat);
        }
Exemplo n.º 2
0
        private static DateFormatSymbols GetProviderInstance(Locale locale)
        {
            LocaleProviderAdapter     adapter  = LocaleProviderAdapter.getAdapter(typeof(DateFormatSymbolsProvider), locale);
            DateFormatSymbolsProvider provider = adapter.DateFormatSymbolsProvider;
            DateFormatSymbols         dfsyms   = provider.GetInstance(locale);

            if (dfsyms == null)
            {
                provider = LocaleProviderAdapter.forJRE().DateFormatSymbolsProvider;
                dfsyms   = provider.GetInstance(locale);
            }
            return(dfsyms);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the <code>DecimalFormatSymbols</code> instance for the specified
        /// locale.  This method provides access to <code>DecimalFormatSymbols</code>
        /// instances for locales supported by the Java runtime itself as well
        /// as for those supported by installed
        /// {@link java.text.spi.DecimalFormatSymbolsProvider
        /// DecimalFormatSymbolsProvider} implementations.
        /// If the specified locale contains the <seealso cref="java.util.Locale#UNICODE_LOCALE_EXTENSION"/>
        /// for the numbering system, the instance is initialized with the specified numbering
        /// system if the JRE implementation supports it. For example,
        /// <pre>
        /// NumberFormat.getNumberInstance(Locale.forLanguageTag("th-TH-u-nu-thai"))
        /// </pre>
        /// This may return a {@code NumberFormat} instance with the Thai numbering system,
        /// instead of the Latin numbering system.
        /// </summary>
        /// <param name="locale"> the desired locale. </param>
        /// <returns> a <code>DecimalFormatSymbols</code> instance. </returns>
        /// <exception cref="NullPointerException"> if <code>locale</code> is null
        /// @since 1.6 </exception>
        public static DecimalFormatSymbols GetInstance(Locale locale)
        {
            LocaleProviderAdapter adapter;

            adapter = LocaleProviderAdapter.getAdapter(typeof(DecimalFormatSymbolsProvider), locale);
            DecimalFormatSymbolsProvider provider = adapter.DecimalFormatSymbolsProvider;
            DecimalFormatSymbols         dfsyms   = provider.GetInstance(locale);

            if (dfsyms == null)
            {
                provider = LocaleProviderAdapter.forJRE().DecimalFormatSymbolsProvider;
                dfsyms   = provider.GetInstance(locale);
            }
            return(dfsyms);
        }
Exemplo n.º 4
0
        private static DateFormat Get(LocaleProviderAdapter adapter, int timeStyle, int dateStyle, Locale loc)
        {
            DateFormatProvider provider = adapter.DateFormatProvider;
            DateFormat         dateFormat;

            if (timeStyle == -1)
            {
                dateFormat = provider.GetDateInstance(dateStyle, loc);
            }
            else
            {
                if (dateStyle == -1)
                {
                    dateFormat = provider.GetTimeInstance(timeStyle, loc);
                }
                else
                {
                    dateFormat = provider.GetDateTimeInstance(dateStyle, timeStyle, loc);
                }
            }
            return(dateFormat);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the Collator for the desired locale. </summary>
        /// <param name="desiredLocale"> the desired locale. </param>
        /// <returns> the Collator for the desired locale. </returns>
        /// <seealso cref= java.util.Locale </seealso>
        /// <seealso cref= java.util.ResourceBundle </seealso>
        public static Collator GetInstance(Locale desiredLocale)
        {
            SoftReference <Collator> @ref = Cache[desiredLocale];
            Collator result = (@ref != null) ? @ref.get() : null;

            if (result == null)
            {
                LocaleProviderAdapter adapter;
                adapter = LocaleProviderAdapter.getAdapter(typeof(CollatorProvider), desiredLocale);
                CollatorProvider provider = adapter.CollatorProvider;
                result = provider.GetInstance(desiredLocale);
                if (result == null)
                {
                    result = LocaleProviderAdapter.forJRE().CollatorProvider.getInstance(desiredLocale);
                }
                while (true)
                {
                    if (@ref != null)
                    {
                        // Remove the empty SoftReference if any
                        Cache.Remove(desiredLocale, @ref);
                    }
                    @ref = Cache.PutIfAbsent(desiredLocale, new SoftReference <>(result));
                    if (@ref == null)
                    {
                        break;
                    }
                    Collator cachedColl = @ref.get();
                    if (cachedColl != null)
                    {
                        result = cachedColl;
                        break;
                    }
                }
            }
            return((Collator)result.Clone());             // make the world safe
        }
Exemplo n.º 6
0
        private void InitializeData(Locale desiredLocale)
        {
            Locale = desiredLocale;

            // Copy values of a cached instance if any.
            SoftReference <DateFormatSymbols> @ref = CachedInstances[Locale];
            DateFormatSymbols dfs;

            if (@ref != null && (dfs = @ref.get()) != null)
            {
                CopyMembers(dfs, this);
                return;
            }

            // Initialize the fields from the ResourceBundle for locale.
            LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(typeof(DateFormatSymbolsProvider), Locale);

            // Avoid any potential recursions
            if (!(adapter is ResourceBundleBasedAdapter))
            {
                adapter = LocaleProviderAdapter.ResourceBundleBased;
            }
            ResourceBundle resource = ((ResourceBundleBasedAdapter)adapter).LocaleData.getDateFormatData(Locale);

            // JRE and CLDR use different keys
            // JRE: Eras, short.Eras and narrow.Eras
            // CLDR: long.Eras, Eras and narrow.Eras
            if (resource.ContainsKey("Eras"))
            {
                Eras_Renamed = resource.GetStringArray("Eras");
            }
            else if (resource.ContainsKey("long.Eras"))
            {
                Eras_Renamed = resource.GetStringArray("long.Eras");
            }
            else if (resource.ContainsKey("short.Eras"))
            {
                Eras_Renamed = resource.GetStringArray("short.Eras");
            }
            Months_Renamed      = resource.GetStringArray("MonthNames");
            ShortMonths_Renamed = resource.GetStringArray("MonthAbbreviations");
            Ampms = resource.GetStringArray("AmPmMarkers");
            LocalPatternChars_Renamed = resource.GetString("DateTimePatternChars");

            // Day of week names are stored in a 1-based array.
            Weekdays_Renamed      = ToOneBasedArray(resource.GetStringArray("DayNames"));
            ShortWeekdays_Renamed = ToOneBasedArray(resource.GetStringArray("DayAbbreviations"));

            // Put a clone in the cache
            @ref = new SoftReference <>((DateFormatSymbols)this.Clone());
            SoftReference <DateFormatSymbols> x = CachedInstances.PutIfAbsent(Locale, @ref);

            if (x != null)
            {
                DateFormatSymbols y = x.get();
                if (y == null)
                {
                    // Replace the empty SoftReference with ref.
                    CachedInstances[Locale] = @ref;
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes the symbols from the FormatData resource bundle.
        /// </summary>
        private void Initialize(Locale locale)
        {
            this.Locale = locale;

            // get resource bundle data
            LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(typeof(DecimalFormatSymbolsProvider), locale);

            // Avoid potential recursions
            if (!(adapter is ResourceBundleBasedAdapter))
            {
                adapter = LocaleProviderAdapter.ResourceBundleBased;
            }
            Object[] data           = adapter.getLocaleResources(locale).DecimalFormatSymbolsData;
            String[] numberElements = (String[])data[0];

            DecimalSeparator_Renamed  = numberElements[0].CharAt(0);
            GroupingSeparator_Renamed = numberElements[1].CharAt(0);
            PatternSeparator_Renamed  = numberElements[2].CharAt(0);
            Percent_Renamed           = numberElements[3].CharAt(0);
            ZeroDigit_Renamed         = numberElements[4].CharAt(0);     //different for Arabic,etc.
            Digit_Renamed             = numberElements[5].CharAt(0);
            MinusSign_Renamed         = numberElements[6].CharAt(0);
            Exponential          = numberElements[7].CharAt(0);
            ExponentialSeparator = numberElements[7];             //string representation new since 1.6
            PerMill_Renamed      = numberElements[8].CharAt(0);
            Infinity_Renamed     = numberElements[9];
            NaN_Renamed          = numberElements[10];

            // Try to obtain the currency used in the locale's country.
            // Check for empty country string separately because it's a valid
            // country ID for Locale (and used for the C locale), but not a valid
            // ISO 3166 country code, and exceptions are expensive.
            if (locale.Country.Length() > 0)
            {
                try
                {
                    Currency_Renamed = Currency.GetInstance(locale);
                }
                catch (IllegalArgumentException)
                {
                    // use default values below for compatibility
                }
            }
            if (Currency_Renamed != null)
            {
                IntlCurrencySymbol = Currency_Renamed.CurrencyCode;
                if (data[1] != null && data[1] == IntlCurrencySymbol)
                {
                    CurrencySymbol_Renamed = (String)data[2];
                }
                else
                {
                    CurrencySymbol_Renamed = Currency_Renamed.GetSymbol(locale);
                    data[1] = IntlCurrencySymbol;
                    data[2] = CurrencySymbol_Renamed;
                }
            }
            else
            {
                // default values
                IntlCurrencySymbol = "XXX";
                try
                {
                    Currency_Renamed = Currency.GetInstance(IntlCurrencySymbol);
                }
                catch (IllegalArgumentException)
                {
                }
                CurrencySymbol_Renamed = "\u00A4";
            }
            // Currently the monetary decimal separator is the same as the
            // standard decimal separator for all locales that we support.
            // If that changes, add a new entry to NumberElements.
            MonetarySeparator = DecimalSeparator_Renamed;
        }