2012-07-31 50 views
0

我使用JSF 2.0和Spring 3.0.2和tomcat 7.0.14.0作爲服務器。我遇到了改變我的網站語言的問題。所有的代碼在我的本地服務器上運行良好。但是當我在服務器上部署時,語言更改不起作用。它會自動選擇意大利語作爲默認語言。當我點擊另一種語言時,沒有任何變化。 這裏是我的豆代碼:JSF2.0國際化不能在服務器上工作

  @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("Italian", Locale.ITALIAN); 
} 

public LanguageBean() { 
    countries = new LinkedHashMap<String,Object>(); 


      countries.put("English", Locale.ENGLISH); //label, value 
      countries.put("Italian", Locale.ITALIAN); 

} 

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()) { 
      System.out.println("newLocaleValue "+newLocaleValue+"\n entry.getValue().toString()"+entry.getValue().toString()); 
     if(entry.getValue().toString().equals(newLocaleValue)){ 

      FacesContext.getCurrentInstance() 
       .getViewRoot().setLocale((Locale)entry.getValue()); 
        FacesContext context = FacesContext.getCurrentInstance(); 
        System.out.println("Default : "+context.getApplication().getDefaultLocale()); 

        context.getApplication().setDefaultLocale((Locale)entry.getValue()); 


     } 
    } 

} 

這是config.xml文件::

<locale-config> 
     <default-locale>en</default-locale> 
    </locale-config> 
    <resource-bundle> 
     <base-name>com.mad_u.welcome</base-name> 
     <var>msg</var> 
    </resource-bundle> 

請給我一些想法。提前致謝。

+0

誰能幫幫我嗎? – Rounak 2012-08-01 04:44:53

+0

this [thread1](http://stackoverflow.com/questions/4830588/jsf-locale-is-set-per-request-not-for-session),[thread2](http://www.coderanch.com/t/447920/JSF/java/JSF-Locale)會幫助你。 – 2012-08-01 08:07:49

回答

0

我有同樣的問題。我的屬性文件是這樣的:

  • message_en_US.properties
  • message_es_ES.properties
  • message_eu_ES.properties

它在我的本地服務器的工作,但在上傳網頁到我的服務器的語言變化沒有工作,所以我改變了這一點:

 countries.put("Castellano", new Locale("es")); 
     countries.put("Euskara", new Locale("eu")); 
     countries.put("English", new Locale("en")); 

<locale-config> 
      <default-locale>es</default-locale> 
      <supported-locale>es</supported-locale> 
      <supported-locale>eu</supported-locale> 
      <supported-locale>en</supported-locale> 
     </locale-config> 

  • message_en.properties
  • message_es.properties
  • message_eu.properties