[Enterprise Extensions only]

Accessing caller locales and time zone

Every invocation of an application component has an associated caller Internationalization context associated with the thread running that invocation. Caller context is propagated using the Internationalization Service and middleware to the target of a request, such as remote Enterprise JavaBean or servlet service methods.

To obtain caller locales and time zone, an application component first resolves a reference to the Internationalization interface, and then calls the appropriate accessor method. Details for obtaining this reference can be found in Gaining access to the Internationalization context API.

The Internationalization interface contains the following methods to get caller Internationalization context elements:

Locale [] getLocales()
Returns the list of caller locales associated with the current thread.
Locale getLocale()
Returns the first in the list of caller locales associated with the current thread.
TimeZone getTimeZone()
Returns the caller SimpleTimeZone associated with the current thread.

For complete information about Internationalization interface methods, see sections:

The following code snippet illustrates the basic usage of the Internationalization interface:

//------------------------------------------------------------
// Internationalization context imports.
//------------------------------------------------------------
import com.ibm.websphere.i18n.context.*;
...

public class MyApplication {
...

//------------------------------------------------------------
// Resolve the Internationalization context API here. See the
// Gaining access to the Internationalization Context API
// topic for complete details.
//------------------------------------------------------------
UserInternationalization userI18n   = null;
Internationalization     callerI18n = null;
...

//------------------------------------------------------------
// Obtain the desired Internationalization context element.
//------------------------------------------------------------
java.util.Locale [] myLocales       = callerI18n.getLocales();
java.util.Locale myLocale           = callerI18n.getLocale();
java.util.SimpleTimeZone myTimeZone = callerI18n.getTimezone();
...

//------------------------------------------------------------
// Utilize the caller context element to perform a locale or
// time zone sensitive computation, for example:
//------------------------------------------------------------
DateFormat df = DateFormat.getDateInstance(myLocale);
String localizedDate = df.getDateInstance().format(aDateInstance);
...

Internationalization interface methods are utilized in the same manner and are available within all Enterprise JavaBean application components; however their semantics vary slightly depending upon a component's type. For instance, when obtaining a caller Internationalization context element within a Java client application the service returns the corresponding default or process-based element; in contrast, when obtaining caller locales within a servlet service method (for example, doPost() or doGet()), the service returns the locales propagated within the corresponding HTML request. See Internationalization context management for a discussion of how the service propagates Internationalization context throughout an application.