001: package dtw.webmail.util.config;
002:
003: import dtw.webmail.util.StringUtil;
004:
005: import java.util.*;
006: import java.io.Serializable;
007:
008: /**
009: * Class representing a model for i18n configuration
010: * data.
011: * <p>
012: * @author Dieter Wimberger (wimpi)
013: * @version (created Feb 24, 2003)
014: */
015: public class Internationalization implements Serializable {
016:
017: private Locale m_SystemLocale;
018: private Locale m_DefaultViewLocale;
019: private Set m_ViewLocales;
020:
021: //private Set m_ViewLanguages;
022:
023: public Internationalization() {
024: //m_ViewLanguages = Collections.synchronizedSet(new TreeSet());
025: m_ViewLocales = Collections.synchronizedSet(new HashSet(10));
026: }//constructor
027:
028: /**
029: * Returns the system language in ISO two-letter
030: * format.
031: *
032: * @return the language code as <tt>String</tt>.
033: */
034: public String getSystemLanguage() {
035: return m_SystemLocale.getLanguage();
036: }//getSystemLanguage
037:
038: /**
039: * Sets the system language. The parameter should be
040: * an ISO two-letter format string.
041: *
042: * @param language the language as <tt>String</tt>.
043: */
044: public void setSystemLanguage(String language) {
045: m_SystemLocale = new Locale(language, "");
046: }//setSystemLanguage
047:
048: /**
049: * Returns the system locale.
050: *
051: * @return a <tt>Lcoale</tt> instance.
052: */
053: public Locale getSystemLocale() {
054: return m_SystemLocale;
055: }//getSystemLocale
056:
057: /**
058: * Sets the system locale.
059: *
060: * @param locale a <tt>Locale</tt> instance.
061: */
062: public void setSystemLocale(Locale locale) {
063: m_SystemLocale = locale;
064: }//setSystemLocale
065:
066: /**
067: * Returns the default view language in ISO two-letter
068: * format.
069: *
070: * @return the language code as <tt>String</tt>.
071: */
072: public String getDefaultViewLanguage() {
073: return m_DefaultViewLocale.getLanguage();
074: }//getDefaultViewLanguage
075:
076: /**
077: * Sets the default view language. The parameter should be
078: * an ISO two-letter format string.
079: *
080: * @param language the language as <tt>String</tt>.
081: */
082: public void setDefaultViewLanguage(String language) {
083: m_DefaultViewLocale = new Locale(language, "");
084: }//setDefaultViewLanguage
085:
086: /**
087: * Returns the default view locale.
088: *
089: * @return a <tt>Lcoale</tt> instance.
090: */
091: public Locale getDefaultViewLocale() {
092: return m_DefaultViewLocale;
093: }//getDefaultViewLocale
094:
095: /**
096: * Sets the default view locale.
097: *
098: * @param locale a <tt>Locale</tt> instance.
099: */
100: public void setDefaultViewLocale(Locale locale) {
101: m_DefaultViewLocale = locale;
102: }//setDefaultViewLocale
103:
104: /**
105: * Convenience method for obtaining the supported languages
106: * (represented in ISO two letter code) as a simple
107: * comma seperated list.
108: * <p>
109: * Note that this is supposed to be helpful for simplifying
110: * the persistency mechanism.
111: *
112: * @return the list as <tt>String</tt>.
113: */
114: public String getViewLanguageList() {
115: return StringUtil.join(listViewLanguages(), ",");
116: }//getViewLanguageList
117:
118: /**
119: * Convenience method for setting the supported languages
120: * (represented in ISO two letter code) as a simple
121: * comma seperated list.
122: * <p>
123: * Note that this is supposed to be helpful for simplifying
124: * the persistency mechanism.
125: *
126: * @return the list as <tt>String</tt>.
127: */
128: public void setViewLanguageList(String list) {
129: String[] locales = StringUtil.split(list, ",");
130: m_ViewLocales.clear();
131: for (int i = 0; i < locales.length; i++) {
132: addViewLocale(new Locale(locales[i], ""));
133: }
134: }//setViewLanguageList
135:
136: /**
137: * Returns the list of languages (by their ISO two
138: * letter code).
139: *
140: * @return the list as <tt>String[]</tt>.
141: */
142: public String[] listViewLanguages() {
143: String[] strs = new String[m_ViewLocales.size()];
144: int i = 0;
145: for (Iterator iterator = m_ViewLocales.iterator(); iterator
146: .hasNext(); i++) {
147: strs[i] = ((Locale) iterator.next()).getLanguage();
148: }
149: return strs;
150: }//listViewLanguages
151:
152: /**
153: * Returns a list of view languages as
154: * locale instances.
155: *
156: * @return the view languages as <tt>Locale[]</tt>.
157: */
158: public Locale[] listViewLocales() {
159: Locale[] strs = new Locale[m_ViewLocales.size()];
160: return (Locale[]) m_ViewLocales.toArray(strs);
161: }//listViewLocales
162:
163: /**
164: * Adds a language, specifying it's ISO two letter
165: * code.
166: *
167: * @param lang the language as <tt>String</tt>.
168: */
169: public void addViewLanguage(String lang) {
170: m_ViewLocales.add(new Locale(lang, ""));
171: }//addViewLanguage
172:
173: /**
174: * Adds a view locale.
175: *
176: * @param locale a <tt>Locale</tt> instance.
177: */
178: public void addViewLocale(Locale locale) {
179: m_ViewLocales.add(locale);
180: }//addViewLocale
181:
182: /**
183: * Removes a view language.
184: *
185: * @param lang the language as <tt>String</tt>.
186: */
187: public void removeViewLanguage(String lang) {
188: m_ViewLocales.remove(new Locale(lang, ""));
189: }//removeViewLanguage
190:
191: /**
192: * Removes a view locale.
193: *
194: * @param locale a <tt>Locale</tt> instance.
195: */
196: public void removeViewLocale(Locale locale) {
197: m_ViewLocales.remove(locale);
198: }//removeViewLocale
199:
200: /**
201: * Tests if a given language (by ISO two letter code)
202: * is supported.
203: *
204: * @param lang a <tt>String</tt> representing a language.
205: * @return true if supported, false otherwise.
206: */
207: public boolean isSupportedViewLanguage(String lang) {
208: return m_ViewLocales.contains(new Locale(lang, ""));
209: }//isSupportedViewLanguage
210:
211: /**
212: * Tests if a given locale is supported.
213: *
214: * @param loc a <tt>Locale</tt> instance.
215: * @return true if supported, false otherwise.
216: */
217: public boolean isSupportedViewLocale(Locale loc) {
218: return m_ViewLocales.contains(loc);
219: }//isSupportedViewLocale
220:
221: }//class Internationalization
|