Solved

Change a locale dynamically

Posted on 2014-01-03
16
250 Views
Last Modified: 2014-02-10
I'm trying to change the locale of my application , but nothing happens . it always stays at a default definition. I'm using JSF and Bean Validation and MessagesProperties of BeanValidation I know that does't need any configuration at faces-config.xml

@Named
@SessionScoped
public class UsuarioMB implements Serializable {

	private static final long serialVersionUID = 1L;
	private String userCSS = "estilo.css";
&#9;private List<String> cssDisponivel;
&#9;private String tema;
&#9;private String linguaEscolhida="pt" ;
&#9;private Locale locale;

&#9;private int tabAtiva;

&#9;public void inicializar() {
&#9;&#9;cssDisponivel = new ArrayList<String>();
&#9;&#9;cssDisponivel.add("estilo.css");
&#9;&#9;cssDisponivel.add("estilo2.css");
&#9;}

&#9;public void alterarTema() {
&#9;&#9;System.out.println("alterado para: " + tema);
&#9;}

&#9;public String alterarEstilo() {
&#9;&#9;return null;
&#9;}

&#9;public String alterarIdioma() {
&#9;&#9;locale = new Locale(linguaEscolhida);
&#9;&#9;FacesContext instance = FacesContext.getCurrentInstance();
&#9;&#9;instance.getViewRoot().setLocale(locale);
&#9;&#9;return null;
&#9;}

&#9;public List<String> getCssDisponivel() {
&#9;&#9;return cssDisponivel;
&#9;}

&#9;public void setCssDisponivel(List<String> cssDisponivel) {
&#9;&#9;this.cssDisponivel = cssDisponivel;
&#9;}

&#9;public String getUserCSS() {
&#9;&#9;return userCSS;
&#9;}

&#9;public void setUserCSS(String userCSS) {
&#9;&#9;this.userCSS = userCSS;
&#9;}

&#9;public String getTema() {
&#9;&#9;if (tema == null) {
&#9;&#9;&#9;tema = "bluesky";
&#9;&#9;}

&#9;&#9;return tema;
&#9;}

&#9;public void setTema(String tema) {
&#9;&#9;this.tema = tema;
&#9;}

&#9;public String getLinguaEscolhida() {
&#9;&#9;return linguaEscolhida;
&#9;}

&#9;public void setLinguaEscolhida(String linguaEscolhida) {
&#9;&#9;this.linguaEscolhida = linguaEscolhida;
&#9;}

&#9;public Locale getLocale() {
&#9;&#9;if (locale == null) {
&#9;&#9;&#9;locale = new Locale(linguaEscolhida);
&#9;&#9;}

&#9;&#9;return locale;
&#9;}

&#9;public int getTabAtiva() {
&#9;&#9;return tabAtiva;
&#9;}

&#9;public void setTabAtiva(int tabAtiva) {
&#9;&#9;this.tabAtiva = tabAtiva;
&#9;}

&#9;public void onTabChange(TabChangeEvent event) {
&#9;&#9;TabView tabView = (TabView) event.getComponent();
&#9;&#9;setTabAtiva(tabView.getActiveIndex());
&#9;}

Open in new window



My faces config:

<message-bundle>br.com.confiabilidade.mensagens.Messages</message-bundle>

&#9;&#9;<resource-bundle>
&#9;&#9;&#9;<base-name>br.com.confiabilidade.mensagens.sistema</base-name>
&#9;&#9;&#9;<var>mensagens</var>
&#9;&#9;</resource-bundle>

&#9;&#9;<locale-config>
&#9;&#9;&#9;<default-locale>en</default-locale>
&#9;&#9;&#9;<supported-locale>pt</supported-locale>
&#9;&#9;&#9;<supported-locale>en</supported-locale>
&#9;&#9;</locale-config>
&#9;</application>

Open in new window



I've put at root view:

<f:view contentType="text/html; charset=UTF-8" encoding="UTF-8" locale="#{usuarioMB.locale}">
0
Comment
Question by:Rafael_Moreira
  • 8
  • 6
16 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 39758577
How you are setting linguaEscolhida?
If you change
return linguaEscolhida; to
return "pt";
is it working?
0
 

Author Comment

by:Rafael_Moreira
ID: 39760159
When i put

