Esempio n. 1
0
 public LocaleDisplayNames Get(ULocale locale, DialectHandling dialectHandling)
 {
     if (!(dialectHandling == this.dialectHandling && DisplayContext.CapitalizationNone == this.capitalization &&
           DisplayContext.LengthFull == this.nameLength && DisplayContext.Substitute == this.substituteHandling &&
           locale.Equals(this.locale)))
     {
         this.locale             = locale;
         this.dialectHandling    = dialectHandling;
         this.capitalization     = DisplayContext.CapitalizationNone;
         this.nameLength         = DisplayContext.LengthFull;
         this.substituteHandling = DisplayContext.Substitute;
         this.cache = new LocaleDisplayNamesImpl(locale, dialectHandling);
     }
     return(cache);
 }
Esempio n. 2
0
            public LocaleDisplayNames Get(ULocale locale, params DisplayContext[] contexts)
            {
                DialectHandling dialectHandlingIn  = DialectHandling.STANDARD_NAMES;
                DisplayContext  capitalizationIn   = DisplayContext.CapitalizationNone;
                DisplayContext  nameLengthIn       = DisplayContext.LengthFull;
                DisplayContext  substituteHandling = DisplayContext.Substitute;

                foreach (DisplayContext contextItem in contexts)
                {
                    switch (contextItem.Type())
                    {
                    case DisplayContextType.DialectHandling:
                        dialectHandlingIn = (contextItem.Value() == DisplayContext.StandardNames.Value()) ?
                                            DialectHandling.STANDARD_NAMES : DialectHandling.DIALECT_NAMES;
                        break;

                    case DisplayContextType.Capitalization:
                        capitalizationIn = contextItem;
                        break;

                    case DisplayContextType.DisplayLength:
                        nameLengthIn = contextItem;
                        break;

                    case DisplayContextType.SubstituteHandling:
                        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);
            }
Esempio n. 3
0
        public void TestIllformedLocale()
        {
            ULocale            french   = ULocale.FRENCH;
            Collator           collator = Collator.GetInstance(french);
            LocaleDisplayNames names    = LocaleDisplayNames.GetInstance(french,
                                                                         DisplayContext.CapitalizationForUIListOrMenu);

            foreach (String malformed in new string[] { "en-a", "$", "ü--a", "en--US" })
            {
                try
                {
                    ISet <ULocale> supported = ImmutableHashSet.Create(new ULocale(malformed)); //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.ToString());
                }
            }
        }
Esempio n. 4
0
 public LocaleDisplayNames Get(ULocale locale, DialectHandling dialectHandling)
 {
     syncLock.EnterUpgradeableReadLock();
     try
     {
         if (!(dialectHandling == this.dialectHandling && DisplayContext.CapitalizationNone == this.capitalization &&
               DisplayContext.LengthFull == this.nameLength && DisplayContext.Substitute == this.substituteHandling &&
               locale.Equals(this.locale)))
         {
             syncLock.EnterWriteLock();
             try
             {
                 if (!(dialectHandling == this.dialectHandling && DisplayContext.CapitalizationNone == this.capitalization &&
                       DisplayContext.LengthFull == this.nameLength && DisplayContext.Substitute == this.substituteHandling &&
                       locale.Equals(this.locale)))
                 {
                     this.locale             = locale;
                     this.dialectHandling    = dialectHandling;
                     this.capitalization     = DisplayContext.CapitalizationNone;
                     this.nameLength         = DisplayContext.LengthFull;
                     this.substituteHandling = DisplayContext.Substitute;
                     this.cache = new LocaleDisplayNamesImpl(locale, dialectHandling);
                 }
             }
             finally
             {
                 syncLock.ExitWriteLock();
             }
         }
         return(cache);
     }
     finally
     {
         syncLock.ExitUpgradeableReadLock();
     }
 }
Esempio n. 5
0
            public LocaleDisplayNames Get(ULocale locale, params DisplayContext[] contexts)
            {
                DialectHandling dialectHandlingIn  = DialectHandling.StandardNames;
                DisplayContext  capitalizationIn   = DisplayContext.CapitalizationNone;
                DisplayContext  nameLengthIn       = DisplayContext.LengthFull;
                DisplayContext  substituteHandling = DisplayContext.Substitute;

                foreach (DisplayContext contextItem in contexts)
                {
                    switch (contextItem.Type())
                    {
                    case DisplayContextType.DialectHandling:
                        dialectHandlingIn = (contextItem.Value() == DisplayContext.StandardNames.Value()) ?
                                            DialectHandling.StandardNames : DialectHandling.DialectNames;
                        break;

                    case DisplayContextType.Capitalization:
                        capitalizationIn = contextItem;
                        break;

                    case DisplayContextType.DisplayLength:
                        nameLengthIn = contextItem;
                        break;

                    case DisplayContextType.SubstituteHandling:
                        substituteHandling = contextItem;
                        break;

                    default:
                        break;
                    }
                }
                syncLock.EnterUpgradeableReadLock();
                try
                {
                    if (!(dialectHandlingIn == this.dialectHandling && capitalizationIn == this.capitalization &&
                          nameLengthIn == this.nameLength && substituteHandling == this.substituteHandling &&
                          locale.Equals(this.locale)))
                    {
                        syncLock.EnterWriteLock();
                        try
                        {
                            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);
                            }
                        }
                        finally
                        {
                            syncLock.ExitWriteLock();
                        }
                    }
                    return(cache);
                }
                finally
                {
                    syncLock.ExitUpgradeableReadLock();
                }
            }
