Source code: com/aendvari/common/notices/ResourceNoticeTranslator.java
1 /*
2 * ResourceNoticeTranslator.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.common.notices;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Iterator;
15 import java.util.Map;
16 import java.util.HashMap;
17 import java.util.Locale;
18 import java.util.ResourceBundle;
19
20 import java.text.MessageFormat;
21
22
23 /**
24 * <p>Provides the ability to translate formatted notices into plain text.</p>
25 *
26 * <p>The text of the notice is used as a <code>java.util.ResourceBundle</code> key.</p>
27 *
28 * @author Scott Milne
29 *
30 */
31
32 public class ResourceNoticeTranslator implements NoticeTranslator
33 {
34 /** The resource bundle to use for parsing notices. */
35 private ResourceBundle resourceBundle;
36
37 /**
38 * Constructs a {@link ResourceNoticeTranslator} with a default locale.
39 *
40 * @param resourcePath The class path to the resource file.
41 *
42 */
43
44 public ResourceNoticeTranslator(String resourcePath)
45 {
46 // get resource information
47 resourceBundle = ResourceBundle.getBundle(resourcePath);
48 }
49
50 /**
51 * Constructs a {@link ResourceNoticeTranslator} with the given resource.
52 *
53 * @param resourcePath The class path to the resource file.
54 * @param locale The {@link Locale} to use for translation.
55 *
56 */
57
58 public ResourceNoticeTranslator(String resourcePath, Locale locale)
59 {
60 // get resource information
61 resourceBundle = ResourceBundle.getBundle(resourcePath, locale);
62 }
63
64 /**
65 * Translates a single notice.
66 *
67 * @param notice The {@link Notice} object to translate.
68 *
69 * @return A string containing the translated notice text.
70 *
71 */
72
73 public String translate(Notice notice)
74 {
75 // use notice text as resource bundle key
76 String text = resourceBundle.getString(notice.getNotice());
77
78 // format text
79 MessageFormat noticeFormat = new MessageFormat(text);
80 noticeFormat.setLocale(resourceBundle.getLocale());
81
82 String parsedNotice = noticeFormat.format(notice.getValues());
83
84 return parsedNotice;
85 }
86
87 /**
88 * Translates a <code>Collection</code> of notices.
89 *
90 * @param notices A <code>Collection</code> of {@link Notice} objects to translate.
91 *
92 * @return A <code>Collection</code> of strings containing translated notice text.
93 *
94 */
95
96 public Collection translate(Collection notices)
97 {
98 Collection translated = new ArrayList();
99 Iterator noticeIterator = notices.iterator();
100
101 // translate each notice
102 while (noticeIterator.hasNext())
103 {
104 Notice notice = (Notice)noticeIterator.next();
105 String text = translate(notice);
106
107 translated.add(text);
108 }
109
110 return translated;
111 }
112
113 /**
114 * Translates an array of notices.
115 *
116 * @param notices An array of {@link Notice} objects to translate.
117 *
118 * @return An array of strings containing translated notice text.
119 *
120 */
121
122 public String[] translate(Notice[] notices)
123 {
124 String[] translated = new String[notices.length];
125 int noticeIndex;
126
127 // translate each notice
128 for (noticeIndex = 0; noticeIndex < notices.length; noticeIndex++)
129 {
130 translated[noticeIndex] = translate(notices[noticeIndex]);
131 }
132
133 return translated;
134 }
135 }
136