Constants abstract class
A tag interface that facilitates locale-sensitive, compile-time binding of constant values supplied from properties files. Using <code>GWT.create(<i>class</i>)</code> to "instantiate" an interface that extends <code>Constants</code> returns an instance of an automatically generated subclass that is implemented using values from a property file selected based on locale.
Locale is specified at run time using a meta tag or query string as described for {@link com.google.gwt.i18n.client.Localizable}.
Extending Constants
To use Constants
, begin by defining an interface that extends
it. Each interface method is referred to as a constant accessor, and
its corresponding localized value is loaded based on the key for that method.
The default key is simply the unqualified name of the method, but can be specified
directly with an {@code @Key} annotation or a different generation method using
{@code @GenerateKeys}. Also, the default value can be specified in an annotation
rather than a default properties file (and some key generators may require the value
to be given in the source file via annotations). For example,
{@example com.google.gwt.examples.i18n.NumberFormatConstants}
expects to find properties named <code>decimalSeparator</code> and <code>thousandsSeparator</code> in an associated properties file. For example, the following properties would be used for a German locale:
{@gwt.include com/google/gwt/examples/i18n/NumberFormatConstantsdeDE.properties}
The following example demonstrates how to use constant accessors defined in the interface above:
{@example com.google.gwt.examples.i18n.NumberFormatConstantsExample#useNumberFormatConstants()} </p>
Here is the same example using annotations to store the default values:
{@example com.google.gwt.examples.i18n.NumberFormatConstantsAnnot} </p>
It is also possible to change the property name bound to a constant accessor using the {@code @Key} annotation. For example, {@example com.google.gwt.examples.i18n.NumberFormatConstantsWithAltKey}
would match the names of the following properties:
{@gwt.include com/google/gwt/examples/i18n/NumberFormatConstantsWithAltKey_en.properties} </p>
Defining Constant Accessors
Constant accessors must be of the formT methodName()
where <code>T</code> is one of the return types in the following table:
The property value is interpreted as... | Annotation to use for default value | |
---|---|---|
String |
A plain string value | {@code @DefaultStringValue} |
String[] |
A comma-separated array of strings; use '\\, ' to escape
commas |
{@code @DefaultStringArrayValue} |
int |
An int value, checked during compilation |
{@code @DefaultIntValue} |
float |
A float value, checked during compilation |
{@code @DefaultFloatValue} |
double |
A double value, checked during compilation |
{@code @DefaultDoubleValue} |
boolean |
A boolean value ("true" or "false"), checked during
compilation |
{@code @DefaultBooleanValue} |
Map |
A comma-separated list of property names, each of which is a key into a generated map; the value mapped to given key is the value of the property having named by that key | {@code @DefaultStringMapValue} |
locale is... |
The properties file that binds to
org.example.foo.Intf is... |
---|---|
unspecified | org/example/foo/Intf.properties |
x |
org/example/foo/Intf_x.properties if it exists and
defines the property being sought, otherwise treated as if
locale were unspecified |
x_Y |
org/example/foo/Intf_x_Y.properties if it exists and
defines the property being sought, otherwise treated as if
locale were x |