public class IlvLocaleUtil extends Object
Locale.getDefault()
returns a locale that is a good
general default, the "current locale" provided by this class depends on the
type of GUI and on the request being executed (in a web server situation).
This class also defines a concept of "server locale" for use by those places in JViews where output is known to be presented to the administrator of a web server and definitely not to a user who sees the JViews components or an applet user.
Additionally, this class keeps track of an association between an ICU locale
and a java.util
locale. When a java.util
locale
that corresponds to an ICU locale is passed to JViews API, the corresponding
ICU locale will be used instead, together with the ICU variants of the
globalization API. This holds for specific portions of JViews only.
Modifier and Type | Field and Description |
---|---|
static String |
ULOCALE_PROPERTY
Property indicating the
ULocale of a JComponent . |
Modifier and Type | Method and Description |
---|---|
static com.ibm.icu.util.ULocale |
getComponentULocale(Component comp)
Returns the
ULocale that is in effect for a given AWT or Swing
component. |
static Locale |
getCurrentLocale()
Returns the current locale.
|
static com.ibm.icu.util.ULocale |
getCurrentULocale()
Returns the current locale as an ICU
ULocale . |
static Locale |
getServerLocale()
Returns the server locale.
|
static com.ibm.icu.util.ULocale |
getServerULocale()
Returns the server locale.
|
static Locale |
getThreadLocale()
Deprecated.
Since JViews 8.8, please use
getThreadULocale()
instead. |
static com.ibm.icu.util.ULocale |
getThreadULocale()
Returns the ICU
ULocale that is currently assigned to the
current thread, or null for none. |
static void |
initJComponentULocale(JComponent comp)
Initializes the
ULocale of a Swing component, in a way that is
consistent with the initialization of the Locale of the same
component during the JComponent constructor. |
static void |
setThreadLocale(Locale locale)
Deprecated.
Since JViews 8.8, please use
setThreadULocale(ULocale)
instead. |
static void |
setThreadULocale(com.ibm.icu.util.ULocale locale)
Assigns the given ICU
ULocale to the current thread. |
public static final String ULOCALE_PROPERTY
ULocale
of a JComponent
.
It is used in PropertyChangeEvent
s that are sent to
PropertyChangeListener
s.
To set an ULocale
on a JComponent
, while keeping
the Locale in sync with the ULocale, your implementation of
setULocale
to call
putClientProperty(ULOCALE_PROPERTY, locale); setLocale(locale != null ? locale.toLocale() : null);
public static Locale getCurrentLocale()
The current locale is determined as follows:
setThreadLocale(Locale)
or setThreadULocale(ULocale)
, it
is returned.
If, in a web application, you want to use a Locale
that is
different from the one computed from the Accept-Language
information sent by the browser — for example, a locale specified by
corporate policies, the user's explicit selection for the current session,
the user's selection from the previous session, or the user's registration
profile —, you need to specify it through
setThreadULocale(ULocale)
for the duration of the current request.
If the locale is changing within a session, you may also have to call the
setLocale
or setULocale
method on the relevant
JViews components.
Note: The result of this method depends on the current thread (in JSF context, in servlet context, and in applet context).
getCurrentULocale()
,
getThreadLocale()
public static com.ibm.icu.util.ULocale getCurrentULocale()
ULocale
. It can be used
as default for initializing the locale of ResourceBundles and Formats.
The current locale is determined as follows:
setThreadLocale(Locale)
or setThreadULocale(ULocale)
, it
is returned.user.language
, user.country
, and user.variant
. Here the following additional optional
system properties are taken into account:
user.script
user.calendar
user.collation
user.currency
user.numbers
If, in a web application, you want to use a ULocale
that is
different from the one computed from the Accept-Language
information sent by the browser — for example, a locale specified by
corporate policies, the user's explicit selection for the current session,
the user's selection from the previous session, or the user's registration
profile —, you need to specify it through
setThreadULocale(ULocale)
for the duration of the current request.
If the locale is changing within a session, you may also have to call the
setULocale
method on the relevant JViews components.
Note: The result of this method depends on the current thread (in JSF context, in servlet context, and in applet context).
getCurrentLocale()
,
getThreadULocale()
@Deprecated public static Locale getThreadLocale()
getThreadULocale()
instead.Locale
that is currently assigned to the current
thread, or null
for none.setThreadLocale(Locale)
public static com.ibm.icu.util.ULocale getThreadULocale()
ULocale
that is currently assigned to the
current thread, or null
for none.setThreadULocale(ULocale)
@Deprecated public static void setThreadLocale(Locale locale)
setThreadULocale(ULocale)
instead.Locale
to the current thread. When the
locale argument is null
, the association between the current
thread and a Locale
is removed.
This assigned locale acts as an override for the result obtained by
getCurrentLocale()
.
The intended use of this method is to temporarily override the notion of "current locale":
Locale locale = ...; IlvLocaleUtil.setThreadLocale(locale); try { ... } finally { IlvLocaleUtil.setThreadLocale(null); }
locale
- Locale to be assigned to the current thread.getThreadLocale()
public static void setThreadULocale(com.ibm.icu.util.ULocale locale)
ULocale
to the current thread. When the
locale argument is null
, the association between the current
thread and a ULocale
is removed.
This assigned locale acts as an override for the result obtained by
getCurrentULocale()
.
The intended use of this method is to temporarily override the notion of "current locale":
ULocale locale = ...; IlvLocaleUtil.setThreadULocale(locale); try { ... } finally { IlvLocaleUtil.setThreadULocale(null); }
locale
- Locale to be assigned to the current thread.getThreadULocale()
public static Locale getServerLocale()
The server locale is currently determined through
ULocale.getDefault()
and the following additional optional
system properties:
user.script
user.calendar
user.collation
user.currency
user.numbers
Locale.getDefault()
public static com.ibm.icu.util.ULocale getServerULocale()
The server locale is currently determined through
ULocale.getDefault()
and the following additional optional
system properties:
user.script
user.calendar
user.collation
user.currency
user.numbers
ULocale.getDefault()
public static com.ibm.icu.util.ULocale getComponentULocale(Component comp)
ULocale
that is in effect for a given AWT or Swing
component.
This method considers inheritance from the parent component, like
Component.getLocale()
does.
comp
- An AWT or Swing componentIllegalStateException
- if this component does not have a locale set (note that Swing
components all have a Locale
set during the
constructor) and none of the ancestors has a Locale
set eitherinitJComponentULocale(JComponent)
public static void initJComponentULocale(JComponent comp)
ULocale
of a Swing component, in a way that is
consistent with the initialization of the Locale
of the same
component during the JComponent
constructor.
This method should be invoked during the constructor of every Swing
component that shall be passed as argument to the
getComponentULocale(Component)
method and that does not have a
setULocale(ULocale)
method.
For components that have a setULocale
method, you should call
setULocale(IlvLocaleUtil.getCurrentULocale());
during the
constructor.
comp
- A Swing component.© Copyright 2024 Rogue Wave Software, Inc., a Perforce company.. All Rights Reserved.