View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.i18n;
21  
22  import java.text.MessageFormat;
23  import java.util.Locale;
24  import java.util.MissingResourceException;
25  import java.util.ResourceBundle;
26  
27  import org.xnap.*;
28  
29  /***
30   * Provides methods for internationalization.
31   */
32  public class I18n {
33  
34      //--- Constant(s) ---
35  
36      //--- Data field(s) ---
37  
38      /*** Reference to the current localization bundle. */
39      private static ResourceBundle resources;
40  
41      //--- Method(s) ---
42  
43      /***
44       * Returns the currently used message bundle.
45       */
46      public static final ResourceBundle getResources()
47      {
48  		return resources;
49      }
50  
51      /***
52       * Loads a resource bundle from location, using the default locale.
53       */
54      public static final void setResources(String location) 
55      {
56  		setResources(ResourceBundle.getBundle(location, Locale.getDefault()));
57      }
58  
59      /***
60       * Sets the resource bundle.
61       */
62      public static final void setResources(ResourceBundle resources) 
63      {
64  		I18n.resources = resources;
65      }
66  	
67  	/***
68  	 * Marks <code>text</code> to be translated, but doesn't translate it.
69  	 *
70  	 */
71  	public static final String marktr(String text)
72  	{
73  		return text;
74  	}
75      
76      /***
77       * Returns <code>text</code> translated into the currently selected
78       * language. Every user-visible string in the program must be wrapped
79       * into this function.  
80       */
81      public static final String tr(String text)
82      {
83  		try {
84  			return resources.getString(text);
85  		}
86  		catch (MissingResourceException e) {
87  			//System.err.println("missing translation key: \"" + text + "\"");
88  			//e.printStackTrace(System.err);
89  			return text;
90  		}
91  		catch (NullPointerException e) {
92  			// the message bundle has not been loaded yet
93  			return text;
94  		}
95      }
96  
97      /***
98       * Returns <code>text</code> translated into the currently selected
99       * language. 
100      *
101      * <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
102      */
103     public static final String tr(String text, Object o1)
104     {
105 		return MessageFormat.format(tr(text), new Object[] { o1 });
106     }
107 
108 	/***
109 	 * Disambiguates translation keys.
110 	 *
111 	 * @param comment the text translated + a disambiguation hint in brackets.
112 	 * @param text the ambiguous key string
113 	 */
114 	public static final String trc(String comment, String text)
115 	{
116 		return Locale.getDefault().equals(Locale.ENGLISH) ? text
117 			: XNap.tr(comment);
118 	}
119 
120     /***
121      * Returns <code>text</code> translated into the currently selected
122      * language. 
123      *
124      * <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
125      * The first occurence of {1} is replaced by <code>o2.toString()</code>.
126      */
127     public static final String tr(String text, Object o1, Object o2)
128     {
129 		return MessageFormat.format(tr(text), new Object[] { o1, o2 });
130     }
131 
132     /***
133      * Returns <code>text</code> translated into the currently selected
134      * language. 
135      *
136      * <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
137      * The first occurence of {1} is replaced by <code>o2.toString()</code>.
138      * The first occurence of {2} is replaced by <code>o3.toString()</code>.
139      */
140     public static final String tr(String text, Object o1, Object o2,
141 								  Object o3)
142     {
143 		return MessageFormat.format(tr(text), 
144 									new Object[] { o1, o2, o3 });
145     }
146 
147 	/***
148 	 * Returns <code>text</code> translated into the currently selected
149 	 * language. 
150 	 *
151 	 * <p>The first occurence of {0} is replaced by <code>o1.toString()</code>.
152 	 * The first occurence of {1} is replaced by <code>o2.toString()</code>.
153 	 * The first occurence of {2} is replaced by <code>o3.toString()</code>.
154 	 * The first occurence of {3} is replaced by <code>o4.toString()</code>.
155 	 */
156 	public static final String tr(String text, Object o1, Object o2,
157 								  Object o3, Object o4)
158 	{
159 		return MessageFormat.format(tr(text), 
160 									new Object[] { o1, o2, o3, o4 });
161 	}
162 
163     /***
164      * Returns <code>text</code> translated into the currently selected
165      * language. Prepends and appends <code>padding</code> whitespaces.
166      */
167     public static final String tr(String text, int padding)
168     {
169 		String s = tr(text);
170 		if (padding <= 0) {
171 			return s;
172 		}
173 		StringBuffer sb = new StringBuffer(s.length() + padding * 2);
174 		append(sb, " ", padding);
175 		sb.append(s);
176 		append(sb, " ", padding);
177 		return sb.toString();
178     }
179 
180     /***
181      * Returns <code>text</code> translated into the currently selected
182      * language. Prepends <code>lpadding</code> whitespaces. Appends
183      * <code>rpadding</code> whitespaces.
184      */
185     public static final String tr(String text, int lpadding, int rpadding)
186     {
187 		String s = tr(text);
188 		StringBuffer sb = new StringBuffer(s.length() + lpadding + rpadding);
189 		append(sb, " ", lpadding);
190 		sb.append(s);
191 		append(sb, " ", rpadding);
192 		return sb.toString();
193     }
194 
195     private static final void append(StringBuffer sb, String s, int count)
196     {
197 		for (int i = 0; i < count; i++) {
198 			sb.append(s);
199 		}
200     }
201 }