|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectxv.text.NLS
public class NLS
This class is based on the Eclipse Native Language Support system. It does
not use ResourceBundle
, instead using
PropertiesReader
to parse the appropriate properties. The great thing
about this class versus ResourceBundle
is that it allows you to
specify the character encoding, so you can use property files written in
UTF-8 and not ISO 8859-1.
This reduces the amount of memory required.
The one side-effect is that the first property set takes precedence. So if
you set a property multiple times in a .properties file, only the first value
takes effect while with a ResourceBundle
only the last value will
take effect.
Of course, there's little reason to set the same property multiple times in a
.properties file, so this shouldn't come up too much, but be aware. Do note
that certain keys are considered equivalent. For example, "foo.bar"
is treated the same as "foo_bar"
.
Constructor Summary | |
---|---|
NLS()
|
Method Summary | |
---|---|
static String |
format(String format,
Object... arguments)
Formats a string. |
static void |
initialize(Class<?> cls,
String resource)
Initializes the given class, setting all public static String
fields that are non-final to the properties loaded from a set of
properties files, using the default locale. |
static void |
initialize(Class<?> cls,
String resource,
Locale locale)
Initializes the given class, setting all public static String
fields that are non-final to the properties loaded from a set of
properties files. |
static void |
initialize(Class<?> cls,
String resource,
Locale locale,
String encoding)
Initializes the given class, setting all public static String
fields that are non-final to the properties loaded from a set of
properties files. |
static HashMap<String,String> |
loadLocaleProperties(URL base)
Loads a map of strings to strings using a PropertiesReader ,
handling overrides in the same manner as initialize(Class, String, Locale) . |
static HashMap<String,String> |
loadLocaleProperties(URL base,
Locale locale,
String encoding)
Loads a map of strings to strings using a PropertiesReader ,
handling overrides in the same manner as initialize(Class, String, Locale) . |
static HashMap<String,String> |
loadLocaleProperties(URL base,
String encoding)
Loads a map of strings to strings using a PropertiesReader ,
handling overrides in the same manner as initialize(Class, String, Locale) . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public NLS()
Method Detail |
---|
public static void initialize(Class<?> cls, String resource)
public static String
fields that are non-final
to the properties loaded from a set of
properties files, using the default locale.
This class follows the same load order as
ResourceBundle.getBundle(String, Locale, ClassLoader)
.
There are some important differences. The first is that this
implementation only sets the first value found in a given
Properties
file, while ResourceBundle
will set the
last value found in a given Properties
file.
The second is that various keys are treated the same. '.'
are
replaced with '_'
in keys found. So assuming the given
.properties
file:
foo.bar = one foo_bar = two foo.bar = threeAnd the following class:
public class Example { public static String foo_bar; }Then the field
foo_bar
will be set to "one"
once this
method completes.
cls
- the class to set properties inresource
- the name of the resource, as a classname similar to what
ResourceBundle.getBundle(String)
takespublic static void initialize(Class<?> cls, String resource, Locale locale)
public static String
fields that are non-final
to the properties loaded from a set of
properties files.
This class follows the same load order as
ResourceBundle.getBundle(String, Locale, ClassLoader)
.
There are some important differences. The first is that this
implementation only sets the first value found in a given
Properties
file, while ResourceBundle
will set the
last value found in a given Properties
file.
The second is that various keys are treated the same. '.'
are
replaced with '_'
in keys found. So assuming the given
.properties
file:
foo.bar = one foo_bar = two foo.bar = threeAnd the following class:
public class Example { public static String foo_bar; }Then the field
foo_bar
will be set to "one"
once this
method completes.
cls
- the class to set properties inresource
- the name of the resource, as a classname similar to what
ResourceBundle.getBundle(String)
takeslocale
- the locale to load frompublic static void initialize(Class<?> cls, String resource, Locale locale, String encoding)
public static String
fields that are non-final
to the properties loaded from a set of
properties files.
This class follows the same load order as
ResourceBundle.getBundle(String, Locale, ClassLoader)
.
There are some important differences. The first is that this
implementation only sets the first value found in a given
Properties
file, while ResourceBundle
will set the
last value found in a given Properties
file.
The second is that various keys are treated the same. '.'
are
replaced with '_'
in keys found. So assuming the given
.properties
file:
foo.bar = one foo_bar = two foo.bar = threeAnd the following class:
public class Example { public static String foo_bar; }Then the field
foo_bar
will be set to "one"
once this
method completes.
cls
- the class to set properties inresource
- the name of the resource, as a classname similar to what
ResourceBundle.getBundle(String)
takeslocale
- the locale to load fromencoding
- the encoding to use - if null
, defaults to ISO 8859-1
if null
public static String format(String format, Object... arguments)
MessageFormat
does, minus supporting any other formats.
Essentially this is a simple substitution. Each instance of
"{n}"
, where n is any number greater
than or equal to 0, is replaced with the Object.toString()
result
of the given argument. If there is no corresponding argument, an empty
string is substituted.
format
- the string containing the formatarguments
- the objects that specify the format
public static HashMap<String,String> loadLocaleProperties(URL base) throws IOException
PropertiesReader
,
handling overrides in the same manner as initialize(Class, String, Locale)
.
Unlike initialize(Class, String, Locale)
, this method does not
"munge" keys (foo_bar
and foo.bar
are unique), but it
does follow the same "first value found sets the property."
Sometimes it turns out to be easier to just deal with properties than fields... but the cost is that you have to deal with exceptions on your own.
base
- the base URL to the given resource
IOException
- if an I/O error occurspublic static HashMap<String,String> loadLocaleProperties(URL base, String encoding) throws IOException
PropertiesReader
,
handling overrides in the same manner as initialize(Class, String, Locale)
.
Unlike initialize(Class, String, Locale)
, this method does not
"munge" keys (foo_bar
and foo.bar
are unique), but it
does follow the same "first value found sets the property."
Sometimes it turns out to be easier to just deal with properties than fields... but the cost is that you have to deal with exceptions on your own.
base
- the base URL to the given resourceencoding
- the character encoding to use, or null
to use
whatever the resource says to use or ISO 8859-1 if the resource doesn't
specify
IOException
- if an I/O error occurspublic static HashMap<String,String> loadLocaleProperties(URL base, Locale locale, String encoding) throws IOException
PropertiesReader
,
handling overrides in the same manner as initialize(Class, String, Locale)
.
Unlike initialize(Class, String, Locale)
, this method does not
"munge" keys (foo_bar
and foo.bar
are unique), but it
does follow the same "first value found sets the property."
Sometimes it turns out to be easier to just deal with properties than fields... but the cost is that you have to deal with exceptions on your own.
base
- the base URL to the given resourcelocale
- encoding
- the character encoding to use, or null
to use
whatever the resource says to use or ISO 8859-1 if the resource doesn't
specify
IOException
- if an I/O error occurs
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |