예제 #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);
        }
예제 #2
0
        public void TestDisplayName()
        {
            ULocale defLoc = IBM.ICU.Util.ULocale.GetDefault();

            IBM.ICU.Util.ULocale.SetDefault(_DEFAULT_LOCALE);
            for (int i = 0; i < _LOCALE_NUMBER; i++)
            {
                ULocale oldLoc = _LOCALES[i][0];
                ULocale newLoc = _LOCALES[i][1];

                for (int j = 0; j < available.Length; j++)
                {
                    String oldCountry = oldLoc.GetDisplayCountry(available[j]);
                    String newCountry = newLoc.GetDisplayCountry(available[j]);
                    String oldLang    = oldLoc.GetDisplayLanguage(available[j]);
                    String newLang    = newLoc.GetDisplayLanguage(available[j]);

                    // is there display name for the current country ID
                    if (!newCountry.Equals(newLoc.GetCountry()))
                    {
                        if (!oldCountry.Equals(newCountry))
                        {
                            Errln("getCountry() failed for " + oldLoc
                                  + " oldCountry= " + IBM.ICU.Charset.TestFmwk.Prettify(oldCountry)
                                  + " newCountry = " + IBM.ICU.Charset.TestFmwk.Prettify(newCountry)
                                  + " in display locale "
                                  + available[j].ToString());
                        }
                    }
                    // there is a display name for the current lang ID
                    if (!newLang.Equals(newLoc.GetLanguage()))
                    {
                        if (!oldLang.Equals(newLang))
                        {
                            Errln("getLanguage() failed for " + oldLoc
                                  + " oldLang = " + IBM.ICU.Charset.TestFmwk.Prettify(oldLang)
                                  + " newLang = " + IBM.ICU.Charset.TestFmwk.Prettify(newLang)
                                  + " in display locale "
                                  + available[j].ToString());
                        }
                    }
                }
            }
            IBM.ICU.Util.ULocale.SetDefault(defLoc);
        }
예제 #3
0
        /// <summary>
        /// Constructs a new Locale using the specified language, country, and
        /// variant codes.
        /// </summary>
        ///
        /// <param name="language"></param>
        /// <param name="country"></param>
        /// <param name="variant"></param>
        /// <exception cref="NullPointerException">if <c>language</c>, <c>country</c> or<c>variant</c> is <c>null</c>.</exception>
        public Locale(String language, String country, String variant)
        {
            if (language == null || country == null || variant == null)
            {
                throw new NullReferenceException();
            }
            if (language.Length == 0 && country.Length == 0)
            {
                languageCode = "";
                countryCode  = "";
                variantCode  = variant;
                return;
            }
            this.uLocale = new ULocale(language, country, variant);
            languageCode = uLocale.GetLanguage();
            // Map new language codes to the obsolete language
            // codes so the correct resource bundles will be used.
            if (languageCode.Equals("he"))
            {                        //$NON-NLS-1$
                languageCode = "iw"; //$NON-NLS-1$
            }
            else if (languageCode.Equals("id"))
            {                        //$NON-NLS-1$
                languageCode = "in"; //$NON-NLS-1$
            }
            else if (languageCode.Equals("yi"))
            {                        //$NON-NLS-1$
                languageCode = "ji"; //$NON-NLS-1$
            }

            // countryCode is defined in ASCII character set
            countryCode = (country.Length != 0) ? uLocale.GetCountry() : "";

            // Work around for be compatible with RI
            variantCode = variant;
        }
예제 #4
0
        public override IList <UiListItem> GetUiListCompareWholeItems(ISet <ULocale> localeSet, IComparer <UiListItem> comparer)
        {
            DisplayContext capContext = GetContext(DisplayContextType.Capitalization);

            List <UiListItem> result = new List <UiListItem>();
            IDictionary <ULocale, ISet <ULocale> > baseToLocales = new Dictionary <ULocale, ISet <ULocale> >();

            ULocale.Builder builder = new ULocale.Builder();
            foreach (ULocale locOriginal in localeSet)
            {
                builder.SetLocale(locOriginal); // verify well-formed. We do this here so that we consistently throw exception
                ULocale        loc     = ULocale.AddLikelySubtags(locOriginal);
                ULocale        @base   = new ULocale(loc.GetLanguage());
                ISet <ULocale> locales = baseToLocales.Get(@base);
                if (locales == null)
                {
                    baseToLocales[@base] = locales = new HashSet <ULocale>();
                }
                locales.Add(loc);
            }
            foreach (var entry in baseToLocales)
            {
                ULocale        @base  = entry.Key;
                ISet <ULocale> values = entry.Value;
                if (values.Count == 1)
                {
                    ULocale locale = values.First();
                    result.Add(NewRow(ULocale.MinimizeSubtags(locale, ULocale.Minimize.FAVOR_SCRIPT), capContext));
                }
                else
                {
                    ISet <string> scripts = new HashSet <string>();
                    ISet <string> regions = new HashSet <string>();
                    // need the follow two steps to make sure that unusual scripts or regions are displayed
                    ULocale maxBase = ULocale.AddLikelySubtags(@base);
                    scripts.Add(maxBase.GetScript());
                    regions.Add(maxBase.GetCountry());
                    foreach (ULocale locale in values)
                    {
                        scripts.Add(locale.GetScript());
                        regions.Add(locale.GetCountry());
                    }
                    bool hasScripts = scripts.Count > 1;
                    bool hasRegions = regions.Count > 1;
                    foreach (ULocale locale in values)
                    {
                        ULocale.Builder modified = builder.SetLocale(locale);
                        if (!hasScripts)
                        {
                            modified.SetScript("");
                        }
                        if (!hasRegions)
                        {
                            modified.SetRegion("");
                        }
                        result.Add(NewRow(modified.Build(), capContext));
                    }
                }
            }
            result.Sort(comparer);
            return(result);
        }
