Blanca Hdez
Blanca Hdez

Reputation: 3563

Changing language by clicking a button

I have a JSF/RichFaces setup, and I found this tutorial. I followed it step by step, but I can't manage to get it.

Also: language.jsp is mentioned in the tutorial, can anybody tell me what exaclty it is?

Or if you think this is not an appropiate tutorial and have a better one, please, let me know.

Upvotes: 1

Views: 14920

Answers (6)

Zouhair Kasmi
Zouhair Kasmi

Reputation: 662

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()));

Upvotes: 0

Andrés Oviedo
Andrés Oviedo

Reputation: 1428

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>

Upvotes: 0

Felix
Felix

Reputation: 227

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

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

Upvotes: 1

Micer
Micer

Reputation: 8979

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.

Upvotes: 2

user568141
user568141

Reputation: 11

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).

Upvotes: 1

Blanca Hdez
Blanca Hdez

Reputation: 3563

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

Upvotes: 6

Related Questions