package org.apache.jetspeed.modules.actions.portlets;

import java.util.Locale;

import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

/**
 * @author Administrator
 *
 * 
 */
public class CustomizeLocale extends VelocityPortletAction {

    /**
	 * @see org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction#buildNormalContext(VelocityPortlet, Context, RunData)
     */
    protected void buildNormalContext(
        VelocityPortlet portlet,
        Context context,
        RunData rundata)
        throws Exception {	
        context.put("action", "portlets.CustomizeLocale");
    }

    public void doChangelocale (RunData data, Context context) throws Exception
    {
        String defaultLanguage = "en";
        String defaultCountry = "US";
    	
        Locale locale = (Locale)data.getUser().getTemp("locale");
        if(locale != null) {
            defaultLanguage = locale.getLanguage();
            defaultCountry = locale.getCountry();
        }
    		
        // put code here to add the user to the system
	    String language = data.getParameters().getString("locale_language", defaultLanguage);
	    String country = data.getParameters().getString("locale_country", defaultCountry);
		
	    if(Log.getLogger().isDebugEnabled()) {
	        Log.debug("Changing language to " + language);
	        Log.debug("Changing country to " + country);
        }
		
        Locale newLocale = new Locale(language, country);
        data.getUser().setTemp("locale", newLocale);
    }

}
