0

Internationalization

asked 2008-07-23 10:59:12 +0800

Emanuele gravatar image Emanuele
117 1

Hi to everyone!

I'm trying to insert dynamic label for internationalization in my test application with ZK. I found the instructions on "The user guide" where the following instructions are contained:

Locale preferredLocale = ...; //decide the locale (from, say, database)
session.setAttribute("px_preferred_locale", preferredLocale);

My application contains something like the following after the user login (not in zscript, but in a pure Java class):

Sessions.getCurrent().setAttribute("px_preferred_locale",new Locale("en","EN"));

But it doesn't work because, while I'm expecting that now it reads the labels from the file i3-label_en.properties that I placed in the WEB-INF directory, it continues to read the label from the default i3-label.properties.

Any suggestions?

Thanks
Emanuele

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2008-07-23 14:27:18 +0800

sousa1981 gravatar image sousa1981
573 4

updated 2008-07-23 14:39:25 +0800

The following worked for me:

1. In web.xml I defined a listener

<listener>
	<listener-class>
		mypackage.MyListener
	</listener-class>
</listener>

2. Then in MyListener

public class BciOeListener implements HttpSessionListener {

	public void sessionCreated(HttpSessionEvent e) {
		HttpSession session = e.getSession();		
		// Forcing locale to portuguese
		session.setAttribute("px_preferred_locale", new Locale("pt", "PT"));
	}

	public void sessionDestroyed(HttpSessionEvent e) {
		// TODO: 
	}
}

3. And I have in WEB-INF file "i3-label_pt_PT.properties"

Hope it help

link publish delete flag offensive edit

answered 2008-07-23 15:56:38 +0800

Emanuele gravatar image Emanuele
117 1

Thank you sousa.

I think this solution may work if I want to set the language statically. But what happens if I want to set the language in a second moment (this is what I want to do), when the session has been already created?

link publish delete flag offensive edit

answered 2008-07-23 16:29:17 +0800

Emanuele gravatar image Emanuele
117 1

I found this solution for my question...

I defined the following class:

*************************************************************

import java.io.File;
import java.net.URL;
import java.util.Locale;

import org.zkoss.mesg.Messages;
import org.zkoss.util.resource.LabelLocator;

/**
* Class which implements a locator for general labels.<br/>
* It must be registered for Labels with
* <i>Labels.register(org.zkoss.util.resource.LabelLocator)</i>
*/
public class GeneralLabelLocator implements LabelLocator {

private static final String MENU_FILE_NAME="i3-label";
private static final String MENU_FILE_SUFFIX=".properties";
private String context;

/**
* Constructor
*
* @param context wam context
*/
public GeneralLabelLocator(String context)
{
this.context = context;
}


/* (non-Javadoc)
* @see org.zkoss.util.resource.LabelLocator#locate(java.util.Locale)
*/
public URL locate(Locale locale) throws Exception
{
String menu_res_filename =(locale.getLanguage().equals(Locale.ITALIAN.getLanguage()))?MENU_FILE_NAME+MENU_FILE_SUFFIX:MENU_FILE_NAME+"_"+locale.getLanguage()+MENU_FILE_SUFFIX;

// real path
String menu_res_path = AppUtil.getRealPath("/WEB-INF/"+menu_res_filename);

// check if the file exists
File fmr = new File(menu_res_path);
if(!fmr.exists())
throw new Exception(...........);

// return url
return fmr.toURL();
}

}

*******************************************************************************

After that, I added something like the following line in the method invocated when the application is initialized:

Labels.register(new GeneralLabelLocator(context));



And it works!



Hope this will be useful for you!

link publish delete flag offensive edit

answered 2010-03-14 21:04:17 +0800

Stas283 gravatar image Stas283
93 2
www.trade4stas.com

updated 2010-03-14 22:36:47 +0800

Emanuele, It works good for me. But I added something that might be helpful for people who do not want to deal with files and real file-system location.

- Please note that all properties files must be encoded in UTF-8 to let ZK reads them properly


public class ZKLabelLocator implements org.zkoss.util.resource.LabelLocator
{
    private static final char FS='/';

    public URL locate(final Locale locale) throws Exception
    {
        final String country = locale.getLanguage();
        final StringBuffer path = new StringBuffer().append("com").append(FS).append("trade4stas").append(FS).append("config").append(FS); //Set a relative path as you would do it for a resource bundle

        if ("ru".equalsIgnoreCase(country)) //Choose a properties file. Do not forget about UTF-8 encoding
        {
            path.append("struts_messages_ru_RU.properties"); //Russian
        }
        else
        {
            path.append("struts_messages.properties"); //English
        }

        final URL finalURL=this.getClass().getClassLoader().getResource(path.toString());
        return finalURL; //Here we go !
    }
}


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-07-23 10:59:12 +0800

Seen: 639 times

Last updated: Mar 14 '10

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