Book Image

Tapestry 5: Building Web Applications

Book Image

Tapestry 5: Building Web Applications

Overview of this book

Table of Contents (17 chapters)
Tapestry 5
Credits
About the Author
About the Reviewers
Preface
Foreword
Where to Go Next

Switching Locale


Basically, to switch the locale of our web application, all we need to do is to tell Tapestry which locale we want to see as current. Tapestry has a special service named PersistentLocale. To get access to this service we simply inject it into the page. This is how it looks:

@Inject
private PersistentLocale persistentLocale;

And then we use the service's set method to set the new locale, like this:

persistentLocale.set(Locale.GERMAN);

It's that simple! However, to make this functionality work, we need to run the above line of code in some event handler. For simplicity, let's use the ActionLink component at this stage.

We could use two links, one of them saying Switch to German, another Switch to English, but, it will be slightly closer to a real life functionality if we use just one link, Switch Locale, and it will do the job whatever the current locale is. Here is how such a link could look in the page template:

<p align="right">
<t:actionlink t:id="switchlocale"...