From 174e15f05687273fe93857dac0a29de2029bc2af Mon Sep 17 00:00:00 2001 From: Dancovich Date: Wed, 23 Oct 2013 12:56:54 -0300 Subject: [PATCH] IN PROGRESS - issue FWK-93: Definir resource bundle através da informação de idioma do navegador (HTTP header language) --- archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/resources/messages_en.properties | 45 +++++++++++++++++++++++++++++++++++++++++++++ archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/webapp/template/main.xhtml | 2 +- impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Locales.java | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/resources/messages_en.properties diff --git a/archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/resources/messages_en.properties b/archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/resources/messages_en.properties new file mode 100644 index 0000000..540894e --- /dev/null +++ b/archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/resources/messages_en.properties @@ -0,0 +1,45 @@ +menu.bookmark=Bookmarks +bookmark.label=Bookmark + +bookmark-delete-ok=Bookmark removed\: {0} +bookmark-insert-ok=Bookmark inserted\: {0} +bookmark-update-ok=Bookmark updated\: {0} + +bookmark.list.table.title=All Links + +bookmark.label.id=ID +bookmark.label.link=Link +bookmark.label.description=Description + +bookmark.alt.id=ID +bookmark.alt.link=Link +bookmark.alt.description=Description + +button.add.new=Insert New +button.back=Back +button.delete=Remove +button.dialog.no=No, sorry\! +button.dialog.yes=Yes, sure\! +button.edit=Edit +button.new=New +button.save=Save + +label.action=Action +label.dialog.alert=Alert +label.dialog.delete=Remove +label.confirm.delete=Confirm? +label.date.pattern=MM/dd/yyyy + +main.app.title=Bookmarks +main.app.welcome=Welcome to the example application Bookmark. This is your starting point, feel free to change this application. +main.change.skin=Change Skin +main.skin=Skin +main.footer.text=Example Application for Demoiselle ${parent.version} + +menu.language=Language +menu.menuitem.language-portuguese=Portuguese +menu.menuitem.language-english=English +menu.menuitem.contents=Content +menu.menuitem.list=List +menu.menuitem.new=New +menu.menuitem.quit=Quit diff --git a/archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/webapp/template/main.xhtml b/archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/webapp/template/main.xhtml index 97b4aac..b7d1818 100644 --- a/archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/webapp/template/main.xhtml +++ b/archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/webapp/template/main.xhtml @@ -1,7 +1,7 @@ - + #{messages['main.app.title']} diff --git a/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Locales.java b/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Locales.java index 9428043..58634ef 100644 --- a/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Locales.java +++ b/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Locales.java @@ -37,8 +37,10 @@ package br.gov.frameworkdemoiselle.util; import java.io.Serializable; +import java.util.Iterator; import java.util.Locale; +import javax.enterprise.context.SessionScoped; import javax.faces.context.FacesContext; import javax.inject.Inject; import javax.inject.Named; @@ -49,25 +51,85 @@ import javax.inject.Named; * @author SERPRO * */ @Named +@SessionScoped public class Locales implements Serializable { private static final long serialVersionUID = 1L; private static final Locale PT_BR = new Locale("pt", "BR"); + + private Locale locale = Locale.getDefault(); @Inject private FacesContext facesContext; + /** + * Set the language to "en_US". This is a shorthand to setLocale(Locale.US). + */ public void setEnglish() { setLocale(Locale.US); } + /** + * Set the language to "pt_BR". This is a shorthand to setLocale(Locales.PT_BR). + */ public void setPortuguese() { setLocale(PT_BR); } - - protected void setLocale(Locale locale) { - facesContext.getApplication().setDefaultLocale(locale); + + /** + * @return The current locale, or {@link Locale#getDefault()} if one has not been set. + */ + public Locale getLocale(){ + return this.locale!=null ? this.locale : Locale.getDefault(); } + /** + * Set the locale for the current view + * + * @param locale The new locale + */ + public void setLocale(Locale locale) { + Iterator supportedLocales = getContext().getApplication().getSupportedLocales(); + if (supportedLocales==null){ + this.locale = locale; + getContext().getViewRoot().setLocale(this.locale); + } + else{ + boolean selectedLocale = false; + while(supportedLocales.hasNext()){ + Locale supportedLocale = supportedLocales.next(); + if (supportedLocale.equals(locale)){ + this.locale = locale; + getContext().getViewRoot().setLocale(this.locale); + selectedLocale = true; + break; + } + } + + if (!selectedLocale && this.locale==null){ + this.locale = Locale.getDefault(); + } + } + } + + /** + * Set the default locale for the entire application. After this call + * all views from this application will use this locale (unless a specific + * session defined a different locale using {@link #setLocale(Locale locale)}). + * + * @param locale The locale to set + */ + public void setApplicationLocale(Locale locale) { + setLocale(locale); + getContext().getApplication().setDefaultLocale(this.locale); + } + + private FacesContext getContext(){ + if (facesContext==null){ + facesContext = Beans.getReference(FacesContext.class); + } + + return facesContext; + } } -- libgit2 0.21.2