

A component can get the current locale in use calling its method getLocale(). By default this method will be recursively called on component's parent containers until one of them returns a valid locale. If no one of them returns a locale, this method will get the one associated with the current user session. This locale is automatically generated by Wicket in accordance with the language settings of the browser.

Developers can change the locale of the current session with Session's method setLocale (Locale locale): 

{code}
Session.get().setLocale(locale)
{code}

h3. Style and variation parameters for bundles

In addition to locale's informations, Wicket supports two further parameters to identify a resource bundle: style and variation. Parameter style is a string value and is defined at session-level. To set/get the style for the current session we can use the corresponding setter and getter of class Session:

{code}
Session.get().setStyle("myStyle");
Session.get().getStyle();
{code}

If set, style's value contributes to the final full name of the bundle and it is placed between the base name and the locale's informations:

{code}
<base name>[_style][_<language code>[_<COUNTRY_CODE>[_<variant code>]]]
{code}

Wicket gives the priority to candidate names containing the style information (if available). The other parameter we can use for localization is variation. Just like style also variation is a string value, but it is defined at component-level. The value of variation is returned by Component's method getVariation(). By default this method returns the variation of the parent component or a null value if a component hasn't a parent (i.e. it's a page). If we want to customize this parameter we must overwrite method  getVariation and make it return the desired value.

Variation's value contributes to the final full name of the bundle and is placed before style parameter: 

{code}
<base name>[_variation][_style][_<language code>[_<COUNTRY_CODE>[_<variant code>]]]
{code}


h3. Using UTF-8 for resource bundles

Java uses the standard character set "ISO 8859-11":http://en.wikipedia.org/wiki/ISO/IEC_8859-1 to encode text files like properties files. Unfortunately ISO 8859-1 does not support most of the extra-European languages like Chinese or Japanese. The only way to use properties files with such languages is to use escaped "Unicode":http://en.wikipedia.org/wiki/List_of_Unicode_characters characters, but this leads to not human-readable files. For example if we wanted to write the word 'website' in simplified Chinese (the ideograms are 网站) we should write the Unicode characters @\u7F51\u7AD9@.
For this reason ISO 8859-11 is being replaced with another Unicode-compliant character encoding called UTF-8. Text files created with this encoding can contain Unicode symbols in plain format.
Wicket provides a useful convention to use properties file encoded with UTF-8. We just have to add prefix @.utf8.@ to file extension (i.e. @.utf8.properties@).

{note}
If you want to use UTF-8 with your text files, make sure that your editor/IDE is actually using this character encoding. Some OS like Windows use a different encoding by default.
{note}

h3. Using XML files as resource bundles

Starting from version 1.5, Java introduced the support for XML files as resource bundles. XML files are generally encoded with character sets UTF-8 or UTF-16 which support every symbol of the Unicode standard. In order to be a valid resource bundle the XML file must conform to the DTD available at "http://java.sun.com/dtd/properties.dtd":http://java.sun.com/dtd/properties.dtd .

Here is an example of XML resource bundle taken from project LocalizedGreetings (file WicketApplication_zh.properties.xml) containing the translation in simplified Chinese of the greeting message “Welcome to the website!”:

{code:xml}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<entry key="greetingMessage">欢迎光临本网站！</entry>
</properties>
{code}

To use XML bundles in Wicket we don't need to put in place any additional configuration. The only rule we have to respect with these files is to use properties.xml as extension while their base name follows the same rules seen so far for bundle names.

h3. Reading bundles from code

Class Component makes reading bundles very easy with method getString(String key). This method searches for a resource with the given key looking into the resource bundles visited by the lookup algorithm illustrated in [paragraph 14.3|guide:i18n_3]. For example if we have a greeting message with key greetingMessage in our application's resource  bundle, we can read it from our component code with this instruction:

{code}
getString("greetingMessage");
{code}

h3. Localization of bundles in Wicket

In [paragraph 12.2|guide:forms2_2] we have used as resource bundle the properties file placed next to our application class. This file is the default resource bundle for the entire application and it is used by the lookup algorithm if it doesn't find any better match for a given component and locale. If we want to provide localized versions of this file we must simply follow the rules of Java i18n and put our translated resources into another properties file with a name corresponding to the desired locale. For example project LocalizedGreetings comes with the default application's properties file ( WicketApplication.properties) containing a greeting message:

{code}
greetingMessage=Welcome to the site!
{code}

Along with this file we can also find a bundle for German (WicketApplication_de.properties) and another one in XML format for simplified Chinese (WicketApplication_zh.properties.xml). The example project consists of a single page (HomePage.java) displaying the greeting message. The current locale can be changed with a drop-down list and the possible options are English (the default one), German and simplified Chinese:

!locale-german.png!

The label displaying the greeting message has a custom read-only model which returns the message with method getString. The initialization code for this label is this:

{code}
AbstractReadOnlyModel<String> model = new AbstractReadOnlyModel<String>() {			
			@Override
			public String getObject() {
				return getString("greetingMessage");			
			}
};

add(new Label("greetingMessage", model));
{code}

Class @org.apache.wicket.model.AbstractReadOnlyModel@ is a convenience class for implementing read-only models. In this project we have implemented a custom read-only model for illustrative purposes only because Wicket already provides built-in models for the same task. We will see them in paragraph [paragraph 14.5|guide:i18n_5].

The rest of the code of the home page builds the stateless form and the drop-down menu used to change the locale.

{code}
List<Locale> locales = Arrays.asList(Locale.ENGLISH, Locale.CHINESE, Locale.GERMAN);
final DropDownChoice<Locale> changeLocale = 
             new DropDownChoice<Locale>("changeLocale", new Model<Locale>(), locales);
		
StatelessForm form = new StatelessForm("form"){
	@Override
	protected void onSubmit() {
		Session.get().setLocale(changeLocale.getModelObject());
	}
};		
		
setStatelessHint(true);
add(form.add(changeLocale))
{code}


h3. Localization of markup files

Although resource bundles exist to extract local-dependent elements from our code and from UI components, in Wicket we can decide to provide different markup files for different locale settings. Just like standard markup files, by default localized markup files must be placed next to component's class and their file name must contain the locale's informations. In the following picture, CustomPanel comes with a standard (or default) markup file and with another one localized for German:

!comp-with-markup-german.png!

When the current locale corresponds to German country (language code de), markup file CustomPanel_de.html will be used in place of the default one.

h3. Reading bundles with tag <wicket:message>

String resources can be also retrieved directly from markup code using tag <wicket:message>. The key of the desired resource is specified with attribute key:

{code:xml}
<wicket:message key="greetingMessage">message goes here</wicket:message>
{code}

By default the resource value is not escaped for HTML entities. To do that use the @escape@ attribute:

{code:xml}
<wicket:message key="greetingMessage" escape="true">message goes here</wicket:message>
{code}


@wicket:message@ can be adopted also to localize the attributes of a tag. The name of the attribute and the resource key are expressed as a colon-separated value. In the following markup the content of attribute @value@ will be replaced with the localized resource having 'key4value' as key:

{code:html}
<input type="submit" value="Preview value" wicket:message="value:key4value"/>
{code}

If we want to specify multiple attributes at once, we can separate them with a comma:

{code:html}
<input type="submit" value="Preview value" wicket:message="value:key4value, title:key4title"/>
{code}
