Commit 174e15f05687273fe93857dac0a29de2029bc2af

Authored by Dancovich
1 parent fbd040d4
Exists in master

IN PROGRESS - issue FWK-93: Definir resource bundle através da

informação de idioma do navegador (HTTP header language) 
https://demoiselle.atlassian.net/browse/FWK-93
archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/resources/messages_en.properties 0 → 100644
... ... @@ -0,0 +1,45 @@
  1 +menu.bookmark=Bookmarks
  2 +bookmark.label=Bookmark
  3 +
  4 +bookmark-delete-ok=Bookmark removed\: {0}
  5 +bookmark-insert-ok=Bookmark inserted\: {0}
  6 +bookmark-update-ok=Bookmark updated\: {0}
  7 +
  8 +bookmark.list.table.title=All Links
  9 +
  10 +bookmark.label.id=ID
  11 +bookmark.label.link=Link
  12 +bookmark.label.description=Description
  13 +
  14 +bookmark.alt.id=ID
  15 +bookmark.alt.link=Link
  16 +bookmark.alt.description=Description
  17 +
  18 +button.add.new=Insert New
  19 +button.back=Back
  20 +button.delete=Remove
  21 +button.dialog.no=No, sorry\!
  22 +button.dialog.yes=Yes, sure\!
  23 +button.edit=Edit
  24 +button.new=New
  25 +button.save=Save
  26 +
  27 +label.action=Action
  28 +label.dialog.alert=Alert
  29 +label.dialog.delete=Remove
  30 +label.confirm.delete=Confirm?
  31 +label.date.pattern=MM/dd/yyyy
  32 +
  33 +main.app.title=Bookmarks
  34 +main.app.welcome=Welcome to the example application Bookmark. This is your starting point, feel free to change this application.
  35 +main.change.skin=Change Skin
  36 +main.skin=Skin
  37 +main.footer.text=Example Application for Demoiselle ${parent.version}
  38 +
  39 +menu.language=Language
  40 +menu.menuitem.language-portuguese=Portuguese
  41 +menu.menuitem.language-english=English
  42 +menu.menuitem.contents=Content
  43 +menu.menuitem.list=List
  44 +menu.menuitem.new=New
  45 +menu.menuitem.quit=Quit
... ...
archetype/jsf-jpa/src/main/resources/archetype-resources/src/main/webapp/template/main.xhtml
1 1 <html xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"
2 2 xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
3 3  
4   -<f:view contentType="text/html" locale="#{currentLocale}" />
  4 +<f:view contentType="text/html" />
5 5  
6 6 <h:head>
7 7 <title>#{messages['main.app.title']}</title>
... ...
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/Locales.java
... ... @@ -37,8 +37,10 @@
37 37 package br.gov.frameworkdemoiselle.util;
38 38  
39 39 import java.io.Serializable;
  40 +import java.util.Iterator;
40 41 import java.util.Locale;
41 42  
  43 +import javax.enterprise.context.SessionScoped;
42 44 import javax.faces.context.FacesContext;
43 45 import javax.inject.Inject;
44 46 import javax.inject.Named;
... ... @@ -49,25 +51,85 @@ import javax.inject.Named;
49 51 * @author SERPRO
50 52 * */
51 53 @Named
  54 +@SessionScoped
52 55 public class Locales implements Serializable {
53 56  
54 57 private static final long serialVersionUID = 1L;
55 58  
56 59 private static final Locale PT_BR = new Locale("pt", "BR");
  60 +
  61 + private Locale locale = Locale.getDefault();
57 62  
58 63 @Inject
59 64 private FacesContext facesContext;
60 65  
  66 + /**
  67 + * Set the language to "en_US". This is a shorthand to <code>setLocale(Locale.US)</code>.
  68 + */
61 69 public void setEnglish() {
62 70 setLocale(Locale.US);
63 71 }
64 72  
  73 + /**
  74 + * Set the language to "pt_BR". This is a shorthand to <code>setLocale(Locales.PT_BR)</code>.
  75 + */
65 76 public void setPortuguese() {
66 77 setLocale(PT_BR);
67 78 }
68   -
69   - protected void setLocale(Locale locale) {
70   - facesContext.getApplication().setDefaultLocale(locale);
  79 +
  80 + /**
  81 + * @return The current locale, or {@link Locale#getDefault()} if one has not been set.
  82 + */
  83 + public Locale getLocale(){
  84 + return this.locale!=null ? this.locale : Locale.getDefault();
71 85 }
72 86  
  87 + /**
  88 + * Set the locale for the current view
  89 + *
  90 + * @param locale The new locale
  91 + */
  92 + public void setLocale(Locale locale) {
  93 + Iterator<Locale> supportedLocales = getContext().getApplication().getSupportedLocales();
  94 + if (supportedLocales==null){
  95 + this.locale = locale;
  96 + getContext().getViewRoot().setLocale(this.locale);
  97 + }
  98 + else{
  99 + boolean selectedLocale = false;
  100 + while(supportedLocales.hasNext()){
  101 + Locale supportedLocale = supportedLocales.next();
  102 + if (supportedLocale.equals(locale)){
  103 + this.locale = locale;
  104 + getContext().getViewRoot().setLocale(this.locale);
  105 + selectedLocale = true;
  106 + break;
  107 + }
  108 + }
  109 +
  110 + if (!selectedLocale && this.locale==null){
  111 + this.locale = Locale.getDefault();
  112 + }
  113 + }
  114 + }
  115 +
  116 + /**
  117 + * Set the default locale for the entire application. After this call
  118 + * all views from this application will use this locale (unless a specific
  119 + * session defined a different locale using {@link #setLocale(Locale locale)}).
  120 + *
  121 + * @param locale The locale to set
  122 + */
  123 + public void setApplicationLocale(Locale locale) {
  124 + setLocale(locale);
  125 + getContext().getApplication().setDefaultLocale(this.locale);
  126 + }
  127 +
  128 + private FacesContext getContext(){
  129 + if (facesContext==null){
  130 + facesContext = Beans.getReference(FacesContext.class);
  131 + }
  132 +
  133 + return facesContext;
  134 + }
73 135 }
... ...