Changing language with h:graphicImage click event

1.8k views Asked by At

I want to change the language on click one of the images. There are two image and one of them is turkish the other one is english. If I click to the english language is english. Another one is turkish with onclick.Here is my managed bean and xhtml;

public class DilSecimBean {

private boolean isTurkey = true;
private final Locale TR = new Locale("tr");
private final Locale EN = Locale.ENGLISH;

public Locale getLocale() {
    if (isTurkey) {
        return TR;
    } else {
        return EN;
    }
}

public void swapLocale() {
    System.out.println("SWAP LOCALE");
    switchLocale();
}

private void switchLocale() {
    isTurkey = !isTurkey;
    Locale newLocale;
    if (isTurkey) {
        newLocale = TR;
    } else {
        newLocale = EN;
    }
    FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);

}

}

Here is my xhtml;

 <h:panelGrid columns="3" border="0">
            <h:outputText value="Dil seçimi : " />
            <h:graphicImage alt="JSF"
                        url="/resimler/tb.png"
                        width="20" height="20">
                <f:ajax event="click" execute="#{dilSecimBean.swapLocale()}"/>
            </h:graphicImage>
            <h:graphicImage alt="JSFS"
                        url="/resimler/ib.png"
                        width="20" height="20">
                <f:ajax event="click" execute="#{dilSecimBean.swapLocale()}"/>
            </h:graphicImage>

        </h:panelGrid>

When I click to the image there is no change on the language.How can I change the language with image click event?

1

There are 1 answers

0
hanan Ahmed On BEST ANSWER

first you should have a form to submit second: why are yusing ajax here? any way here is a working example

<h:form>
      <h:commandLink action="#{localeChanger.turkishAction}">
        <h:graphicImage library="images" name="de_flag.gif"
                        style="border: 0px; margin-right: 1em;"/>
     </h:commandLink>
     <h:commandLink action="#{localeChanger.englishAction}">
        <h:graphicImage library="images"
                        name="en_flag.gif" style="border: 0px"/>
     </h:commandLink>
         </h:form>

for the managed bean:

       import java.io.Serializable;
       import java.util.Locale;
      import javax.inject.Named;
      // or import javax.faces.bean.ManagedBean;
        import javax.enterprise.context.SessionScoped;
     // or import javax.faces.bean.SessionScoped;
        import javax.faces.context.FacesContext;

  @Named // or @ManagedBean
 @SessionScoped
 public class LocaleChanger implements Serializable {

public String TurkishAction() {
    FacesContext context = FacesContext.getCurrentInstance();
    context.getViewRoot().setLocale(new Locale("tr"));
    return null;
}

public String englishAction() {
    FacesContext context = FacesContext.getCurrentInstance();
    context.getViewRoot().setLocale(Locale.ENGLISH);
    return null;
   }
}