Changeset 6854
- Timestamp:
- 2004-02-20T12:38:04+13:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/Dictionary.java
r6849 r6854 70 70 extends HashMap { 71 71 72 static final private boolean KEY_LIST_DEBUG = false;73 static final private String KEY_LIST_FILENAME = "keylist.txt";74 75 /** A String which more explicitly states the Locale of this dictionary. */76 public String language = null;77 72 /** The font used when displaying various html text. */ 78 staticprivate FontUIResource font = null;73 private FontUIResource font = null; 79 74 /** A reference to remind us of the current locale. */ 80 75 private Locale locale = null; 81 76 /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "dictionary<I>locale</I>.properties*/ 82 static private ResourceBundle dictionary = null; 83 84 private TreeSet key_list = null; 85 86 /** Constructs the Dictionary class by first checking if a Locale has been set. If not the default locale is used, and a ResourceBundle is created. Finally a single important String, Language, is made available outside the class so a more read-able version of the Locale of this Dictionary is present. 87 * @param locale The <strong>Locale</strong> used to load the desired dictionary resource bundle. 88 */ 89 public Dictionary(Locale locale, FontUIResource font) { 77 private ResourceBundle dictionary = null; 78 79 80 public Dictionary(Locale locale, FontUIResource font) 81 { 90 82 super(); 91 83 92 if (KEY_LIST_DEBUG) { 93 // Reload the keylist 94 File file = new File(KEY_LIST_FILENAME); 95 key_list = new TreeSet(); 96 try { 97 BufferedReader br = new BufferedReader(new FileReader(file)); 98 String line; 99 while((line = br.readLine()) != null) { 100 key_list.add(line); 101 } 102 br.close(); 103 br = null; 104 } 105 catch(Exception error) { 106 error.printStackTrace(); 107 } 108 } 109 110 // Initialize. 84 // Initialize 111 85 this.font = font; 112 if (locale == null) { 113 this.locale = Locale.getDefault(); 114 } 115 else { 116 this.locale = locale; 117 //Locale.setDefault(locale); 118 } 119 dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, this.locale); 120 // Now quickly read in language name. 121 language = dictionary.getString("Language"); 122 } 123 124 125 public void destroy() { 126 // if(key_list != null) { 127 // try { 128 // FileOutputStream fos = new FileOutputStream(KEY_LIST_FILENAME); 129 // for(Iterator iter = key_list.iterator(); iter.hasNext(); ) { 130 // String value = (String) iter.next(); 131 // fos.write(value.getBytes()); 132 // fos.write('\n'); 133 // value = null; 134 // } 135 // fos.close(); 136 // fos = null; 137 // } 138 // catch(Exception error) { 139 // error.printStackTrace(); 140 // } 141 // } 86 this.locale = ((locale == null) ? Locale.getDefault() : locale); 87 this.dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, this.locale); 142 88 } 143 89 … … 146 92 * @return A <strong>String</strong> containing the two letter ISO639 language code. 147 93 */ 148 public String getLanguage() { 94 public String getLanguage() 95 { 149 96 return locale.getLanguage(); 150 97 } … … 173 120 static public String get(String key, String[] args) 174 121 { 175 // if(key_list != null) {176 // synchronized(this) {177 // key_list.add(key);178 // }179 // }180 122 try { 181 String initial = dictionary.getString(key); 123 String initial_raw = dictionary.getString(key); 124 125 // Convert into Unicode 126 String initial = ""; 127 try { 128 // This "ISO-8859-1" looks out of place, but it is very important. 129 // It is essential to call getBytes with an 8-bit encoding, otherwise 130 // Java kindly deems some characters "undisplayable", and replaces 131 // them with question marks. This is NOT good. 132 initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8"); 133 } 134 catch (Exception ex) { 135 System.err.println("Exception: " + ex); 136 ex.printStackTrace(); 137 return initial_raw; 138 } 182 139 183 140 // If the string contains arguments we have to insert them. … … 190 147 int closing = initial.indexOf('}'); 191 148 int comment_mark = initial.indexOf('-', opening); // May not exist 192 if (comment_mark > closing) { // May also be detecting a later comment149 if (comment_mark > closing) { // May also be detecting a later comment 193 150 comment_mark = -1; 194 151 } … … 196 153 // Parse arg_num 197 154 String arg_str = null; 198 if (comment_mark != -1) {155 if (comment_mark != -1) { 199 156 arg_str = initial.substring(opening + 1, comment_mark); 200 157 } … … 203 160 } 204 161 int arg_num = Integer.parseInt(arg_str); 205 if (closing + 1 < initial.length()) {162 if (closing + 1 < initial.length()) { 206 163 initial = initial.substring(closing + 1); 207 164 } … … 210 167 } 211 168 // Insert argument 212 if (args != null && 0 <= arg_num && arg_num < args.length) {169 if (args != null && 0 <= arg_num && arg_num < args.length) { 213 170 complete = complete + args[arg_num]; 214 171 } 215 else if (arg_num >= 32) {172 else if (arg_num >= 32) { 216 173 String f_subargs[] = new String[1]; 217 if (font != null) {174 if (font != null) { 218 175 f_subargs[0] = font.getFontName(); 219 176 } … … 226 183 complete = complete + initial; 227 184 228 String unicode = ""; 229 try { 230 // This "ISO-8859-1" looks out of place, but it is very important. 231 // It is essential to call getBytes with an 8-bit encoding, otherwise 232 // Java kindly deems some characters "undisplayable", and replaces 233 // them with question marks. This is NOT good. 234 unicode = new String(complete.getBytes("ISO-8859-1"), "UTF-8"); 235 } 236 catch (Exception ex) { 237 System.err.println("Exception: " + ex); 238 Gatherer.printStackTrace(ex); 239 return initial; 240 } 241 return unicode; 185 return complete; 242 186 } 243 187 catch (Exception e) {
Note:
See TracChangeset
for help on using the changeset viewer.