diff --git a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java index 1fbf484a..eae4ed9b 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java @@ -1,5 +1,5 @@ // -// $Id: DispatcherServlet.java,v 1.15 2002/02/10 05:06:08 mdb Exp $ +// $Id: DispatcherServlet.java,v 1.16 2002/05/02 05:30:31 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -251,11 +251,11 @@ public class DispatcherServlet extends VelocityServlet ictx.put(APPLICATION_KEY, _app); // if the application provides a message manager, we want - // to put a message resolver in the context as well + // create a translation tool and stuff that into the context MessageManager msgmgr = _app.getMessageManager(); if (msgmgr != null) { - MessageResolver mrslv = new MessageResolver(msgmgr); - ictx.put(MSGRESOLVER_KEY, mrslv); + I18nTool i18n = new I18nTool(req, msgmgr); + ictx.put(I18NTOOL_KEY, i18n); } // create a form tool for use by the template @@ -400,14 +400,13 @@ public class DispatcherServlet extends VelocityServlet */ protected static final String APPLICATION_KEY = "%_app_%"; - /** This is the key used to store the message resolver in the - * context. */ - protected static final String MSGRESOLVER_KEY = "i18n"; + /** The key used to store the translation tool in the context. */ + protected static final String I18NTOOL_KEY = "i18n"; - /** This is the key used to store the form tool in the context. */ + /** The key used to store the form tool in the context. */ protected static final String FORMTOOL_KEY = "form"; - /** This is the key used to store the string tool in the context. */ + /** The key used to store the string tool in the context. */ protected static final String STRINGTOOL_KEY = "string"; /** The servlet parameter key specifying the application class. */ diff --git a/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java b/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java new file mode 100644 index 00000000..33a558af --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java @@ -0,0 +1,101 @@ +// +// $Id: I18nTool.java,v 1.1 2002/05/02 05:30:31 mdb Exp $ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.velocity; + +import javax.servlet.http.HttpServletRequest; + +import com.samskivert.servlet.MessageManager; + +/** + * Provides access to a set of application messages (translation strings) + * to templates that wish to display localized text. + * + *
If the tool is mapped into the context as i18n, then
+ * it might be accessed as follows:
+ *
+ *
+ * $i18n.xlate("main.intro", $user.username)
+ *
+ *
+ * where main.intro might be defined in the message resource
+ * file as:
+ *
+ *
+ * main.intro=Hello there {0}, welcome to our site!
+ *
+ *
+ * @see MessageManager
+ */
+public class I18nTool
+{
+ /**
+ * Constructs a new i18n tool which will use the supplied message
+ * manager to obtain translated strings and the supplied request
+ * instance to determine the locale in which to do so.
+ */
+ public I18nTool (HttpServletRequest req, MessageManager msgmgr)
+ {
+ _req = req;
+ _msgmgr = msgmgr;
+ }
+
+ /**
+ * Looks up the specified message and returns the translation string.
+ */
+ public String xlate (String key)
+ {
+ return _msgmgr.getMessage(_req, key);
+ }
+
+ /**
+ * Looks up the specified message and creates the translation string
+ * using the supplied argument.
+ */
+ public String xlate (String key, Object arg)
+ {
+ return _msgmgr.getMessage(_req, key, new Object[] { arg });
+ }
+
+ /**
+ * Looks up the specified message and creates the translation string
+ * using the supplied arguments.
+ */
+ public String xlate (String key, Object arg1, Object arg2)
+ {
+ return _msgmgr.getMessage(_req, key, new Object[] { arg1, arg2 });
+ }
+
+ /**
+ * Looks up the specified message and creates the translation string
+ * using the supplied arguments.
+ */
+ public String xlate (String key, Object arg1, Object arg2, Object arg3)
+ {
+ return _msgmgr.getMessage(_req, key,
+ new Object[] { arg1, arg2, arg3 });
+ }
+
+ /** The servlet request was are providing i18n messages for. */
+ protected HttpServletRequest _req;
+
+ /** The message manager we're using to provide i18n messages. */
+ protected MessageManager _msgmgr;
+}