To manually switch the locale:

public final class MyPage extends WebPage {

	...

	add(new Link("languageSwitch") {

		@Override
		public final void onClick() {
			Locale locale = new Locale(MyPage.this.getString("header.versionLanguage"));
			this.getSession().setLocale(locale);
		}

	});

	...

}
  • No labels

1 Comment

  1. I've created a LocaleLink which you can add to your page. It will not show if it is the current Locale

    import java.util.Locale;
    
    import org.apache.wicket.markup.html.link.Link;
    
    /**
     * @author Herman Suijs
     */
    public final class LocaleLink extends Link
    {
        /**
         * Dummy serial version uid.
         */
        private static final long serialVersionUID = 1L;
    
        /**
         * Locale for this link.
         */
        private final Locale      locale;
    
        /**
         * Constructor for a new LocaleLink.
         * 
         * @param wicketId Id of wicket component
         * @param localeString Locale to switch to
         */
        public LocaleLink(final String wicketId, final String localeString)
        {
            super(wicketId);
            this.locale = new Locale(localeString);
        }
    
        /**
         * Constructor with locale.
         * 
         * @param wicketId Id of wicket component
         * @param locale Locale to switch to
         */
        public LocaleLink(final String wicketId, final Locale locale)
        {
            super(wicketId);
            this.locale = locale;
        }
    
        /**
         * On click of a user.
         */
        @Override
        public void onClick()
        {
            this.getSession().setLocale(this.locale);
        }
    
        /**
         * Determine if visible.
         * 
         * @return True if locale for component is different from locale in session.
         */
        @Override
        public boolean isVisible()
        {
            return !this.getSession().getLocale().equals(this.locale);
        }
    }