如何在jsf2.0中设置整个应用程序的语言环境

时间:2013-06-19 10:49:08

标签: java jsf-2 jboss6.x

在我的情况下,jsf locale是按请求设置的,而不是为会话设置的 我想在主页中选择语言,它反映了每个请求的整个应用程序,如何实现这个...

对于每个请求我都改变了这样的语言环境:

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

@ManagedBean(name="language")
@SessionScoped
public class LanguageBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private String localeCode;

    private static Map<String,Object> countries;
    static{
        countries = new LinkedHashMap<String,Object>();
        countries.put("English", Locale.ENGLISH); //label, value
        countries.put("Arabic", new Locale("ar"));
    }

    public Map<String, Object> getCountriesInMap() {
        return countries;
    }


    public String getLocaleCode() {
        return localeCode;
    }


    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }



    public void countryLocaleCodeChanged(ValueChangeEvent e){

        String newLocaleValue = e.getNewValue().toString();

        for (Map.Entry<String, Object> entry : countries.entrySet()) {

            if(entry.getValue().toString().equals(newLocaleValue)){

                FacesContext.getCurrentInstance()
                    .getViewRoot().setLocale((Locale)entry.getValue());
            }
        }
        FacesContext context = FacesContext.getCurrentInstance();
        String value = context.getApplication().evaluateExpressionGet(context, "#{msg['port']}", String.class);
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("hello="+value));

//        

    }

}

请帮忙 谢谢

0 个答案:

没有答案