From 549ed08a6b3a62ca9e288efef3a90d7d5f2f0fd7 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 19 Jan 2005 21:13:18 +0000 Subject: [PATCH] Added support for exists(key) in the I18nTool. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1575 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../samskivert/servlet/MessageManager.java | 67 +++++++++++++------ .../com/samskivert/velocity/I18nTool.java | 10 ++- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/servlet/MessageManager.java b/projects/samskivert/src/java/com/samskivert/servlet/MessageManager.java index cd9777bd..b0e7bbe6 100644 --- a/projects/samskivert/src/java/com/samskivert/servlet/MessageManager.java +++ b/projects/samskivert/src/java/com/samskivert/servlet/MessageManager.java @@ -3,7 +3,7 @@ // // 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 @@ -75,12 +75,50 @@ public class MessageManager _siteIdent = siteIdent; } + /** + * Return true if the specifed path exists in the resource bundle. + */ + public boolean exists (HttpServletRequest req, String path) + { + return (getMessage(req, path, false) != null); + } + /** * Looks up the message with the specified path in the resource bundle * most appropriate for the locales described as preferred by the - * request. + * request. Always reports missing paths. */ public String getMessage (HttpServletRequest req, String path) + { + return getMessage(req, path, true); + } + + /** + * Looks up the message with the specified path in the resource bundle + * most appropriate for the locales described as preferred by the + * request, then substitutes the supplied arguments into that message + * using a MessageFormat object. + * + * @see java.text.MessageFormat + */ + public String getMessage (HttpServletRequest req, String path, + Object[] args) + { + String msg = getMessage(req, path, true); + // we may cache message formatters later, but for now just + // use the static convenience function + return MessageFormat.format(msg, args); + } + + /** + * Looks up the message with the specified path in the resource bundle + * most appropriate for the locales described as preferred by the + * request. If requested it will log a missing path and return the + * path as the translation (which should make it obvious in the + * servlet that the translation is missing) otherwise it returns null. + */ + protected String getMessage (HttpServletRequest req, String path, + boolean reportMissing) { if (path == null) { return "[null message key]"; @@ -128,26 +166,13 @@ public class MessageManager } } - // if there's no translation for this path, complain about it - Log.warning("Missing translation message [path=" + path + "]."); - return path; - } + if (reportMissing) { + // if there's no translation for this path, complain about it + Log.warning("Missing translation message [path=" + path + "]."); + return path; + } - /** - * Looks up the message with the specified path in the resource bundle - * most appropriate for the locales described as preferred by the - * request, then substitutes the supplied arguments into that message - * using a MessageFormat object. - * - * @see java.text.MessageFormat - */ - public String getMessage (HttpServletRequest req, String path, - Object[] args) - { - String msg = getMessage(req, path); - // we may cache message formatters later, but for now just - // use the static convenience function - return MessageFormat.format(msg, args); + return null; } /** diff --git a/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java b/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java index 329f28cc..ec624b0c 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java @@ -3,7 +3,7 @@ // // 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 @@ -61,6 +61,14 @@ public class I18nTool _msgmgr = msgmgr; } + /** + * Returns true if the key exists. + */ + public boolean exists (String key) + { + return _msgmgr.exists(_req, key); + } + /** * Looks up the specified message and returns the translation string. */