Home of: [Atelier "FUJIGURUMA"] >> [PageMixer hosted by SourceForge.net]

SEE "For Readers of English Version",
or Japanese version of this page

Determine 'Locale'

This section explains how to customize determining Locale with PageServlet.

You can skip this section if you have no interest in locale sensitive response renderring.

Overview

Class diagram

Class diagram in this section is shown below:

Class diagram
Class diagram (click for large figure)

Classes which you must define are colored, and other are already defined.

Class names

In this tutorial, abbreviated class names are used. Complete names are shown below.

Classes of PageMixer framework

NotationFull name
ConsumerContext jp.ne.dti.lares.foozy.pagemixer.mixer.ConsumerContext
LocaleFactory jp.ne.dti.lares.foozy.pagemixer.servlet.LocaleFactory
PageServlet jp.ne.dti.lares.foozy.pagemixer.servlet.PageServlet
ParameterKey jp.ne.dti.lares.foozy.pagemixer.servlet.ParameterKey
SessionKey jp.ne.dti.lares.foozy.pagemixer.servlet.SessionKey

Tutorial specific classes

NotationFull name
CustomLocaleFactory pagemixer.servlet.CustomLocaleFactory

Purpose of customization

Servlet API provides HttpServletRequest#getLocale() to get Locale of current request.

In addition to it, by customization of determining Locale, PageMixer allows you to determine Locale per request in the other ways. For example:

Override LocaleFactory

PageMixer provides "LocaleFactory" for customization of determining Locale.

In almost all cases, you can satisfy your needs by overriding create method of it, event though it has more overridable methods for complex purpose. Please see API document for detail about them.


public Locale create(ConsumerContext context)

Method signature of create()

Sample overriding is shown below.


String value =
(String)(context.getValue(KEY_PARAM_LOCALE));

Locale locale =
(Locale)(context.getValue(KEY_SESSION_LOCALE));

if(null != value){
    locale = new Locale(value, "", "");
    context.setValue(KEY_SESSION_LOCALE, locale);
}
else if(null == locale){
    locale = Locale.getDefault();
}

return locale;

Customization of locale determination

In this example, KEY_PARAM_LOCALE and KEY_SESSION_LOCALE are instances of ParameterKey and SessionKey. So, above sample determines Locale in steps shown below.

  1. create new Locale if request parameter is specified,
  2. use Locale in session if it is already stored in, or
  3. user default Locale of runtime environment otherwise

Configuration

Parameters for base class

LocaleFactory does not requires any initialization parameters.

Parameter to use custom class

To use your custom LocaleFactory, you should specify "servlet.localeFactory" Servlet init-param in deployment descriptor as shown below.


<init-param>
  <param-name>
    servlet.localeFactory
  </param-name>
  <param-value>
    pagemixer.servlet.CustomLocaleFactory
  </param-value>
</init-param>

Specify custom LocaleFactory

To next section "Determine page information"