Пример #1
0
        /* === MANAGING LOCALE DATA (TEXT MAPPINGS) === */

        /**
         * Registers a resource file as a source of locale data for the specified
         * locale.
         *
         * @param locale The locale of the definitions provided.
         * @param resource A LocaleDataSource containing string data for the locale provided
         * @throws NullPointerException if resource or locale are null
         */
        public void registerLocaleResource(String locale, LocaleDataSource resource)
        {
            if (locale == null)
            {
                throw new NullReferenceException("Attempt to register a data source to a null locale in the localizer");
            }
            if (resource == null)
            {
                throw new NullReferenceException("Attempt to register a null data source in the localizer");
            }
            ArrayList resources = new ArrayList();

            if (localeResources.ContainsKey(locale))
            {
                resources = (ArrayList)localeResources[locale];
            }
            resources.Add(resource);
            localeResources.put(locale, resources);

            if (locale.Equals(currentLocale))
            {
                loadCurrentLocaleResources();
            }
        }
Пример #2
0
        /**
         * Get the set of mappings for a locale.
         *
         * @param locale Locale
         * @returns Hashtable representing text mappings for this locale. Returns null if locale not defined or null.
         */
        public OrderedHashtable getLocaleData(String locale)
        {
            if (locale == null || !this.locales.Contains(locale))
            {
                return(null);
            }

            //It's very important that any default locale contain the appropriate strings to localize the interface
            //for any possible language. As such, we'll keep around a table with only the default locale keys to
            //ensure that there are no localizations which are only present in another locale, which causes ugly
            //and difficult to trace errors.
            OrderedHashtable defaultLocaleKeys = new OrderedHashtable();

            //This table will be loaded with the default values first (when applicable), and then with any
            //language specific translations overwriting the existing values.
            OrderedHashtable data = new OrderedHashtable();

            // If there's a default locale, we load all of its elements into memory first, then allow
            // the current locale to overwrite any differences between the two.
            if (fallbackDefaultLocale && defaultLocale != null)
            {
                ArrayList defaultResources = (ArrayList)localeResources[defaultLocale];
                for (int i = 0; i < defaultResources.Count; ++i)
                {
                    loadTable(data, ((LocaleDataSource)defaultResources[i]).getLocalizedText());
                }
                for (IEnumerator en = data.GetEnumerator(); en.MoveNext();)
                {
                    defaultLocaleKeys.put(en.Current, Boolean.TrueString);
                }
            }

            ArrayList resources = (ArrayList)localeResources[locale];

            for (int i = 0; i < resources.Count; ++i)
            {
                loadTable(data, ((LocaleDataSource)resources[i]).getLocalizedText());
            }

            //If we're using a default locale, now we want to make sure that it has all of the keys
            //that the locale we want to use does. Otherwise, the app will crash when we switch to
            //a locale that doesn't contain the key.
            if (fallbackDefaultLocale && defaultLocale != null)
            {
                String missingKeys = "";
                int    keysmissing = 0;
                for (IEnumerator en = data.GetEnumerator(); en.MoveNext();)
                {
                    String key = (String)en.Current;
                    if (!defaultLocaleKeys.ContainsKey(key))
                    {
                        missingKeys += key + ",";
                        keysmissing++;
                    }
                }
                if (keysmissing > 0)
                {
                    //Is there a good way to localize these exceptions?
                    throw new NoLocalizedTextException("Error loading locale " + locale +
                                                       ". There were " + keysmissing + " keys which were contained in this locale, but were not " +
                                                       "properly registered in the default Locale. Any keys which are added to a locale should always " +
                                                       "be added to the default locale to ensure appropriate functioning.\n" +
                                                       "The missing translations were for the keys: " + missingKeys, missingKeys, defaultLocale);
                }
            }

            return(data);
        }