Solved

Change a locale dynamically

Posted on 2014-01-03
16
254 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

856 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