Esempio n. 1
0
        /// <summary>
        /// Instantiate a currency from a resource bundle found in Locale loc.
        /// </summary>
        ///
        /* package */ static internal Currency CreateCurrency(ULocale loc)
        {
            String country   = loc.GetCountry();
            String variant   = loc.GetVariant();
            bool   isPreEuro = variant.Equals("PREEURO");
            bool   isEuro    = variant.Equals("EURO");
            // TODO: ICU4C has service registration, and the currency is requested
            // from the service here.
            ICUResourceBundle bundle = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle
                                       .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME,
                                                          "supplementalData",
                                                          IBM.ICU.Impl.ICUResourceBundle.ICU_DATA_CLASS_LOADER);

            if (bundle == null)
            {
                // throw new MissingResourceException()
                return(null);
            }
            try {
                UResourceBundle cm           = bundle.Get("CurrencyMap");
                String          curriso      = null;
                UResourceBundle countryArray = cm.Get(country);
                // Some regions can have more than one current currency in use.
                // The latest default currency is always the first one.
                UResourceBundle currencyReq = countryArray.Get(0);
                curriso = currencyReq.GetString("id");
                if (isPreEuro && curriso.Equals(EUR_STR))
                {
                    currencyReq = countryArray.Get(1);
                    curriso     = currencyReq.GetString("id");
                }
                else if (isEuro)
                {
                    curriso = EUR_STR;
                }
                if (curriso != null)
                {
                    return(new Currency(curriso));
                }
            } catch (MissingManifestResourceException ex) {
                // We don't know about this region.
                // As of CLDR 1.5.1, the data includes deprecated region history
                // too.
                // So if we get here, either the region doesn't exist, or the data
                // is really bad.
                // Deprecated regions should return the last valid currency for that
                // region in the data.
                // We don't try to resolve it to a new region.
            }
            return(null);
        }
Esempio n. 2
0
 /// <summary>
 /// Returns the next element of this iterator if this iterator object has at
 /// least one more element to provide
 /// </summary>
 ///
 /// <returns>the UResourceBundle object</returns>
 /// <exception cref="NoSuchElementException"></exception>
 /// @draft ICU 3.8
 /// @provisional This API might change or be removed in a future release.
 public UResourceBundle Next()
 {
     if (index < size)
     {
         return(bundle.Get(index++));
     }
     throw new InvalidOperationException();
 }
Esempio n. 3
0
        /// <summary>
        /// Returns the display name for the given currency in the given locale. For
        /// example, the display name for the USD currency object in the en_US locale
        /// is "$".
        /// </summary>
        ///
        /// <param name="locale">locale in which to display currency</param>
        /// <param name="nameStyle">selector for which kind of name to return</param>
        /// <param name="isChoiceFormat">fill-in; isChoiceFormat[0] is set to true if the returnedvalue is a ChoiceFormat pattern; otherwise it is set to false</param>
        /// <returns>display string for this currency. If the resource data contains
        /// no entry for this currency, then the ISO 4217 code is returned.
        /// If isChoiceFormat[0] is true, then the result is a ChoiceFormat
        /// pattern. Otherwise it is a static string.</returns>
        /// @stable ICU 3.2
        public String GetName(ULocale locale, int nameStyle,
                              bool[] isChoiceFormat)
        {
            // Look up the Currencies resource for the given locale. The
            // Currencies locale data looks like this:
            // |en {
            // | Currencies {
            // | USD { "US$", "US Dollar" }
            // | CHF { "Sw F", "Swiss Franc" }
            // | INR { "=0#Rs|1#Re|1<Rs", "=0#Rupees|1#Rupee|1<Rupees" }
            // | //...
            // | }
            // |}

            if (nameStyle < 0 || nameStyle > 1)
            {
                throw new ArgumentException();
            }

            String s = null;

            try {
                UResourceBundle rb = IBM.ICU.Util.UResourceBundle.GetBundleInstance(
                    IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale);
                ICUResourceBundle currencies = (ICUResourceBundle)rb
                                               .Get("Currencies");

                // Fetch resource with multi-level resource inheritance fallback
                s = currencies.GetWithFallback(isoCode).GetString(nameStyle);
            } catch (MissingManifestResourceException e) {
                // TODO what should be done here?
            }

            // Determine if this is a ChoiceFormat pattern. One leading mark
            // indicates a ChoiceFormat. Two indicates a static string that
            // starts with a mark. In either case, the first mark is ignored,
            // if present. Marks in the rest of the string have no special
            // meaning.
            isChoiceFormat[0] = false;
            if (s != null)
            {
                int i = 0;
                while (i < s.Length && s[i] == '=' && i < 2)
                {
                    ++i;
                }
                isChoiceFormat[0] = (i == 1);
                if (i != 0)
                {
                    // Skip over first mark
                    s = s.Substring(1);
                }
                return(s);
            }

            // If we fail to find a match, use the ISO 4217 code
            return(isoCode);
        }
