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"...