/* === INFORMATION ABOUT AVAILABLE LOCALES === */ /** * Create a new locale (with no mappings). Do nothing if the locale is already defined. * * @param locale Locale to add. Must not be null. * @return True if the locale was not already defined. * @throws NullPointerException if locale is null */ public Boolean addAvailableLocale(String locale) { if (hasLocale(locale)) { return(false); } else { locales.Add(locale); localeResources.put(locale, new ArrayList()); return(true); } }
/** * Moves all relevant entries in the source dictionary into the destination dictionary * @param destination A dictionary of key/value locale pairs that will be modified * @param source A dictionary of key/value locale pairs that will be copied into * destination */ private void loadTable(OrderedHashtable destination, OrderedHashtable source) { for (IEnumerator en = source.GetEnumerator(); en.MoveNext();) { String key = (String)en.Current; destination.put(key, (String)source[key]); } }
/** * Set a text mapping for a single text handle for a given locale. * * @param textID Text handle. Must not be null. Need not be previously defined for this locale. * @param text Localized text for this text handle and locale. Will overwrite any previous mapping, if one existed. * If null, will remove any previous mapping for this text handle, if one existed. * @throws UnregisteredLocaleException If locale is not defined or null. * @throws NullPointerException if textID is null */ public void setLocaleMapping(String textID, String text) { if (textID == null) { throw new NullReferenceException("Null textID when attempting to register " + text + " in locale table"); } if (text == null) { localeData.remove(textID); } else { localeData.put(textID, text); } }
private void parseAndAdd(OrderedHashtable locale, String line, int curline) { //trim whitespace. line = line.Trim(); //clear comments while (line.IndexOf("#") != -1) { line = line.Substring(0, line.IndexOf("#")); } if (line.IndexOf('=') == -1) { // TODO: Invalid line. Empty lines are fine, especially with comments, // but it might be hard to get all of those. if (line.Trim().Equals("")) { //Empty Line } else { Console.WriteLine("Invalid line (#" + curline + ") read: " + line); } } else { //Check to see if there's anything after the '=' first. Otherwise there //might be some big problems. if (line.IndexOf('=') != line.Length - 1) { String value = line.Substring(line.IndexOf('=') + 1, line.Length); locale.put(line.Substring(0, line.IndexOf('=')), value); } else { Console.WriteLine("Invalid line (#" + curline + ") read: '" + line + "'. No value follows the '='."); } } }
/** * 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); }