Esempio n. 4
0
        /// <summary>
        /// Returns the size of paper used in the locale. The paper sizes returned
        /// are always in <em> milli-meters<em>.
        /// </summary>
        ///
        /// <param name="locale">The locale for which the measurement system to be retrieved.</param>
        /// <returns>The paper size used in the locale</returns>
        /// @stable ICU 3.0
        public static LocaleData.PaperSize  GetPaperSize(ULocale locale)
        {
            UResourceBundle bundle_0 = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle
                                       .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale);
            UResourceBundle obj = bundle_0.Get(PAPER_SIZE);

            int[] size = obj.GetIntVector();
            return(new LocaleData.PaperSize(size[0], size[1]));
        }
Esempio n. 5
0
        /// <summary>
        /// Internal function to look up currency data. Result is an array of two
        /// Integers. The first is the fraction digits. The second is the rounding
        /// increment, or 0 if none. The rounding increment is in units of
        /// 10^(-fraction_digits).
        /// </summary>
        ///
        private int[] FindData()
        {
            try {
                // Get CurrencyMeta resource out of root locale file. [This may
                // move out of the root locale file later; if it does, update this
                // code.]
                UResourceBundle root = IBM.ICU.Util.UResourceBundle.GetBundleInstance(
                    IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, "supplementalData",
                    IBM.ICU.Impl.ICUResourceBundle.ICU_DATA_CLASS_LOADER);
                UResourceBundle currencyMeta = root.Get("CurrencyMeta");

                // Integer[] i = null;
                // int defaultPos = -1;
                int[] i = currencyMeta.Get(isoCode).GetIntVector();

                // Do a linear search for isoCode. At the same time,
                // record the position of the DEFAULT meta data. If the
                // meta data becomes large, make this faster.

                /*
                 * for (int j=0; j<currencyMeta.length; ++j) { Object[] row =
                 * currencyMeta[j]; String s = (String) row[0]; int c =
                 * isoCode.compareToIgnoreCase(s); if (c == 0) { i = (Integer[])
                 * row[1]; break; } if ("DEFAULT".equalsIgnoreCase(s)) { defaultPos
                 * = j; } if (c < 0 && defaultPos >= 0) { break; } }
                 */
                if (i == null)
                {
                    i = currencyMeta.Get("DEFAULT").GetIntVector();
                }

                if (i != null && i.Length >= 2)
                {
                    return(i);
                }
            } catch (MissingManifestResourceException e) {
            }

            // Config/build error; return hard-coded defaults
            return(LAST_RESORT_DATA);
        }
Esempio n. 6
0
        /// <summary>
        /// Returns the measurement system used in the locale specified by the
        /// locale.
        /// </summary>
        ///
        /// <param name="locale">The locale for which the measurement system to be retrieved.</param>
        /// <returns>MeasurementSystem the measurement system used in the locale.</returns>
        /// @stable ICU 3.0
        public static LocaleData.MeasurementSystem  GetMeasurementSystem(ULocale locale)
        {
            UResourceBundle bundle_0 = (ICUResourceBundle)IBM.ICU.Util.UResourceBundle
                                       .GetBundleInstance(IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale);
            UResourceBundle sysBundle = bundle_0.Get(MEASUREMENT_SYSTEM);

            int system = sysBundle.GetInt();

            if (IBM.ICU.Util.LocaleData.MeasurementSystem.US.Equals(system))
            {
                return(IBM.ICU.Util.LocaleData.MeasurementSystem.US);
            }
            if (IBM.ICU.Util.LocaleData.MeasurementSystem.SI.Equals(system))
            {
                return(IBM.ICU.Util.LocaleData.MeasurementSystem.SI);
            }
            // return null if the object is null or is not an instance
            // of integer indicating an error
            return(null);
        }
Esempio n. 7
0
        /// <summary>
        /// Returns the resource in a given resource at the specified index.
        /// </summary>
        ///
        /// <param name="index">an index to the wanted resource.</param>
        /// <returns>the sub resource UResourceBundle object</returns>
        /// <exception cref="IndexOutOfBoundsException"></exception>
        /// <exception cref="MissingResourceException"></exception>
        /// @draft ICU 3.8
        /// @provisional This API might change or be removed in a future release.
        public UResourceBundle Get(int index)
        {
            UResourceBundle obj = HandleGet(index, null, this);

            if (obj == null)
            {
                obj = (ICUResourceBundle)GetParent();
                if (obj != null)
                {
                    obj = obj.Get(index);
                }
                if (obj == null)
                {
                    throw new MissingManifestResourceException("Can't find resource for bundle "
                                                               + ((object)this).GetType().FullName + ", key "
                                                               + GetKey());
                }
            }
            IBM.ICU.Impl.ICUResourceBundle.SetLoadingStatus(obj, GetLocaleID());
            return(obj);
        }
