0

Switching locale does not work (ENG/RUS)

asked 2008-12-05 04:24:24 +0800

Stas283 gravatar image Stas283
93 2
www.trade4stas.com

- I have followed the instruction (Chapter 14 - http://www.zkoss.org/doc/devguide/ch14.html) regarding how to switch user language.

- I have made all the steps required.

- There are 2 properties files with messages in russian and english.

i3-label.properties
locale=Locale

i3-label_ru_RU.properties
locale=Языковой код

- I am setting a user preferred language in the struts action like it described in manual

Locale preferredLocale = org.zkoss.util.Locales.getLocale("ru_RU");
session.setAttribute(org.zkoss.web.Attributes.PREFERRED_LOCALE, preferredLocale);

- But It does not make any impact on output

<%@ taglib uri="http://www.zkoss.org/dsp/web/core" prefix = "zc"%>
...
<z:label id="localeLabel" value="${zc:l('locale')}"/>
...

- Value of label localeLabel is desplayed in English no matter what.

- But if I put some code in zkscript that I can manually change its value

<zkscript>
void changeLocale()
{
preferredLocale = org.zkoss.util.Locales.getLocale(localeName);
session.setAttribute(org.zkoss.web.Attributes.PREFERRED_LOCALE, preferredLocale);
String x=org.zkoss.util.resource.Labels.getLabel("locale");
localeLabel.setValue(x+" "+org.zkoss.util.Locales.getCurrent()+" ");
}
</zkscript>

delete flag offensive retag edit

5 Replies

Sort by » oldest newest

answered 2008-12-08 12:23:51 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

Hi, Do you use JSP Tag? the document and example are for ZUL,
Currently, there is no ZK handler to intercept JSP to set the Locale.
Maybe the JSP Tag-page should handle this, but for now, you could use Locales.setThreadLocal to work around.
following example works for me.

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.zkoss.org/dsp/web/core" prefix="zc"%>
<%@ taglib uri="http://www.zkoss.org/jsp/zul" prefix="zk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Locale</title>
</head>
<body>
<zk:page>
	<zk:zscript>
org.zkoss.util.Locales.setThreadLocal(org.zkoss.util.Locales.getLocale("ru_RU"));
	</zk:zscript>
	<zk:label id="localeLabel1">
		<zk:attribute name="value">ru_RU: ${zc:l("locale")}</zk:attribute>
	</zk:label>
	<br/>
	<zk:zscript>
org.zkoss.util.Locales.setThreadLocal(org.zkoss.util.Locales.getLocale("en_US"));
	</zk:zscript>
	<zk:label id="localeLabel2">
		<zk:attribute name="value">en_US: ${zc:l("locale")}</zk:attribute>
	</zk:label>

</zk:page>

</body>
</html>

/Dennis

link publish delete flag offensive edit

answered 2008-12-08 12:29:02 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

another solution is better. Writing a listener for .jsp to do the thing the ZK does (for work around)
1.get Locale attribute form seesion.
2.set locale by Locales.setThreadLocal.

and also post a feature to JSP tags to make ZK JSP Tag better.

link publish delete flag offensive edit

answered 2008-12-24 03:33:29 +0800

Stas283 gravatar image Stas283
93 2
www.trade4stas.com

Thank you, Dennis, You really helped me

It seems the first solution worked.

In the long run, You are also right about solution 2. It is better to have some filter intercepting all requests and setting a proper locale before letting response come back to client. Filter will check some parameter in session and set locale as a tread local variable.

This is my portal: www.trade4stas.com

On the top right corner there are two flags. Each of them is to select a language. Once language is selected it should be effective during the life of the session unless user changes it.

I wish ZK supported a feature to read locale setting directly from the session like it was documented.

session.setAttribute(org.zkoss.web.Attributes.PREFERRED_LOCALE, preferredLocale);

One more time, thank you for the good advice.

link publish delete flag offensive edit

answered 2011-03-14 06:53:05 +0800

yaryan997 gravatar image yaryan997
210 2

@dennis.

Hello dennis i need your help to setCurrent locale to English or to Italy using the label click event in zk. Actually i am working with a application in which i had to work on two main languages English and Italy.

Can you help me to. that how to specify the locale attribute or get locale when user select the language as a label available.

Best Regards
Yogendra

link publish delete flag offensive edit

answered 2011-03-18 06:04:19 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

Please refer to this demo.

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: 2008-12-05 04:24:24 +0800

Seen: 1,156 times

Last updated: Mar 18 '11

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