001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.control.impl;
017:
018: import java.text.MessageFormat;
019: import java.util.Locale;
020: import java.util.MissingResourceException;
021: import java.util.ResourceBundle;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: /**
026: * I18n support using java resource bundles.
027: *
028: * @author Christoph Beck
029: */
030: public class DefaultI18nSupport extends
031: de.odysseus.calyxo.base.I18nSupport {
032: public static final String LOCALE_KEY = "de.odysseus.calyxo.base.i18n.locale";
033:
034: /**
035: * Get the desired locale.
036: * This implementation tries to get a cached locale set by
037: * the <code>setLocale</code> method.
038: */
039: public Locale getLocale(HttpServletRequest request) {
040: Locale locale = (Locale) request.getSession().getAttribute(
041: LOCALE_KEY);
042: if (locale == null) {
043: locale = request.getLocale();
044: }
045: return locale;
046: }
047:
048: /**
049: * Save the specified desired locale for the request's session.
050: */
051: public void setLocale(HttpServletRequest request, Locale locale) {
052: request.getSession().setAttribute(LOCALE_KEY, locale);
053: }
054:
055: /**
056: * Lookup resource.
057: * <code>MissingResourceException</code>s are caught and the
058: * string <code>"???" + key + "???"</code> is answered.
059: */
060: public String lookupResource(Locale locale, String bundle,
061: String key) {
062: if (bundle == null)
063: return "???" + key + "???";
064: try {
065: return ResourceBundle.getBundle(bundle, locale).getString(
066: key);
067: } catch (MissingResourceException e) {
068: return "???" + key + "???";
069: }
070: }
071:
072: /**
073: * Replace "'" by "''"
074: */
075: private String escape(String string) {
076: if (string == null || string.indexOf('\'') < 0) {
077: return string;
078: }
079: int n = string.length();
080: StringBuffer sb = new StringBuffer(n);
081: for (int i = 0; i < n; i++) {
082: char c = string.charAt(i);
083: if (c == '\'')
084: sb.append('\'');
085: sb.append(c);
086: }
087: return sb.toString();
088: }
089:
090: /**
091: * Lookup formatted message string.
092: * <code>MissingResourceException</code>s are caught and a
093: * generic message string of the form
094: * <code>"???" + key + "(" + arg1 + "," + "...)???"</code>
095: * is answered.
096: */
097: protected String lookupMessage(Locale locale, String bundle,
098: String key, Object[] values) {
099: String template = null;
100: if (bundle != null) {
101: try {
102: template = ResourceBundle.getBundle(bundle, locale)
103: .getString(key);
104: } catch (MissingResourceException e) {
105: }
106: }
107: if (template == null) {
108: StringBuffer sb = new StringBuffer();
109: sb.append("???");
110: sb.append(key);
111: sb.append("(");
112: for (int i = 0; i < values.length; i++) {
113: if (i > 0) {
114: sb.append(",");
115: }
116: sb.append(values[i]);
117: }
118: sb.append(")???");
119: return sb.toString();
120: } else {
121: return MessageFormat.format(escape(template), values);
122: }
123: }
124: }
|