Source code: com/aendvari/common/notices/FormattedNoticeTranslator.java
1 /*
2 * FormattedNoticeTranslator.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.Locale;
16
17 import java.text.MessageFormat;
18
19
20 /**
21 * <p>Provides the ability to translate formatted notices into plain text.</p>
22 *
23 * <p>See the <code>java.text.MessageFormat</code> class for information on creating
24 * formatted text</p>
25 *
26 * @author Scott Milne
27 *
28 */
29
30 public class FormattedNoticeTranslator implements NoticeTranslator
31 {
32 /** The <code>Locale</code> to use for translation. */
33 private Locale locale;
34
35 /**
36 * Constructs a {@link FormattedNoticeTranslator}.
37 *
38 * @param setLocale The <code>Locale</code> to use for translation.
39 *
40 */
41
42 public FormattedNoticeTranslator(Locale setLocale)
43 {
44 locale = setLocale;
45 }
46
47 /**
48 * Constructs a {@link FormattedNoticeTranslator}.
49 *
50 */
51
52 public FormattedNoticeTranslator()
53 {
54 locale = null;
55 }
56
57 /**
58 * Translates a single notice.
59 *
60 * @param notice The {@link Notice} object to translate.
61 *
62 * @return A string containing the translated notice text.
63 *
64 */
65
66 public String translate(Notice notice)
67 {
68 MessageFormat noticeFormat = new MessageFormat(notice.getNotice());
69
70 if (locale != null)
71 noticeFormat.setLocale(locale);
72
73 String parsedNotice = noticeFormat.format(notice.getValues());
74
75 return parsedNotice;
76 }
77
78 /**
79 * Translates a <code>Collection</code> of notices.
80 *
81 * @param notices A <code>Collection</code> of {@link Notice} objects to translate.
82 *
83 * @return A <code>Collection</code> of strings containing translated notice text.
84 *
85 */
86
87 public Collection translate(Collection notices)
88 {
89 Collection translated = new ArrayList();
90 Iterator noticeIterator = notices.iterator();
91
92 // translate each notice
93 while (noticeIterator.hasNext())
94 {
95 Notice notice = (Notice)noticeIterator.next();
96 String text = translate(notice);
97
98 translated.add(text);
99 }
100
101 return translated;
102 }
103
104 /**
105 * Translates an array of notices.
106 *
107 * @param notices An array of {@link Notice} objects to translate.
108 *
109 * @return An array of strings containing translated notice text.
110 *
111 */
112
113 public String[] translate(Notice[] notices)
114 {
115 String[] translated = new String[notices.length];
116 int noticeIndex;
117
118 // translate each notice
119 for (noticeIndex = 0; noticeIndex < notices.length; noticeIndex++)
120 {
121 translated[noticeIndex] = translate(notices[noticeIndex]);
122 }
123
124 return translated;
125 }
126 }
127