Abstract base class for all "resource-bundles" using this native language support (NLS) module. Instead of a
ResourceBundle
you create a custom class extending this one in order to define the
messages
. This is done by defining methods with
NlsMessage
as return type and the
dynamic argument values
as parameters. We recommend to
follow these conventions:
- For your bundle you define a
Package
and a base name (e.g. my.package.NlsBundleExample
). This
namespace must not be occupied by an existing type.
- You create a non-abstract final class for that namespace (e.g.
my.package.NlsBundleExample
) extending
NlsBundle
.
- This class defines a public method for each provided
NlsMessage
using the
internationalized message
for the root
locale
.
- By default the
message key
used in the
method implementation (see example below) shall be the method name
.
- You have to ensure that the
message
key
s are unique within an NlsBundle
class as otherwise the localization is NOT possible.
- Your localized
*.properties
files have to be named using the base name followed by the according locale
suffix as for a regular ResourceBundle
(e.g. my/package/NlsBundleExample_de.properties
or
my/package/NlsBundleExample_zh_HK.properties
). For the root locale you ommit that locale suffix (e.g.
my/package/NlsBundleExample.properties
).
- For creating and maintaining these localized
*.properties
files, we provide the CLI tool
NlsBundleSynchronizer
.
Here is an example:
package com.example;
public final class NlsBundleMynameRoot extends NlsBundle
{
private static NlsBundleMynameRoot INSTANCE = new NlsBundleMynameRoot();
public NlsMessage
errorValueOutOfRange({int value, int min, int max) {
return create("errorValueOutOfRange", "The value {value} has to be in the range from {min} to {max}!", NlsArguments.of(KEY_VALUE, value, KEY_MIN, min, KEY_MAX, max));
}
public static NlsBundleMynameRoot get() {
return INSTANCE;
}
}
For localization you create or generate
*.properties
files in the same package for each supported
Locale
. In the example above e.g.
com/example/NlsBundleMyname_de.properties
with this
content:
errorValueOutOfRange = Der Wert {value} muss innerhalb des Wertebereichs von {min} bis {max} liegen!
Now you can use this as following:
NlsMessage
message = NlsBundleMynameRoot.get().errorValueOutOfRange(5, 0, 4);
System.out.println(message.getMessage
();
System.out.println(message.getLocalizedMessage
(Locale.GERMAN
);
This will print the following result:
The value 5 has to be in the range from 0 to 4!
Der Wert 5 muss innerhalb des Wertebereichs von 0 bis 4 liegen!
See
NlsVariable
for advanced features.