Differences between revisions 3 and 4
Revision 3 as of 2009-09-14 15:43:19
Size: 1573
Editor: MarvinMonroe
Comment:
Revision 4 as of 2009-09-20 23:20:03
Size: 1575
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
See also [http://tapestry.apache.org/tapestry5/guide/localization.html T5 Localization Guide] See also [[http://tapestry.apache.org/tapestry5/guide/localization.html|T5 Localization Guide]]

Tapestry stores the locale of the current user in the ThreadLocale service. This locale is either determined by the HTTP header "Accept-Language" or the cookie "org.apache.tapestry.locale" (both sent by the browser). The cookie has precedence.

For changing the locale you have to use the PersistentLocale service and (re-)set the locale.

Here is an example with an ActionLink with the id "changeLocale" (the language comes as the context):

@Inject
private PersistentLocale persistentLocale;

public void onActionFromChangeLocale(String language)
{
        persistentLocale.set(new Locale(language));
}

See also T5 Localization Guide

Nick Westgate also contributed the following snippet to the mailing list:

Create two property files:

app_ja.properties
app_en.properties

AppModule.java:

    configuration.add("tapestry.supported-locales", "en,ja");

Page.html with two action links to select your locale (Japanese or English):

    <a t:type="ActionLink" t:id="jaLocaleLink" href="#">Japanese</a><br/>
    <a t:type="ActionLink" t:id="enLocaleLink" href="#">English</a><br/>

Page.java contains the event handlers to switch the locale:

    @Inject
    private PersistentLocale persistentLocaleService;

...

    void onActionFromJaLocaleLink()
    {
        persistentLocaleService.set(Locale.JAPANESE);
    }

    void onActionFromEnLocaleLink()
    {
        persistentLocaleService.set(Locale.ENGLISH);
    }

Tapestry5HowToChangeLocale (last edited 2009-09-20 23:20:03 by localhost)