Added support for exists(key) in the I18nTool.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1575 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
eric
2005-01-19 21:13:18 +00:00
parent 7429fcf836
commit 549ed08a6b
2 changed files with 55 additions and 22 deletions
@@ -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 <code>MessageFormat</code> 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 <code>MessageFormat</code> 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;
}
/**
@@ -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.
*/