source: gli/trunk/src/org/greenstone/gatherer/Dictionary.java@ 18370

Last change on this file since 18370 was 18370, checked in by kjdon, 15 years ago

committed code submitted by Amin Hedjazi for making the GLI right to left. I worked on this code on the rtl-gli branch, then merged the branch back to the trunk at revision 18368. The branch code was slightly different in a couple of places where it shouldn't have been. So don't use the branch code next time. Start a new branch.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer;
38
39import java.awt.ComponentOrientation;
40import javax.swing.plaf.FontUIResource;
41import java.util.HashMap;
42import java.util.Locale;
43import java.util.ResourceBundle;
44
45
46/** Extends the ResourceBundle class to allow for the automatic insertion of arguments.<BR>
47 * <BR>
48 * Property files usable by this class have the Backus-Naur form: <BR>
49 * <BR>
50 * FileLine ::= Comment | Mapping <BR>
51 * Comment ::= '#' SString <BR>
52 * Mapping ::= NZString ':' SString ( Argument SString )* <BR>
53 * NZString ::= ( Char | Int ) SString <BR>
54 * Argument ::= '{' Int '}' <BR>
55 * SString ::= String . ['"','#',...] -> ['\"','\#',...] <BR>
56 * <BR>
57 * In order to add a new dictionary Locale, simply copy the existing dictionary.properties files, replace the values (Strings after the ':') with the new language specific ones being careful to maintain formatting and Gatherer placed arguments, then save the new dictionary as: <br>
58 * <BR>
59 * dictionary_<I>locale</I>.properties<BR>
60 * <BR>
61 * where locale is made of two two-letter codes seperated by an underscore. The first code is in lower-case and defines the language. The second is in upper-case and defines the country. For example the default dictionary could also correctly be called:
62 * <BR>
63 * dictionary_en_NZ.properties<BR>
64 * @author John Thompson, Greenstone Digital Library, University of Waikato
65 * @version 2.3
66 */
67public class Dictionary
68 extends HashMap {
69
70 /** The font used when displaying various html text. */
71 static private FontUIResource font = null;
72 /** A reference to remind us of the current locale. */
73 static private Locale locale = null;
74 /** The ResourceBundle which contains the raw key-value mappings.
75 Loaded from a file named "dictionary<I>locale</I>.properties. */
76 static private ResourceBundle dictionary = null;
77 /** language orientation property */
78 static private ComponentOrientation orientation = null;
79
80 public Dictionary(Locale locale, FontUIResource font)
81 {
82 super();
83
84 // Initialize
85 this.font = font;
86 this.locale = ((locale == null) ? Locale.getDefault() : locale);
87 this.dictionary = ResourceBundle.getBundle("dictionary", this.locale);
88 if (this.get("Component.Orientation").equals("RTL")){
89 this.orientation = ComponentOrientation.RIGHT_TO_LEFT;
90 } else {
91 this.orientation = ComponentOrientation.LEFT_TO_RIGHT;
92 }
93
94 }
95
96 /** Retrieve the two letter code of the current language we are using, according to the stored locale.
97 * @return A <strong>String</strong> containing the two letter ISO639 language code.
98 */
99 static public String getLanguage()
100 {
101 return locale.getLanguage();
102 }
103 /** returns the Components Orientation for use with right to left languages
104 * @return A <strong>ComponentOrientation</strong> with the current
105 dictionary's language orientation.
106 * by Amin Hejazi
107 */
108 static public ComponentOrientation getOrientation()
109 {
110 return orientation;
111 }
112
113 static public String get(String key)
114 {
115 return get(key, (String[]) null);
116 }
117
118 static public String get(String key, String arg)
119 {
120 String[] args = new String[1];
121 args[0] = arg;
122 return get(key, args);
123 }
124
125
126 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
127 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned.
128 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
129 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
130 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
131 */
132 static public String get(String key, String[] args)
133 {
134 try {
135 String initial_raw = dictionary.getString(key);
136
137 // Convert into Unicode
138 String initial = "";
139 try {
140 // This "ISO-8859-1" looks out of place, but it is very important.
141 // It is essential to call getBytes with an 8-bit encoding, otherwise
142 // Java kindly deems some characters "undisplayable", and replaces
143 // them with question marks. This is NOT good.
144 initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
145 }
146 catch (Exception exception) {
147 DebugStream.printStackTrace(exception);
148 return initial_raw;
149 }
150
151 // Remove any comments from the string
152 if (initial.indexOf("#") != -1) {
153 initial = initial.substring(0, initial.indexOf("#"));
154 }
155
156 // If the string contains arguments we have to insert them.
157 StringBuffer complete = new StringBuffer();
158
159 // While we still have initial string left.
160 while (initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
161 // Remove preamble
162 int opening = initial.indexOf('{');
163 int closing = initial.indexOf('}');
164 int comment_mark = initial.indexOf('-', opening); // May not exist
165 if (comment_mark > closing) { // May also be detecting a later comment
166 comment_mark = -1;
167 }
168 complete.append(initial.substring(0, opening));
169
170 // Parse arg_num
171 String arg_str = null;
172 if (comment_mark != -1) {
173 arg_str = initial.substring(opening + 1, comment_mark);
174 }
175 else {
176 arg_str = initial.substring(opening + 1, closing);
177 }
178
179 if (closing + 1 < initial.length()) {
180 initial = initial.substring(closing + 1);
181 }
182 else {
183 initial = "";
184 }
185
186 // Insert argument
187 if (arg_str.equals("FONT")) {
188 complete.append((font != null ? font.getFontName() : "Arial"));
189 }
190 else {
191 int arg_num = Integer.parseInt(arg_str);
192 if (args != null && 0 <= arg_num && arg_num < args.length) {
193 complete.append(args[arg_num]);
194 }
195 }
196 }
197 complete.append(initial);
198
199 return complete.toString();
200 }
201 catch (Exception exception) {
202 System.err.println("Missing value for key: " + key);
203 // DebugStream.printStackTrace(e);
204 return key;
205 }
206 }
207
208}
209
Note: See TracBrowser for help on using the repository browser.