001: /*
002: * I18nUtil.java
003: *
004: *
005: * Copyright (c) 2002-2007, Hewlett-Packard Company and Massachusetts
006: * Institute of Technology. All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions are
010: * met:
011: *
012: * - Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * - Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in the
017: * documentation and/or other materials provided with the distribution.
018: *
019: * - Neither the name of the Hewlett-Packard Company nor the name of the
020: * Massachusetts Institute of Technology nor the names of their
021: * contributors may be used to endorse or promote products derived from
022: * this software without specific prior written permission.
023: *
024: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
025: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
026: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
027: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
028: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
029: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
030: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
031: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
033: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
034: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
035: * DAMAGE.
036: */
037:
038: package org.dspace.core;
039:
040: import org.apache.commons.lang.StringUtils;
041: import org.apache.log4j.Logger;
042: import org.dspace.eperson.EPerson;
043:
044: import java.io.File;
045: import java.util.Locale;
046: import java.util.MissingResourceException;
047: import java.util.ResourceBundle;
048: import java.util.StringTokenizer;
049:
050: /**
051: * I18nUtil.java
052: *
053: * Some Utilities for i18n Support.
054: * - getting the default Locale for this DSpace Instance
055: * - getting all supported Locales for this DSpace Instance
056: * - getting email template, help file, input forms for a given Locale
057: *
058: *
059: * @author Bernadette Schlonsok and Claudia Juergen
060: *
061: * @version 1.0
062: */
063:
064: public class I18nUtil {
065: private static final Logger log = Logger.getLogger(I18nUtil.class);
066:
067: // the default Locale of this DSpace Instance
068: public static final Locale DEFAULTLOCALE = getDefaultLocale();
069:
070: /**
071: * Gets the default locale as defined in dspace.cfg If no default locale is
072: * defined, the Locale of the JVM is used
073: *
074: * @return defaultLocale
075: * the default Locale for this DSpace instance
076: */
077: public static Locale getDefaultLocale() {
078: Locale defaultLocale = null;
079: if ((ConfigurationManager.getProperty("default.locale") != null)
080: && (ConfigurationManager.getProperty("default.locale") != "")) {
081: StringTokenizer configDefaultLocale = new StringTokenizer(
082: ConfigurationManager.getProperty("default.locale"));
083: int countTokens = configDefaultLocale.countTokens();
084: switch (countTokens) {
085:
086: case 1:
087: defaultLocale = new Locale(configDefaultLocale
088: .nextToken().trim());
089: break;
090:
091: case 2:
092: defaultLocale = new Locale(configDefaultLocale
093: .nextToken().trim(), configDefaultLocale
094: .nextToken().trim());
095: break;
096: case 3:
097: defaultLocale = new Locale(configDefaultLocale
098: .nextToken().trim(), configDefaultLocale
099: .nextToken().trim(), configDefaultLocale
100: .nextToken().trim());
101: break;
102: }
103:
104: }
105: if (defaultLocale == null) {
106: // use the Locale of the JVM
107: defaultLocale = Locale.getDefault();
108: }
109:
110: return defaultLocale;
111: }
112:
113: /**
114: * Get the Locale for a specified EPerson. If the language is missing,
115: * return the default Locale for the repository.
116: *
117: * @param ep
118: * @return
119: */
120: public static Locale getEPersonLocale(EPerson ep) {
121: if (ep == null) {
122: log.error("No EPerson specified, returning default locale");
123: return I18nUtil.getDefaultLocale();
124: }
125:
126: String lang = ep.getLanguage();
127:
128: if (StringUtils.isBlank(lang)) {
129: log
130: .error("No language specified for EPerson "
131: + ep.getID());
132: return I18nUtil.getDefaultLocale();
133: }
134:
135: return I18nUtil.getSupportedLocale(new Locale(lang));
136: }
137:
138: /**
139: * get the available Locales for the User Interface as defined in dspace.cfg
140: * returns an array of Locales or null
141: *
142: * @return an array of supported Locales or null
143: */
144: public static Locale[] getSupportedLocales() {
145: Locale[] availableLocales;
146:
147: if (ConfigurationManager.getProperty("webui.supported.locales") != null) {
148:
149: StringTokenizer configuredLocales = new StringTokenizer(
150: ConfigurationManager
151: .getProperty("webui.supported.locales"),
152: ",");
153: availableLocales = new Locale[configuredLocales
154: .countTokens()];
155:
156: while (configuredLocales.hasMoreTokens()) {
157: StringTokenizer localeElements = new StringTokenizer(
158: configuredLocales.nextToken().trim(), "_");
159: int countTokens = localeElements.countTokens();
160: switch (countTokens) {
161:
162: case 1:
163: availableLocales[configuredLocales.countTokens()] = new Locale(
164: localeElements.nextToken().trim());
165: break;
166:
167: case 2:
168: availableLocales[configuredLocales.countTokens()] = new Locale(
169: localeElements.nextToken().trim(),
170: localeElements.nextToken().trim());
171: break;
172: case 3:
173: availableLocales[configuredLocales.countTokens()] = new Locale(
174: localeElements.nextToken().trim(),
175: localeElements.nextToken().trim(),
176: localeElements.nextToken().trim());
177: break;
178: }
179: }
180: } else {
181: availableLocales = new Locale[1];
182: availableLocales[0] = DEFAULTLOCALE;
183: }
184: return availableLocales;
185: }
186:
187: /**
188: * Gets the appropriate supported Locale according for a given Locale If
189: * no appropriate supported locale is found, the DEFAULTLOCALE is used
190: *
191: * @param locale
192: * Locale to find the corresponding Locale
193: * @return supportedLocale
194: * Locale for session according to locales supported by this DSpace instance as set in dspace.cfg
195: */
196:
197: public static Locale getSupportedLocale(Locale locale) {
198:
199: Locale[] availableLocales = getSupportedLocales();
200: boolean isSupported = false;
201: Locale supportedLocale = null;
202: String testLocale = "";
203: if (availableLocales == null) {
204: supportedLocale = DEFAULTLOCALE;
205: } else {
206: if (!locale.getVariant().equals("")) {
207: testLocale = locale.toString();
208: for (int i = 0; i < availableLocales.length; i++) {
209: if (testLocale.equalsIgnoreCase(availableLocales[i]
210: .toString())) {
211: isSupported = true;
212: supportedLocale = availableLocales[i];
213: }
214:
215: }
216: }
217:
218: if (!(isSupported && locale.getCountry().equals(""))) {
219: testLocale = locale.getLanguage() + "_"
220: + locale.getCountry();
221:
222: for (int i = 0; i < availableLocales.length; i++) {
223: if (testLocale.equalsIgnoreCase(availableLocales[i]
224: .toString())) {
225: isSupported = true;
226: supportedLocale = availableLocales[i];
227: }
228: }
229:
230: }
231: if (!isSupported) {
232: testLocale = locale.getLanguage();
233:
234: for (int i = 0; i < availableLocales.length; i++) {
235: if (testLocale.equalsIgnoreCase(availableLocales[i]
236: .toString())) {
237: isSupported = true;
238: supportedLocale = availableLocales[i];
239: }
240:
241: }
242: }
243: if (!isSupported) {
244: supportedLocale = DEFAULTLOCALE;
245: }
246: }
247: return supportedLocale;
248: }
249:
250: /**
251: * Get the appropriate localized version of input-forms.xml according to language settings
252: *
253: * @param locale
254: * Locale, the local to get the input-forms.xml for
255: * @return String - localized filename for input-forms.xml
256: */
257: public static String getInputFormsFileName(Locale locale) {
258: /** Name of the form definition XML file */
259: String fileName = "";
260: final String FORM_DEF_FILE = "input-forms";
261: final String FILE_TYPE = ".xml";
262: String defsFilename = ConfigurationManager
263: .getProperty("dspace.dir")
264: + File.separator
265: + "config"
266: + File.separator
267: + FORM_DEF_FILE;
268: fileName = getFilename(locale, defsFilename, FILE_TYPE);
269: return fileName;
270: }
271:
272: /**
273: * et the i18n message string for a given key and use the default Locale
274: *
275: * @param key
276: * String - name of the key to get the message for
277: *
278: * @return message
279: * String of the message
280: *
281: *
282: */
283: public static String getMessage(String key)
284: throws MissingResourceException {
285:
286: String message = getMessage(key.trim(), DEFAULTLOCALE);
287:
288: return message;
289: }
290:
291: /**
292: * Get the i18n message string for a given key and locale
293: *
294: * @param key
295: * String - name of the key to get the message for
296: * @param locale
297: * Locale, to get the message for
298: *
299: * @return message
300: * String of the message
301: *
302: *
303: */
304: public static String getMessage(String key, Locale locale)
305: throws MissingResourceException {
306: String message = "";
307: if (locale == null) {
308: locale = DEFAULTLOCALE;
309: }
310: ResourceBundle messages = ResourceBundle.getBundle("Messages",
311: locale);
312: message = messages.getString(key.trim());
313:
314: return message;
315: }
316:
317: /**
318: * Get the i18n message string for a given key and locale
319: *
320: * @param key
321: * String - name of the key to get the message for
322: * @param locale
323: * Locale, to get the message for
324: *
325: * @return message
326: * String of the message
327: *
328: *
329: */
330: public static String getMessage(String key, Context c)
331: throws MissingResourceException {
332: String message = getMessage(key.trim(), c.getCurrentLocale());
333: return message;
334: }
335:
336: /**
337: * Get the appropriate localized version of the default.license according to language settings
338: *
339: * @param context
340: * the current DSpace context
341: * @return fileName
342: * String - localized filename for default.license
343: */
344: public static String getDefaultLicense(Context context) {
345: Locale locale = context.getCurrentLocale();
346: String fileName = "";
347: /** Name of the default license */
348: final String DEF_LIC_FILE = "default";
349: final String FILE_TYPE = ".license";
350: String defsFilename = ConfigurationManager
351: .getProperty("dspace.dir")
352: + File.separator
353: + "config"
354: + File.separator
355: + DEF_LIC_FILE;
356:
357: fileName = getFilename(locale, defsFilename, FILE_TYPE);
358:
359: return fileName;
360: }
361:
362: /**
363: * Get the appropriate localized version of a file according to language settings
364: * e. g. help files in jsp/help/
365: *
366: * @param locale
367: * Locale to get the file for
368: * @param fileName
369: * String fileName, to get the localized file for
370: * @param fileType
371: * String file extension
372: * @return localizedFileName
373: * String - localized filename
374: */
375: private static String getFilename(Locale locale, String fileName,
376: String fileType) {
377: String localizedFileName = null;
378: boolean fileFound = false;
379: // with Language, Country, Variant
380: String fileNameLCV = null;
381: // with Language, Country
382: String fileNameLC = null;
383: // with Language
384: String fileNameL = null;
385: fileNameL = fileName + "_" + locale.getLanguage();
386:
387: if (fileType == null) {
388: fileType = "";
389: }
390:
391: if (!("".equals(locale.getCountry()))) {
392: fileNameLC = fileName + "_" + locale.getLanguage() + "_"
393: + locale.getCountry();
394:
395: if (!("".equals(locale.getVariant()))) {
396: fileNameLCV = fileName + "_" + locale.getLanguage()
397: + "_" + locale.getCountry() + "_"
398: + locale.getVariant();
399: }
400: }
401:
402: if (fileNameLCV != null && !fileFound) {
403: File fileTmp = new File(fileNameLCV + fileType);
404: if (fileTmp.exists()) {
405: fileFound = true;
406: localizedFileName = fileNameLCV + fileType;
407: }
408: }
409:
410: if (fileNameLC != null && !fileFound) {
411: File fileTmp = new File(fileNameLC + fileType);
412: if (fileTmp.exists()) {
413: fileFound = true;
414: localizedFileName = fileNameLC + fileType;
415: }
416: }
417:
418: if (fileNameL != null && !fileFound) {
419: File fileTmp = new File(fileNameL + fileType);
420: if (fileTmp.exists()) {
421: fileFound = true;
422: localizedFileName = fileNameL + fileType;
423: }
424: }
425: if (!fileFound) {
426: localizedFileName = fileName + fileType;
427: }
428: return localizedFileName;
429: }
430:
431: /**
432: * Get the appropriate localized version of an email template according to language settings
433: *
434: * @param locale
435: * Locale for this request
436: * @param name
437: * String - base name of the email template
438: * @return templateName
439: * String - localized filename of an email template
440: */
441: public static String getEmailFilename(Locale locale, String name) {
442: String templateName = "";
443: String templateFile = ConfigurationManager
444: .getProperty("dspace.dir")
445: + File.separator
446: + "config"
447: + File.separator
448: + "emails"
449: + File.separator + name;
450:
451: templateName = getFilename(locale, templateFile, "");
452: return templateName;
453: }
454:
455: }
|