001: package com.teamkonzept.international;
002:
003: import java.util.*;
004: import java.text.MessageFormat;
005: import org.apache.log4j.Category;
006: import com.teamkonzept.web.TKEvent;
007:
008: /**
009: * The language manager.
010: *
011: * @author $Author: uli $
012: * @version $Revision: 1.25 $
013: */
014: public class LanguageManager implements LanguageSelection {
015: private static Hashtable contexts;
016:
017: private static Category cat = Category
018: .getInstance(LanguageManager.class);
019:
020: private static Hashtable CACHED_LOCALES = new Hashtable();
021:
022: public final static String GENERAL = "general";
023:
024: static {
025: contexts = new Hashtable();
026: registerContext("/content", LanguageManager.class);
027: registerContext("/general", LanguageManager.class);
028: registerContext("/workflow", LanguageManager.class);
029: registerContext("/form", LanguageManager.class);
030: registerContext("/field", LanguageManager.class);
031: registerContext("/contenttags", LanguageManager.class);
032: registerContext("/documenttype", LanguageManager.class);
033: registerContext("/copytool", LanguageManager.class);
034: registerContext("/properties", LanguageManager.class);
035: registerContext("/start", LanguageManager.class);
036: registerContext("/accessrights", LanguageManager.class);
037: registerContext("/templates", LanguageManager.class);
038: registerContext("/sitestructure", LanguageManager.class);
039: registerContext("/gui", LanguageManager.class);
040: }
041:
042: /** aktuell gesetztes Server Locale */
043: private static Locale serverLocale = Locale.GERMAN;
044:
045: /** aktuell gesetzte LanguageSelektion */
046: private static int languageCode = USER_CHOSEN_LOCALE;
047:
048: /**
049: registriert Kontext und den Ort -> Properties mussen also in dem Package der uebergebenen
050: Klasse liegen
051: @param context name des Kontexts
052: @param source die aufrufende Klasse, um den Ort des PropertyFiles zu bestimmen
053: */
054: public static void registerContext(String context, Object source) {
055: Class cl = source.getClass();
056: String name = cl.getName();
057: int index = name.lastIndexOf(".");
058: if (index == -1 || context.startsWith("/")) {
059: // kein Package
060: if (context.startsWith("/")) {
061: contexts.put(context.substring(1, context.length()),
062: context.substring(1, context.length()));
063: } else
064: contexts.put(context, context);
065: } else {
066: contexts.put(context, name.substring(0, index + 1)
067: + context);
068: }
069: }
070:
071: /**
072: * Returns the specified internationalized text token.
073: *
074: * @param context the name of the context.
075: * @param identifier the identifier of the text token.
076: * @return the specified internationalized text token.
077: */
078: public static String getText(String context, String identifier) {
079: return getText(context, identifier, null);
080: }
081:
082: /**
083: * Returns the specified internationalized text token.
084: *
085: * @param context the name of the context.
086: * @param identifier the identifier of the text token.
087: * @param arguments the arguments of the text token.
088: * @return the specified internationalized text token.
089: */
090: public static String getText(String context, String identifier,
091: Object[] arguments) {
092: try {
093: String location = (String) contexts.get(context);
094: if (location == null) {
095: cat.error("Unknown location '" + context + "'");
096: return "";
097: }
098: ResourceBundle bundle = null;
099: if (languageCode == SERVER_CURRENT_LOCALE) {
100: bundle = ResourceBundle.getBundle(location,
101: serverLocale);
102: } else if (languageCode == USER_CHOSEN_LOCALE) {
103: TKEvent evt = TKEvent.getEventForThread();
104: if (evt != null) {
105: String language = evt.getSelectedLanguage();
106: Locale locale;
107: if (language == null) {
108: locale = serverLocale;
109: } else {
110: locale = getLocale(language);
111: }
112: bundle = ResourceBundle.getBundle(location, locale);
113: } else {
114: // Generator !!!
115: bundle = ResourceBundle.getBundle(location,
116: serverLocale);
117: }
118: }
119: String text = bundle.getString(identifier);
120: if (arguments != null && arguments.length > 0) {
121: text = MessageFormat.format(text, arguments);
122: }
123:
124: return text;
125: } catch (MissingResourceException e) {
126: cat.error("No values for '" + identifier + "' in context '"
127: + context + "'");
128: } catch (Exception e) {
129: cat.error("Unknown error", e);
130: }
131: return "";
132: }
133:
134: /**
135: * holt eine gecachte Locale instanz oder erzeugt eine neue
136: */
137: public static Locale getLocale(String language) {
138: Locale locale = (Locale) CACHED_LOCALES.get(language);
139: if (locale == null) {
140: locale = new Locale(language, "");
141: CACHED_LOCALES.put(language, locale);
142: }
143: return locale;
144: }
145:
146: public static Locale getStandardLocale() {
147: return serverLocale;
148: }
149:
150: public static void setLanguageSelection(int code) {
151: languageCode = code;
152: // noch was machen ???
153: }
154:
155: public static void setLocale(Locale locale) {
156: serverLocale = locale;
157: }
158:
159: /**
160: * returns the language as a ISO 639 language code, as it would be used
161: * for a getText() method call. */
162: public static String getAppliableLanguage() {
163: Locale locale = null;
164: if (languageCode == SERVER_CURRENT_LOCALE) {
165: locale = serverLocale;
166: } else if (languageCode == USER_CHOSEN_LOCALE) {
167: TKEvent evt = TKEvent.getEventForThread();
168: if (evt != null) {
169: String language = evt.getSelectedLanguage();
170: if (language == null) {
171: locale = serverLocale;
172: } else {
173: locale = getLocale(language);
174: }
175: } else {
176: locale = serverLocale;
177: }
178: }
179:
180: return (locale != null ? locale.getLanguage() : null);
181: }
182: }
|