ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

Internationalization

Emanuele
23 Jul 2008 10:59:12 GMT
23 Jul 2008 10:59:12 GMT

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

sousa1981
23 Jul 2008 14:27:18 GMT
23 Jul 2008 14:27:18 GMT

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

Emanuele
23 Jul 2008 15:56:38 GMT
23 Jul 2008 15:56:38 GMT

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?

Emanuele
23 Jul 2008 16:29:17 GMT
23 Jul 2008 16:29:17 GMT

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!

Stas283
14 Mar 2010 21:04:17 GMT
14 Mar 2010 21:04:17 GMT

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 !
    }
}