Esempio n. 6
0
        public void TestNameList()
        {
            string[][][] tests =
            {
                /* name in French, name in self, minimized, modified */
                new string[][]                { new string[] { "fr-Cyrl-BE", "fr-Cyrl-CA" },
                                                new string[] { "Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE" },
                                                new string[] { "Français (cyrillique, Canada)", "Français (cyrillique, Canada)", "fr_Cyrl_CA", "fr_Cyrl_CA" }, },
                new string[][]                { new string[] { "en",         "de", "fr", "zh"},
                                                new string[] { "Allemand", "Deutsch", "de", "de" },
                                                new string[] { "Anglais", "English", "en", "en" },
                                                new string[] { "Chinois", "中文", "zh", "zh" },
                                                new string[] { "Français", "Français", "fr", "fr" }, },
                // some non-canonical names
                new string[][]                { new string[] { "iw",         "iw-US", "no", "no-Cyrl", "in", "in-YU"},
                                                new string[] { "Hébreu (États-Unis)", "עברית (ארצות הברית)", "iw_US", "iw_US" },
                                                new string[] { "Hébreu (Israël)", "עברית (ישראל)", "iw", "iw_IL" },
                                                new string[] { "Indonésien (Indonésie)", "Indonesia (Indonesia)", "in", "in_ID" },
                                                new string[] { "Indonésien (Serbie)", "Indonesia (Serbia)", "in_YU", "in_YU" },
                                                new string[] { "Norvégien (cyrillique)", "Norsk (kyrillisk)", "no_Cyrl", "no_Cyrl" },
                                                new string[] { "Norvégien (latin)", "Norsk (latinsk)", "no", "no_Latn" }, },
                new string[][]                { new string[] { "zh-Hant-TW", "en", "en-gb", "fr", "zh-Hant", "de", "de-CH", "zh-TW"},
                                                new string[] { "Allemand (Allemagne)", "Deutsch (Deutschland)", "de", "de_DE" },
                                                new string[] { "Allemand (Suisse)", "Deutsch (Schweiz)", "de_CH", "de_CH" },
                                                new string[] { "Anglais (États-Unis)", "English (United States)", "en", "en_US" },
                                                new string[] { "Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB" },
                                                new string[] { "Chinois (traditionnel)", "中文(繁體)", "zh_Hant", "zh_Hant" },
                                                new string[] { "Français", "Français", "fr", "fr" }, },
                new string[][]                { new string[] { "zh",         "en-gb", "en-CA", "fr-Latn-FR"},
                                                new string[] { "Anglais (Canada)", "English (Canada)", "en_CA", "en_CA" },
                                                new string[] { "Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB" },
                                                new string[] { "Chinois", "中文", "zh", "zh" },
                                                new string[] { "Français", "Français", "fr", "fr" }, },
                new string[][]                { new string[] { "en-gb",      "fr", "zh-Hant", "zh-SG", "sr", "sr-Latn"},
                                                new string[] { "Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB" },
                                                new string[] { "Chinois (simplifié, Singapour)", "中文(简体,新加坡)", "zh_SG", "zh_Hans_SG" },
                                                new string[] { "Chinois (traditionnel, Taïwan)", "中文(繁體,台灣)", "zh_Hant", "zh_Hant_TW" },
                                                new string[] { "Français", "Français", "fr", "fr" },
                                                new string[] { "Serbe (cyrillique)", "Српски (ћирилица)", "sr", "sr_Cyrl" },
                                                new string[] { "Serbe (latin)", "Srpski (latinica)", "sr_Latn", "sr_Latn" }, },
                new string[][]                { new string[] { "fr-Cyrl",    "fr-Arab"    },
                                                new string[] { "Français (arabe)", "Français (arabe)", "fr_Arab", "fr_Arab" },
                                                new string[] { "Français (cyrillique)", "Français (cyrillique)", "fr_Cyrl", "fr_Cyrl" }, },
                new string[][]                { new string[] { "fr-Cyrl-BE", "fr-Arab-CA" },
                                                new string[] { "Français (arabe, Canada)", "Français (arabe, Canada)", "fr_Arab_CA", "fr_Arab_CA" },
                                                new string[] { "Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE" }, }
            };
            ULocale            french = ULocale.FRENCH;
            LocaleDisplayNames names  = LocaleDisplayNames.GetInstance(french,
                                                                       DisplayContext.CapitalizationForUIListOrMenu);

            foreach (DisplayContextType type in Enum.GetValues(typeof(DisplayContextType)))
            {
                Logln("Contexts: " + names.GetContext(type).ToString());
            }
            Collator collator = Collator.GetInstance(french);

            foreach (String[][] test in tests)
            {
                // ICU4N TODO: LinkedHashSet needed ?
                ISet <ULocale>    list     = new HashSet <ULocale>(); // LinkedHashSet<ULocale>();
                List <UiListItem> expected = new List <UiListItem>();
                foreach (String item in 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]));
                }
                IList <UiListItem> newList = names.GetUiList(list, false, collator);
                if (!expected.SequenceEqual(newList))
                {
                    if (expected.Count != newList.Count)
                    {
                        Errln(CollectionUtil.ToString(list) + ": wrong size" + expected + ", " + newList);
                    }
                    else
                    {
                        Errln(CollectionUtil.ToString(list));
                        for (int i = 0; i < expected.Count; ++i)
                        {
                            assertEquals(i + "", expected[i], newList[i]);
                        }
                    }
                }
                else
                {
                    assertEquals(CollectionUtil.ToString(list), expected, newList);
                }
            }
        }