001: package org.apache.turbine.services.localization;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.List;
023: import java.util.Locale;
024: import java.util.MissingResourceException;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.turbine.services.pull.ApplicationTool;
029: import org.apache.turbine.util.RunData;
030:
031: /**
032: * A pull tool which provides lookups for localized text by delegating
033: * to the configured <code>LocalizationService</code>.
034: *
035: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
036: * @author <a href="mailto:jon@collab.net">Jon Stevens</a>
037: * @version $Id: LocalizationTool.java 551164 2007-06-27 14:04:14Z seade $
038: */
039: public class LocalizationTool implements ApplicationTool {
040: /** Logging */
041: private static Log log = LogFactory.getLog(LocalizationTool.class);
042:
043: /**
044: * The language and country information parsed from the request's
045: * <code>Accept-Language</code> header. Reset on each request.
046: */
047: protected Locale locale;
048:
049: /**
050: * The name of the bundle for this tool to use.
051: */
052: protected String bundleName;
053:
054: /**
055: * Creates a new instance. Used by <code>PullService</code>.
056: */
057: public LocalizationTool() {
058: refresh();
059: }
060:
061: /**
062: * <p>Performs text lookups for localization.</p>
063: *
064: * <p>Assuming there is a instance of this class with a HTTP
065: * request set in your template's context named <code>l10n</code>,
066: * the VTL <code>$l10n.HELLO</code> would render to
067: * <code>hello</code> for English requests and <code>hola</code>
068: * in Spanish (depending on the value of the HTTP request's
069: * <code>Accept-Language</code> header).</p>
070: *
071: * @param key The identifier for the localized text to retrieve.
072: * @return The localized text.
073: */
074: public String get(String key) {
075: try {
076: return Localization.getString(getBundleName(null),
077: getLocale(), key);
078: } catch (MissingResourceException noKey) {
079: log.error(noKey);
080: return null;
081: }
082: }
083:
084: /**
085: * Gets the current locale.
086: *
087: * @return The locale currently in use.
088: */
089: public Locale getLocale() {
090: return locale;
091: }
092:
093: /**
094: * The return value of this method is used to set the name of the
095: * bundle used by this tool. Useful as a hook for using a
096: * different bundle than specifed in your
097: * <code>LocalizationService</code> configuration.
098: *
099: * @param data The inputs passed from {@link #init(Object)}.
100: * (ignored by this implementation).
101: */
102: protected String getBundleName(Object data) {
103: return bundleName;
104: }
105:
106: /**
107: * Formats a localized value using the provided object.
108: *
109: * @param key The identifier for the localized text to retrieve,
110: * @param arg1 The object to use as {0} when formatting the localized text.
111: * @return Formatted localized text.
112: * @see #format(String, Locale, String, Object[])
113: */
114: public String format(String key, Object arg1) {
115: return Localization.format(getBundleName(null), getLocale(),
116: key, arg1);
117: }
118:
119: /**
120: * Formats a localized value using the provided objects.
121: *
122: * @param key The identifier for the localized text to retrieve,
123: * @param arg1 The object to use as {0} when formatting the localized text.
124: * @param arg2 The object to use as {1} when formatting the localized text.
125: * @return Formatted localized text.
126: * @see #format(String, Locale, String, Object[])
127: */
128: public String format(String key, Object arg1, Object arg2) {
129: return Localization.format(getBundleName(null), getLocale(),
130: key, arg1, arg2);
131: }
132:
133: /**
134: * Formats a localized value using the provided objects.
135: *
136: * @param key The identifier for the localized text to retrieve,
137: * @param args The objects to use as {0}, {1}, etc. when
138: * formatting the localized text.
139: * @return Formatted localized text.
140: */
141: public String format(String key, Object[] args) {
142: return Localization.format(getBundleName(null), getLocale(),
143: key, args);
144: }
145:
146: /**
147: * Formats a localized value using the provided objects. This variation
148: * allows for a List so that the velocity ["arg1", "arg2", "arg3"] syntax
149: * is supported.
150: *
151: * @param key The identifier for the localized text to retrieve,
152: * @param args The objects to use as {0}, {1}, etc. when
153: * formatting the localized text.
154: * @return Formatted localized text.
155: */
156: public String format(String key, List args) {
157: return Localization.format(getBundleName(null), getLocale(),
158: key, args.toArray());
159: }
160:
161: // ApplicationTool implmentation
162:
163: /**
164: * Sets the request to get the <code>Accept-Language</code> header
165: * from (reset on each request).
166: */
167: public void init(Object data) {
168: if (data instanceof RunData) {
169: // Pull necessary information out of RunData while we have
170: // a reference to it.
171: locale = Localization.getLocale(((RunData) data)
172: .getRequest());
173: bundleName = Localization.getDefaultBundleName();
174: }
175: }
176:
177: /**
178: * No-op.
179: */
180: public void refresh() {
181: locale = null;
182: bundleName = null;
183: }
184: }
|