      private String linguaEscolhida="en" ;

the locale change to english
and

when put

public void setLinguaEscolhida(String linguaEscolhida) {
            this.linguaEscolhida = "pt";
      }

with the atribute above(private String linguaEscolhida="en" ;

Does't change. keep "en"


Looks like that my method its' not working.
0
 
LVL 11

Expert Comment

by:Manish
ID: 39761239
Whatever value you are setting in method, locale should work on that.
Is it working if you set
public void setLinguaEscolhida(String linguaEscolhida) {
            this.linguaEscolhida = "pt";
      }

?
0
 

Author Comment

by:Rafael_Moreira
ID: 39761753
No . take a look:

public void setLinguaEscolhida(String linguaEscolhida) {
            this.linguaEscolhida = "en";
      }

output on console:



      public Locale getLocale() {
            if (locale == null) {
                  locale = new Locale(linguaEscolhida);
                  System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            }

            return locale;
      }

português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
português --- português


Here is the code  that change the locale

private String linguaEscolhida = "pt";

or

private String linguaEscolhida = "en";
0
 
LVL 11

Expert Comment

by:Manish
ID: 39761759
If you are not calling setLinguaEscolhida method then locale will not change.
I am not getting - how you want to set locale if you are not calling setLinguaEscolhida  method?
0
 

Author Comment

by:Rafael_Moreira
ID: 39761774
In my view:

<p:selectOneMenu value="#{usuarioMB.linguaEscolhida}">  <----HERE
				<f:selectItem itemValue="pt"
					itemLabel="#{mensagens.internacionalizacaoPT}" />


				<f:selectItem itemValue="en"
					itemLabel="#{mensagens.internacionalizacaoEN}" />

			</p:selectOneMenu>

			<p:commandButton action="#{usuarioMB.alterarIdioma}" update="@form"
				value="#{mensagens.internacionalizacaoTrocar}"/>

Open in new window



and when i click at commandbutton  calls the metod:

public String alterarIdioma() {
            locale = new Locale(linguaEscolhida);
            FacesContext instance = FacesContext.getCurrentInstance();
            instance.getViewRoot().setLocale(locale);
            System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            return null;
      }

That would must be working . Would't it?
0
 
LVL 11

Expert Comment

by:Manish
ID: 39761809
so is it not getting set?
what is syout of linguaEscolhida values prints?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Rafael_Moreira
ID: 39762741
I put a sysout:

public Locale getLocale() {
            if (locale == null) {
                  locale = new Locale(linguaEscolhida);
                  System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            }

            return locale;
      }

português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
0
 
LVL 11

Expert Comment

by:Manish
ID: 39764318
No, I am asking about ,
when you click the command button,
what is sys out in alterarIdioma method?
0
 

Author Comment

by:Rafael_Moreira
ID: 39764795
i put a sysout at method and the result is equals:

public String alterarIdioma() {
            locale = new Locale(linguaEscolhida);
            FacesContext instance = FacesContext.getCurrentInstance();
            instance.getViewRoot().setLocale(locale);
            System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            return null;
      }

Jan 08, 2014 9:06:37 AM org.apache.catalina.startup.Catalina start
Informações: Server startup in 16503 ms
português --- português
português --- português
português --- português
português --- português
português --- português


only change if i meke a alteration at  :

private String linguaEscolhida = "em"

for example .
0
 
LVL 11

Expert Comment

by:Manish
ID: 39767468
When you select pt in dropdown, what value you get in alterarIdioma of linguaEscolhida?
ALso check you are importing correct package for Named and SessionScope annotation?
0
 

Author Comment

by:Rafael_Moreira
ID: 39769947
i vrrifyed that the value of drop down its not been captured.

public String alterarIdioma() {
		locale = new Locale(linguaEscolhida);
		FacesContext instance = FacesContext.getCurrentInstance();
		instance.getViewRoot().setLocale(locale);
		System.out.println("lingua----> "+linguaEscolhida +"Local ----->"+locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
		return null;
	}

Open in new window


susout:

lingua----> portuguêsLocal ----->português --- português

when i have choosed English.
0
 

Accepted Solution

by:
Rafael_Moreira earned 0 total points
ID: 39777008
With sessionecope

import javax.enterprise.context.SessionScoped;

it Works . the locale change in sysout method alterarIdioma, but the labels does' t change.

very strange .
the faces-config :


      <application>

            <resource-bundle>
                  <base-name>br.com.confiabilidade.resources.sistema</base-name>
                  <var>msg</var>
            </resource-bundle>

            <locale-config>

                  <default-locale>pt_BR</default-locale>
                  <supported-locale>pt</supported-locale>
                  <supported-locale>en</supported-locale>
            </locale-config>
      </application>

      <factory>
            <exception-handler-factory>
                  br.com.confiabilidade.util.jsf.JsfExceptionHandlerFactory
            </exception-handler-factory>
      </factory>

</faces-config>

se the link of my  Developper enviorment:

http://snag.gy/XAYQ1.jpg


ps. i m using bean validation so ValidationMessages_xx.properties has the messages of the project
0
 

Author Closing Comment

by:Rafael_Moreira
ID: 39846843
tha ajax was the problem .

When i used jsf without ajax it Works.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now