Changing language by clicking a button

血红的双手。 提交于 2019-12-04 16:41:31

All the steps in the tutorial are not really needed.
What I did (And it works) is:
1. In loging.xhtm, for instance:

    <f:view locale="#{languageDetails.locale}" >
    <head>
    .....
    <f:loadBundle basename="messages.Messages" var="msg1"/>
    .....
<f:view>
     <h:form>
          <h:panelGrid columns="2">
                <h:outputText value="Select Language"></h:outputText>
                <h:selectOneMenu id="dropdown" value="#{languageDetails.locale}">
                      <f:selectItem itemValue="en" itemLabel="English" />
                      <f:selectItem itemValue="es" itemLabel="Spanish" />
                      <f:selectItem itemValue="de" itemLabel="German" />
                </h:selectOneMenu>
          </h:panelGrid>
          <p><h:commandButton id="change" value="Change Language"
           action="#{languageDetails.changeLanguage}" /></p>


      </h:form>
</f:view>


    </body>
    </f:view>

2.In java source code I also made some changes:

public class LanguageDetails {

    private static String locale = Locale.getDefault().getDisplayLanguage();

      public void setLocale(String locale1) {
        this.locale = locale1;
      }

      public synchronized String getLocale() {
        return locale;
      }

      public synchronized String changeLanguage() {
        return "changed";
      }
}

And that's all.
Hope this could help

I needed to implement it as a button, which when I click on, the language changes. I have 2 locales (en, cs). Here's my solution:

1.command button:

<h:form>
    <h:commandButton id="change" value="#{locale.locale}" action="#{locale.changeLanguage}" />
</h:form>

2.locale bean:

@ManagedBean(name = "locale")
@SessionScoped
public class LocaleBean {

private static String locale = "en";

  public void setLocale(String locale1) {
    LocaleBean.locale = locale1;
  }

  public synchronized String getLocale() {
    return locale;
  }

  public synchronized String changeLanguage() {
    if (!locale.contains("cs")) {
        setLocale("cs");
    } else {
        setLocale("en");
    }
      return "changed";
  }
}

Maybe this could be helpful for someone.

In the JSF tutorial, language.jsp is a webpage which allow the user to select the locale of their choice. In the example, language.jsp page is not internationalized. When you select a language from the drop down and click change language button, the next pages (which are internationalized)will be shown in the selected language. As clearly mentioned in the tutorial since the 'locale' attribute in f:view tag supports EL we can include <f:view locale="#{languageDetails.locale}"> in the next pages(UserDetailsForm.jsp and userDetailsSubmitted.jsp).

You can change it by this code in bean, controller, etc...

FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.ENGLISH);

Another approach If you are using Spring, is to declare these beans and call URL with parameter "locale=xx" to change locale:

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="es" />
</bean>

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

if you are using JSF you can use

FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.ENGLISH);

but, sometimes you need to send some messages in specific users using them languages of registration then you need to user this

messages.getLocale().setDefault(
            new Locale(comm.getAccount().getLocale()));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!