Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change a locale dynamically

Posted on 2014-01-03
16
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

722 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