LocalStringsManagerpublic class LocalStringsManager extends Object Implementation of a local string manager.
Provides access to i18n messages for classes that need them.
Now emulates the proper sun.enterprise.util.LocalStringManagerImpl
with few essential differences:
1. It uses package hierarchy, but not classes hierarchy for resource files searching(it allows to place resources separatly from source files)
2. You can choose different resource file names (not only LocalName.properties);
3. New optional mode - with fixedResourceBundle allows to exclude bundle file search repeating during the subsequent getString() calls; |
Fields Summary |
---|
static final String | DEFAULT_PROPERTY_FILE_NAME | private String | m_propertyFileName | private String | m_basePackage | private ResourceBundle | m_FixedResourceBundle |
Constructors Summary |
---|
public LocalStringsManager(String basePackage, String propertyFileName)Create a string manager that looks for LocalStrings.properties in
the package of the basePackage.
m_basePackage = basePackage;
m_propertyFileName = propertyFileName;
| public LocalStringsManager(String basePackage)
this(basePackage, DEFAULT_PROPERTY_FILE_NAME);
|
Methods Summary |
---|
public java.lang.String | getString(java.lang.String startPackage, java.lang.String key, java.lang.String defaultValue)Get a localized string.
if(m_FixedResourceBundle!=null)
{
String value = m_FixedResourceBundle.getString(key);
if(value!=null)
return value;
return defaultValue;
}
String stopPackage = m_basePackage;
if(startPackage==null)
startPackage = stopPackage;
else
if(!startPackage.startsWith(m_basePackage))
stopPackage = startPackage; //maybe = ""
ResourceBundle resources = null;
while (startPackage.length()>0 && startPackage.length()>=stopPackage.length())
{
try
{
String resFileName = startPackage+"."+m_propertyFileName;
startPackage = startPackage.substring(0, startPackage.lastIndexOf("."));
resources = ResourceBundle.getBundle(resFileName);
if ( resources != null )
{
String value = resources.getString(key);
if ( value != null )
return value;
}
} catch (Exception ex)
{
}
}
// Look for a global resource (defined by defaultClass)
if ( !stopPackage.equals(m_basePackage) )
{
return getString(null, key, defaultValue);
}
return defaultValue;
| public java.lang.String | getString(java.lang.String key, java.lang.String defaultValue)Get a localized string from the base package.
return getString(null, key, defaultValue);
| public java.lang.String | getString(java.lang.String startPackage, java.lang.String key, java.lang.String defaultFormat, java.lang.Object[] arguments)Get a local string for the caller and format the arguments accordingly.
MessageFormat f = new MessageFormat(
getString(startPackage, key, defaultFormat));
for (int i = 0; i < arguments.length; i++)
{
if ( arguments[i] == null )
{
arguments[i] = "null";
} else if ( !(arguments[i] instanceof String) &&
!(arguments[i] instanceof Number) &&
!(arguments[i] instanceof java.util.Date))
{
arguments[i] = arguments[i].toString();
}
}
return f.format(arguments);
| public java.lang.String | getString(java.lang.String key, java.lang.String defaultFormat, java.lang.Object[] arguments)Get a local string from the base package and
format the arguments accordingly.
return getString(null, key, defaultFormat, arguments);
| protected void | setFixedResourceBundle(java.lang.String startPackage)
if(startPackage==null)
{
m_FixedResourceBundle = null;
return;
}
String stopPackage = m_basePackage;
if(startPackage==null)
startPackage = stopPackage;
else
if(!startPackage.startsWith(m_basePackage))
stopPackage = startPackage; //maybe = ""
ResourceBundle resources = null;
while (startPackage.length()>0 && startPackage.length()>=stopPackage.length())
{
try
{
String resFileName = startPackage+"."+m_propertyFileName;
startPackage = startPackage.substring(0, startPackage.lastIndexOf("."));
resources = ResourceBundle.getBundle(resFileName);
if ( resources != null )
{
m_FixedResourceBundle = resources;
return;
}
} catch (Exception ex)
{
}
}
// Look for a global resource (defined by defaultClass)
if ( !stopPackage.equals(m_basePackage) )
{
setFixedResourceBundle(m_basePackage);
}
|
|