예제 #5
0
        // TODO: implement use of capitalization
        private string LocaleDisplayNameInternal(ULocale locale)
        {
            // lang
            // lang (script, country, variant, keyword=value, ...)
            // script, country, variant, keyword=value, ...

            string resultName = null;

            string lang = locale.GetLanguage();

            // Empty basename indicates root locale (keywords are ignored for this).
            // Our data uses 'root' to access display names for the root locale in the
            // "Languages" table.
            if (locale.GetBaseName().Length == 0)
            {
                lang = "root";
            }
            string script  = locale.GetScript();
            string country = locale.GetCountry();
            string variant = locale.GetVariant();

            bool hasScript  = script.Length > 0;
            bool hasCountry = country.Length > 0;
            bool hasVariant = variant.Length > 0;

            // always have a value for lang
            if (dialectHandling == DialectHandling.DIALECT_NAMES)
            {
                do
                { // loop construct is so we can break early out of search
                    if (hasScript && hasCountry)
                    {
                        string langScriptCountry = lang + '_' + script + '_' + country;
                        string result            = LocaleIdName(langScriptCountry);
                        if (result != null && !result.Equals(langScriptCountry))
                        {
                            resultName = result;
                            hasScript  = false;
                            hasCountry = false;
                            break;
                        }
                    }
                    if (hasScript)
                    {
                        string langScript = lang + '_' + script;
                        string result     = LocaleIdName(langScript);
                        if (result != null && !result.Equals(langScript))
                        {
                            resultName = result;
                            hasScript  = false;
                            break;
                        }
                    }
                    if (hasCountry)
                    {
                        string langCountry = lang + '_' + country;
                        string result      = LocaleIdName(langCountry);
                        if (result != null && !result.Equals(langCountry))
                        {
                            resultName = result;
                            hasCountry = false;
                            break;
                        }
                    }
                } while (false);
            }

            if (resultName == null)
            {
                string result = LocaleIdName(lang);
                if (result == null)
                {
                    return(null);
                }
                resultName = result
                             .Replace(formatOpenParen, formatReplaceOpenParen)
                             .Replace(formatCloseParen, formatReplaceCloseParen);
            }

            StringBuilder buf = new StringBuilder();

            if (hasScript)
            {
                // first element, don't need appendWithSep
                string result = ScriptDisplayNameInContext(script, true);
                if (result == null)
                {
                    return(null);
                }
                buf.Append(result
                           .Replace(formatOpenParen, formatReplaceOpenParen)
                           .Replace(formatCloseParen, formatReplaceCloseParen));
            }
            if (hasCountry)
            {
                string result = RegionDisplayName(country, true);
                if (result == null)
                {
                    return(null);
                }
                AppendWithSep(result
                              .Replace(formatOpenParen, formatReplaceOpenParen)
                              .Replace(formatCloseParen, formatReplaceCloseParen), buf);
            }
            if (hasVariant)
            {
                string result = VariantDisplayName(variant, true);
                if (result == null)
                {
                    return(null);
                }
                AppendWithSep(result
                              .Replace(formatOpenParen, formatReplaceOpenParen)
                              .Replace(formatCloseParen, formatReplaceCloseParen), buf);
            }

            using (IEnumerator <string> keys = locale.GetKeywords())
            {
                if (keys != null)
                {
                    while (keys.MoveNext())
                    {
                        string key            = keys.Current;
                        string value          = locale.GetKeywordValue(key);
                        string keyDisplayName = KeyDisplayName(key, true);
                        if (keyDisplayName == null)
                        {
                            return(null);
                        }
                        keyDisplayName = keyDisplayName
                                         .Replace(formatOpenParen, formatReplaceOpenParen)
                                         .Replace(formatCloseParen, formatReplaceCloseParen);
                        string valueDisplayName = KeyValueDisplayName(key, value, true);
                        if (valueDisplayName == null)
                        {
                            return(null);
                        }
                        valueDisplayName = valueDisplayName
                                           .Replace(formatOpenParen, formatReplaceOpenParen)
                                           .Replace(formatCloseParen, formatReplaceCloseParen);
                        if (!valueDisplayName.Equals(value))
                        {
                            AppendWithSep(valueDisplayName, buf);
                        }
                        else if (!key.Equals(keyDisplayName))
                        {
                            string keyValue = SimpleFormatterImpl.FormatCompiledPattern(
                                keyTypeFormat, keyDisplayName, valueDisplayName);
                            AppendWithSep(keyValue, buf);
                        }
                        else
                        {
                            AppendWithSep(keyDisplayName, buf)
                            .Append("=")
                            .Append(valueDisplayName);
                        }
                    }
                }
            }
            string resultRemainder = null;

            if (buf.Length > 0)
            {
                resultRemainder = buf.ToString();
            }

            if (resultRemainder != null)
            {
                resultName = SimpleFormatterImpl.FormatCompiledPattern(
                    format, resultName, resultRemainder);
            }

            return(AdjustForUsageAndContext(CapitalizationContextUsage.LANGUAGE, resultName));
        }