The process for localizing an Echo2 application is the same as that for a standalone/desktop Java application. Localized strings are held in resource bundles that are created for specific target locales. Strings, instead of being hardcoded into the appliaction, are retrieved from these resource bundles based on unique identifiers.
For example, the following code configures buttons with hardcoded English text:
okButton.setText("Ok"); cancelButton.setText("Cancel");
A localized application would instead retrieve the appropriate text for the button from a ResourceBundle
:
ResourceBundle resourceBundle = ResourceBundle.getBundle("myapp.Messages", ApplicationInstance.getActive().getLocale()); okButton.setText(ResourceBundle.getString("Generic.Cancel")); cancelButton.setText(ResourceBundle.getString("Generic.Cancel"));
The practice of retrieving all displayed text from resource bundles is encouraged even if an application will not support more than one language. The centralization of all strings enables easy review of all text which can be displayed to the user.
An Echo2 ApplicationInstance
provides a Locale
property which descrcibe its current localization. This property is intended to be set by the application developer as required to the desired localization of the user.
Component Localization: Component
provides support for setting a Locale
property to describe its localization state and the default localization state for Component
s beneath it in the hierarchy.
Determining Locale: To determine the localization of a given Component
, you may invoke the Component.getRenderLocale()
method. This method will query the component, it's ancestors, and finally the ApplicationInstance
it is registered to in order to determine the Locale
with which the Component
should be rendered.
An Echo2 appliaction can be configured to detect the locale of the client on startup and configure its Locale
appropriately. This is accomplished using the ClientProperties
object which may be obtained from the ServerContext
. The following example code demonstrates how the locale can be set based on the client browser:
: : import nextapp.echo2.webcontainer.ContainerContext; import nextapp.echo2.webrender.ClientProperties; : : public class MyApp extends ApplicationInstance { : : public void init() { : : ContainerContext context = (ContainerContext) getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME); ClientProperties clientProperties = context.getClientProperties(); if (clientProperties.getProperty(ClientProperties.LOCALE) != null) { setLocale((Locale) clientProperties.getProperty(ClientProperties.LOCALE)); } : : } : : }
Echo2 provides support for languages which are written from left-to-right ("LTR") as well as those which are written right-to-left ("RTL"). Examples of RTL languages include Arabic and Hebrew.
Leading and Trailing Alignment: When developing an application which may be used with RTL languages, the design may call for certain user interface features to be "flipped" to the other side of the screen based on whether an LTR or RTL language is in use. Echo2's Alignment
property object provides this capability automatically when the Alignment.LEADING
or Alignment.TRAILING
values are specified for the horizontal property. The Alignment.LEADING
value indicates that an item should be positioned or aligned to the left when an LTR language is in use, and to the right when an RTL language. Similarly, the Alignment.TRAILING
property indicates that items should be positioned to the right for LTR languages and to the left for RTL languages.