Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Change a locale dynamically

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
Rafael_Moreira
Asked:
Rafael_Moreira
  • 8
  • 6
1 Solution
 
ManishLeadCommented:
How you are setting linguaEscolhida?
If you change
return linguaEscolhida; to
return "pt";
is it working?
0
 
Rafael_MoreiraAuthor Commented:
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
 
ManishLeadCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Rafael_MoreiraAuthor Commented:
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
 
ManishLeadCommented:
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
 
Rafael_MoreiraAuthor Commented:
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
 
ManishLeadCommented:
so is it not getting set?
what is syout of linguaEscolhida values prints?
0
 
Rafael_MoreiraAuthor Commented:
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
 
ManishLeadCommented:
No, I am asking about ,
when you click the command button,
what is sys out in alterarIdioma method?
0
 
Rafael_MoreiraAuthor Commented:
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
 
ManishLeadCommented:
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
 
Rafael_MoreiraAuthor Commented:
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
 
Rafael_MoreiraAuthor Commented:
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
 
Rafael_MoreiraAuthor Commented:
tha ajax was the problem .

When i used jsf without ajax it Works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now