1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import java.util.ResourceBundle;
|
---|
4 | import java.util.Locale;
|
---|
5 | import java.util.Enumeration;
|
---|
6 |
|
---|
7 | public class Dictionary {
|
---|
8 |
|
---|
9 | /** The locale of this dictionary. */
|
---|
10 | protected Locale locale = null;
|
---|
11 |
|
---|
12 | /** The resource that has been loaded */
|
---|
13 | protected String resource = null;
|
---|
14 |
|
---|
15 | /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "resource_locale.properties*/
|
---|
16 | private ResourceBundle raw = null;
|
---|
17 |
|
---|
18 | /** 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.
|
---|
19 | * @param locale The <strong>Locale</strong> used to load the desired dictionary resource bundle.
|
---|
20 | */
|
---|
21 | public Dictionary(String resource, String lang) {
|
---|
22 | // Initialize.
|
---|
23 |
|
---|
24 | this.locale = new Locale(lang);
|
---|
25 | this.resource = resource;
|
---|
26 | if (this.locale == null) {
|
---|
27 | this.locale = Locale.getDefault();
|
---|
28 | }
|
---|
29 | try {
|
---|
30 | this.raw = ResourceBundle.getBundle(this.resource, this.locale);
|
---|
31 | } catch (Exception e) {
|
---|
32 | System.err.println("Dictionary: couldn't locate a resource bundle for "+resource);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public Enumeration getKeys() {
|
---|
37 | if (this.raw != null) {
|
---|
38 | return this.raw.getKeys();
|
---|
39 | }
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 | /** Overloaded to call get with both a key and an empty argument array.
|
---|
43 | * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
|
---|
44 | * @return A <strong>String</strong> which has been referenced by the key String and that contains no argument fields.
|
---|
45 | */
|
---|
46 | public String get(String key) {
|
---|
47 | return get(key, null);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /** 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>
|
---|
51 | * 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.
|
---|
52 | * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
|
---|
53 | * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
|
---|
54 | * @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 populated with argument Strings provided in the get call.
|
---|
55 | */
|
---|
56 | public String get(String key, String args[]) {
|
---|
57 | if (this.raw == null) {
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 | try {
|
---|
61 | String initial = this.raw.getString(key);
|
---|
62 | // If the string contains arguments we have to insert them.
|
---|
63 | String complete = "";
|
---|
64 | // While we still have initial string left.
|
---|
65 | while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
|
---|
66 | // Remove preamble
|
---|
67 | int opening = initial.indexOf('{');
|
---|
68 | int closing = initial.indexOf('}');
|
---|
69 | complete = complete + initial.substring(0, opening);
|
---|
70 | // Parse arg_num
|
---|
71 | String arg_str = initial.substring(opening + 1, closing);
|
---|
72 | int arg_num = Integer.parseInt(arg_str);
|
---|
73 | if(closing + 1 < initial.length()) {
|
---|
74 | initial = initial.substring(closing + 1);
|
---|
75 | }
|
---|
76 | else {
|
---|
77 | initial = "";
|
---|
78 | }
|
---|
79 | // Insert argument
|
---|
80 | if(args != null && 0 <= arg_num && arg_num < args.length) {
|
---|
81 | complete = complete + args[arg_num];
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return complete + initial;
|
---|
85 | }
|
---|
86 | catch (Exception e) {
|
---|
87 | System.err.println("Dictionary Error: couldn't find string for key:" + key +" in resource "+this.resource);
|
---|
88 | return null;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|