Esempio n. 8
0
        /// <exclude/>
        /// <summary>
        /// Attempt to parse the given string as a currency, either as a display name
        /// in the given locale, or as a 3-letter ISO 4217 code. If multiple display
        /// names match, then the longest one is selected. If both a display name and
        /// a 3-letter ISO code match, then the display name is preferred, unless
        /// it's length is less than 3.
        /// </summary>
        ///
        /// <param name="locale">the locale of the display names to match</param>
        /// <param name="text">the text to parse</param>
        /// <param name="pos">input-output position; on input, the position within text tomatch; must have 0 <= pos.getIndex() < text.length(); onoutput, the position after the last matched character. If theparse fails, the position in unchanged upon output.</param>
        /// <returns>the ISO 4217 code, as a string, of the best match, or null if
        /// there is no match</returns>
        public static String Parse(ULocale locale, String text, ILOG.J2CsMapping.Text.ParsePosition pos)
        {
            // TODO: There is a slight problem with the pseudo-multi-level
            // fallback implemented here. More-specific locales don't
            // properly shield duplicate entries in less-specific locales.
            // This problem will go away when real multi-level fallback is
            // implemented. We could also fix this by recording (in a
            // hash) which codes are used at each level of fallback, but
            // this doesn't seem warranted.

            int    start    = pos.GetIndex();
            String fragment = text.Substring(start);

            String iso = null;
            int    max = 0;

            // Look up the Currencies resource for the given locale. The
            // Currencies locale data looks like this:
            // |en {
            // | Currencies {
            // | USD { "US$", "US Dollar" }
            // | CHF { "Sw F", "Swiss Franc" }
            // | INR { "=0#Rs|1#Re|1<Rs", "=0#Rupees|1#Rupee|1<Rupees" }
            // | //...
            // | }
            // |}

            // In the future, resource bundles may implement multi-level
            // fallback. That is, if a currency is not found in the en_US
            // Currencies data, then the en Currencies data will be searched.
            // Currently, if a Currencies datum exists in en_US and en, the
            // en_US entry hides that in en.

            // We want multi-level fallback for this resource, so we implement
            // it manually.

            // Multi-level resource inheritance fallback loop

            while (locale != null)
            {
                UResourceBundle rb = IBM.ICU.Util.UResourceBundle.GetBundleInstance(
                    IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale);
                // We can't cast this to String[][]; the cast has to happen later

                try {
                    UResourceBundle currencies = rb.Get("Currencies");
                    // Do a linear search
                    for (int i = 0; i < currencies.GetSize(); ++i)
                    {
                        // String name = ((String[]) currencies[i][1])[0];
                        UResourceBundle item = currencies.Get(i);
                        String          name = item.GetString(0);
                        if (name.Length < 1)
                        {
                            // Ignore zero-length names -- later, change this
                            // when zero-length is used to mean something.
                            continue;
                        }
                        else if (name[0] == '=')
                        {
                            name = name.Substring(1);
                            if (name.Length > 0 && name[0] != '=')
                            {
                                ChoiceFormat choice = new ChoiceFormat(name);
                                // Number n =
                                choice.Parse(text, pos);
                                int len = pos.GetIndex() - start;
                                if (len > max)
                                {
                                    iso = item.GetKey();
                                    max = len;
                                }
                                pos.SetIndex(start);
                                continue;
                            }
                        }
                        if (name.Length > max && fragment.StartsWith(name))
                        {
                            iso = item.GetKey();
                            max = name.Length;
                        }
                    }
                } catch (MissingManifestResourceException e) {
                }

                locale = locale.GetFallback();
            }

            /*
             * 1. Look at the Currencies array from the locale 1a. Iterate through
             * it, and check each row to see if row[1] matches 1a1. If row[1] is a
             * pattern, use ChoiceFormat to attempt a parse 1b. Upon a match, return
             * the ISO code stored at row[0] 2. If there is no match, fall back to
             * "en" and try again 3. If there is no match, fall back to root and try
             * again 4. If still no match, parse 3-letter ISO {this code is probably
             * unchanged}.
             *
             * ICUResourceBundle rb =
             * (ICUResourceBundle)UResourceBundle.getBundleInstance
             * (UResourceBundle.ICU_BASE_NAME, locale); ICUResourceBundle currencies
             * = rb.get("Currencies");
             */
            // If display name parse fails or if it matches fewer than 3
            // characters, try to parse 3-letter ISO. Do this after the
            // display name processing so 3-letter display names are
            // preferred. Consider /[A-Z]{3}/ to be valid ISO, and parse
            // it manually--UnicodeSet/regex are too slow and heavy.
            if (max < 3 && (text.Length - start) >= 3)
            {
                bool valid = true;
                for (int k = 0; k < 3; ++k)
                {
                    char ch = text[start + k];     // 16-bit ok
                    if (ch < 'A' || ch > 'Z')
                    {
                        valid = false;
                        break;
                    }
                }
                if (valid)
                {
                    iso = text.Substring(start, (start + 3) - (start));
                    max = 3;
                }
            }

            pos.SetIndex(start + max);
            return(iso);
        }