0

Having the locale take effect on the same desktop

asked 2012-08-29 03:51:34 +0800

RichardL gravatar image RichardL
768 4

Hi all,

The locale of my application might be different from page to page. So I have a parameter in the URL which specifies what the locale should be and set the locale based on that parameter, i.e. http://www.mysite.com?loc=en. However, it seems the locale can't take effect on that desktop (browser page) and there needs to be a page refresh before the locale can take effect. I have tried setting the locale at DesktopInit, but it is the same - it still needs to be refreshed.

This is quite central to the functioning of my application, so I'd really like to find a solution. Does anyone have any ideas?

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2012-08-29 08:56:39 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Hi Richard,

here's a little bit of code that you can modify to your needs. Hope it can helps you.

	@Override
	public void doBeforeComposeChildren(Component comp) throws Exception {
		Components.wireController(comp, this);

		/**
		 * SET THE USERS LOCALE IN THE USERS SESSION. Locale info are read from
		 * users data. We must load the users data for his locale new if the
		 * locale has changed.
		 */
		// get the user
		User aUser = ((UserImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();

		// get the users 'locale' data like 'de_DE'; 'en_US'
		String userLocale = getSecurityService().getUserByID(aUser.getId(), aUser.getTenant().getId()).getLocale();

		if (StringUtils.isNotEmpty(userLocale)) {

			FHSessionUtil.setUserLocale(userLocale);

			Session session = Sessions.getCurrent();

			Locale preferredLocale = userLocale.length() > 2 ? new Locale(userLocale.substring(0, 2), userLocale.substring(3)) : new Locale(userLocale);
			session.setAttribute(org.zkoss.web.Attributes.PREFERRED_LOCALE, preferredLocale);

			Locale old = Locales.setThreadLocal(preferredLocale);

			// System.out.println("Locale getCurrent :" +
			// Locales.getCurrent().toString());
			// System.out.println("Locale getLocalThread :" +
			// Locales.getThreadLocal().toString());
		}

best
Stephan

link publish delete flag offensive edit

answered 2012-08-29 12:28:02 +0800

RichardL gravatar image RichardL
768 4

Thanks, Stephan, I'm already using code similar to yours. My issue regards the locale taking effect on the same desktop (instead of a page refresh). Preferably, I'd like to set the locale to session at DesktopInit if it differs from the current locale. As mentioned, the locale doesn't take effect, so I tried implementing a page refresh using: Executions.sendRedirect(null). However, at DesktopInit using null as the argument doesn't seem to refresh the full url.

So, now I am checking the locale in my composers instead of at DesktopInit and using Executions.sendRedirect(null), which is not ideal, but it works. My situation is probably not common, as apps don't tend to change locale from page to page, but if anyone has experience or ideas about this I'd appreciate hearing their thoughts.

Thanks,
Richard

link publish delete flag offensive edit

answered 2012-08-29 19:10:46 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2012-08-29 19:11:19 +0800

Hmmmm, i have look in the second code depending of 'locale' where i change the locale. It does only take effect after a new refresh of the page.
So i think you must change the locale in an early stage of the (every) page creation?

	/**
	 * SET THE USERS LOCALE IN THE USERS SESSION. Locale info are read from
	 * users data.
	 */
	private void doSetUserLocale() {

		// get the logged in user
		User aUser = ((UserImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();

		// get the users 'locale' data like 'de_DE'; 'en_US'
		String userLocale = getSecurityService().getUserByID(aUser.getId(), aUser.getTenant().getId()).getLocale();

		if (StringUtils.isNotEmpty(userLocale)) {

			FHSessionUtil.setUserLocale(userLocale);

			Locale preferredLocale = userLocale.length() > 2 ? new Locale(userLocale.substring(0, 2), userLocale.substring(3)) : new Locale(userLocale);
			session.setAttribute(org.zkoss.web.Attributes.PREFERRED_LOCALE, preferredLocale);

			Locales.setThreadLocal(preferredLocale);

			// reload the main page
			execution.sendRedirect("/pages/index.zul");
		}
	}

In my case i'm working with a ONE page (borderlayout; only center have changed content) solution, so i can reload the main page.

best
Stephan

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-08-29 03:51:34 +0800

Seen: 117 times

Last updated: Aug 29 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More