Exemple #1
0
        public void TestLocale()
        {
            ICULocaleService service = new ICULocaleService("test locale");

            service.RegisterObject("root", UCultureInfo.InvariantCulture);
            service.RegisterObject("german", "de");
            service.RegisterObject("german_Germany", new UCultureInfo("de_DE"));
            service.RegisterObject("japanese", "ja");
            service.RegisterObject("japanese_Japan", new UCultureInfo("ja_JP"));

            object target = service.Get("de_US");

            confirmEqual("test de_US", "german", target);

            UCultureInfo de    = new UCultureInfo("de");
            UCultureInfo de_US = new UCultureInfo("de_US");

            target = service.Get(de_US);
            confirmEqual("test de_US 2", "german", target);

            target = service.Get(de_US, LocaleKey.KindAny);
            confirmEqual("test de_US 3", "german", target);

            target = service.Get(de_US, 1234);
            confirmEqual("test de_US 4", "german", target);

            target = service.Get(de_US, out UCultureInfo actualReturn); // ICU4N specific - refactored to out parameter
            confirmEqual("test de_US 5", "german", target);
            confirmEqual("test de_US 6", actualReturn, de);

            target = service.Get(de_US, LocaleKey.KindAny, out actualReturn); // ICU4N specific - refactored to out parameter
            confirmEqual("test de_US 7", actualReturn, de);

            target = service.Get(de_US, 1234, out actualReturn); // ICU4N specific - refactored to out parameter
            confirmEqual("test de_US 8", "german", target);
            confirmEqual("test de_US 9", actualReturn, de);

            service.RegisterObject("one/de_US", de_US, 1);
            service.RegisterObject("two/de_US", de_US, 2);

            target = service.Get(de_US, 1);
            confirmEqual("test de_US kind 1", "one/de_US", target);

            target = service.Get(de_US, 2);
            confirmEqual("test de_US kind 2", "two/de_US", target);

            target = service.Get(de_US);
            confirmEqual("test de_US kind 3", "german", target);

            LocaleKey lkey = LocaleKey.CreateWithCanonicalFallback("en", null, 1234);

            Logln("lkey prefix: " + lkey.Prefix);
            Logln("lkey descriptor: " + lkey.GetCurrentDescriptor());
            Logln("lkey current locale: " + lkey.GetCurrentCulture());

            lkey.Fallback();
            Logln("lkey descriptor 2: " + lkey.GetCurrentDescriptor());

            lkey.Fallback();
            Logln("lkey descriptor 3: " + lkey.GetCurrentDescriptor());

            target = service.Get("za_PPP");
            confirmEqual("test zappp", "root", target);

            ICollection <string> ids;

            using (new ThreadCultureChange("ja", "ja"))
            {
                target = service.Get("za_PPP");
                confirmEqual("test with ja locale", "japanese", target);

                ids = service.GetVisibleIDs();
                for (var iter = ids.GetEnumerator(); iter.MoveNext();)
                {
                    Logln("id: " + iter.Current);
                }
            }
            ids = service.GetVisibleIDs();
            for (var iter = ids.GetEnumerator(); iter.MoveNext();)
            {
                Logln("id: " + iter.Current);
            }

            target = service.Get("za_PPP");
            confirmEqual("test with en locale", "root", target);

            UCultureInfo[] locales = service.GetUCultures(UCultureTypes.AllCultures);
            confirmIdentical("test available locales", locales.Length, 6);
            Logln("locales: ");
            for (int i = 0; i < locales.Length; ++i)
            {
                Log("\n  [" + i + "] " + locales[i]);
            }
            Logln(" ");

            service.RegisterFactory(new ICUResourceBundleFactory());
            target = service.Get(new UCultureInfo("ja_JP"));

            {
                int n         = 0;
                var factories = service.Factories();
                var iter      = factories.GetEnumerator();
                while (iter.MoveNext())
                {
                    Logln("[" + n++ + "] " + iter.Current);
                }
            }

            // list only the english display names for es, in reverse order
            // since we're using locale keys, we should get all and only the es locales
            // hmmm, the default toString function doesn't print in sorted order for TreeMap
            {
                var map = service.GetDisplayNames(new UCultureInfo("en_US"),
                                                  new AnonymousComparer <object>(compare: (lhs, rhs) =>
                {
                    return(-StringComparer.OrdinalIgnoreCase.Compare((string)lhs, (string)rhs));
                }),
                                                  //                    new Comparator() {
                                                  //                            @Override
                                                  //                            public int compare(object lhs, object rhs)
                                                  //    {
                                                  //        return -string.CASE_INSENSITIVE_ORDER.compare((string)lhs, (string)rhs);
                                                  //    }
                                                  //},
                                                  "es");

                Logln("es display names in reverse order " + map);
            }
        }
        public void TestLocale()
        {
            ICULocaleService service = new ICULocaleService("test locale");

            service.RegisterObject("root", IBM.ICU.Util.ULocale.ROOT);
            service.RegisterObject("german", "de");
            service.RegisterObject("german_Germany", IBM.ICU.Util.ULocale.GERMANY);
            service.RegisterObject("japanese", "ja");
            service.RegisterObject("japanese_Japan", IBM.ICU.Util.ULocale.JAPAN);

            Object target = service.Get("de_US");

            ConfirmEqual("test de_US", "german", target);

            ULocale de    = new ULocale("de");
            ULocale de_US = new ULocale("de_US");

            target = service.Get(de_US);
            ConfirmEqual("test de_US 2", "german", target);

            target = service.Get(de_US, IBM.ICU.Impl.ICULocaleService.LocaleKey.KIND_ANY);
            ConfirmEqual("test de_US 3", "german", target);

            target = service.Get(de_US, 1234);
            ConfirmEqual("test de_US 4", "german", target);

            ULocale[] actualReturn = new ULocale[1];
            target = service.Get(de_US, actualReturn);
            ConfirmEqual("test de_US 5", "german", target);
            ConfirmEqual("test de_US 6", actualReturn[0], de);

            actualReturn[0] = null;
            target          = service.Get(de_US, IBM.ICU.Impl.ICULocaleService.LocaleKey.KIND_ANY, actualReturn);
            ConfirmEqual("test de_US 7", actualReturn[0], de);

            actualReturn[0] = null;
            target          = service.Get(de_US, 1234, actualReturn);
            ConfirmEqual("test de_US 8", "german", target);
            ConfirmEqual("test de_US 9", actualReturn[0], de);

            service.RegisterObject("one/de_US", de_US, 1);
            service.RegisterObject("two/de_US", de_US, 2);

            target = service.Get(de_US, 1);
            ConfirmEqual("test de_US kind 1", "one/de_US", target);

            target = service.Get(de_US, 2);
            ConfirmEqual("test de_US kind 2", "two/de_US", target);

            target = service.Get(de_US);
            ConfirmEqual("test de_US kind 3", "german", target);

            IBM.ICU.Impl.ICULocaleService.LocaleKey lkey = IBM.ICU.Impl.ICULocaleService.LocaleKey
                                                           .CreateWithCanonicalFallback("en", null, 1234);
            Logln("lkey prefix: " + lkey.Prefix());
            Logln("lkey descriptor: " + lkey.CurrentDescriptor());
            Logln("lkey current locale: " + lkey.CurrentLocale());

            lkey.Fallback();
            Logln("lkey descriptor 2: " + lkey.CurrentDescriptor());

            lkey.Fallback();
            Logln("lkey descriptor 3: " + lkey.CurrentDescriptor());

            target = service.Get("za_PPP");
            ConfirmEqual("test zappp", "root", target);

            ULocale loc = IBM.ICU.Util.ULocale.GetDefault();

            IBM.ICU.Util.ULocale.SetDefault(IBM.ICU.Util.ULocale.JAPANESE);
            target = service.Get("za_PPP");
            ConfirmEqual("test with ja locale", "japanese", target);

            ILOG.J2CsMapping.Collections.ISet ids_0 = service.GetVisibleIDs();
            for (IIterator iter = new ILOG.J2CsMapping.Collections.IteratorAdapter(ids_0.GetEnumerator()); iter.HasNext();)
            {
                Logln("id: " + iter.Next());
            }

            IBM.ICU.Util.ULocale.SetDefault(loc);
            ids_0 = service.GetVisibleIDs();
            for (IIterator iter_1 = new ILOG.J2CsMapping.Collections.IteratorAdapter(ids_0.GetEnumerator()); iter_1.HasNext();)
            {
                Logln("id: " + iter_1.Next());
            }

            target = service.Get("za_PPP");
            ConfirmEqual("test with en locale", "root", target);

            ULocale[] locales = service.GetAvailableULocales();
            ConfirmIdentical("test available locales", locales.Length, 6);
            Logln("locales: ");
            for (int i = 0; i < locales.Length; ++i)
            {
                Log("\n  [" + i + "] " + locales[i]);
            }
            Logln(" ");

            service.RegisterFactory(new IBM.ICU.Impl.ICULocaleService.ICUResourceBundleFactory());
            target = service.Get(IBM.ICU.Util.ULocale.JAPAN);

            {
                int       n_2       = 0;
                IList     factories = service.Factories();
                IIterator iter_3    = new ILOG.J2CsMapping.Collections.IteratorAdapter(factories.GetEnumerator());
                while (iter_3.HasNext())
                {
                    Logln("[" + n_2++ + "] " + iter_3.Next());
                }
            }

            // list only the english display names for es, in reverse order
            // since we're using locale keys, we should get all and only the es
            // locales
            // hmmm, the default toString function doesn't print in sorted order for
            // TreeMap
            {
                SortedList map = service.GetDisplayNames(IBM.ICU.Util.ULocale.US,
                                                         new ICUServiceTest.Anonymous_C0(), "es");

                Logln("es display names in reverse order " + map);
            }
        }