[Logo] Mentawai Forum - Mentawai Web Framework
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 

Forum Read-Only! Check the new site and forum here!

FormatterManager + I18n (sugestão)  XML
Forum Index -> Comentários Gerais
Author Message
dtorres



Joined: 04/11/2006 12:53:44
Messages: 5
Offline

Existe algum suporte a internalinalização para o FormatterManager?

Por exemplo a data no formato brasileiro deve ser dd/mm/yyyy enquanto no formato america é mm/dd/yyyy.

Estava pensando em ter um arquivo i18n separado somente para o FormatterManager. Apenas uma sugestão.
velo


[Avatar]

Joined: 16/02/2006 13:33:54
Messages: 1197
Location: Jaraguá do Sul - SC
Offline

Tem sim...

Code:
 /*
  * Mentawai Web Framework <a href="http://mentawai.lohis.com.br/" target="_blank" rel="nofollow">http://mentawai.lohis.com.br/</a>
  * Copyright (C) 2005  Sergio Oliveira Jr. (sergio.oliveira.jr@gmail.com)
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 package org.mentawai.formatter;
 
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Locale;
 
 public class DateFormatter implements Formatter {
 	
 	private int style = -1;
 	
 	private SimpleDateFormat sdf = null;
 	
 	public DateFormatter(int style) {
 		
 		this.style = style;
 	}
 	
 	public DateFormatter(String pattern) {
 		
 		this.sdf = new SimpleDateFormat(pattern);
 	}
 	
 	public String format(Object value, Locale loc) {
 		
 		if (!(value instanceof Date)) return value.toString();
 		
 		Date d = (Date) value;
 		
 		if (sdf != null) {
 			
 			return sdf.format(d);
 			
 		} else if (style != -1) {
 			
 			DateFormat df = DateFormat.getDateInstance(style, loc);
 			
 			return df.format(d);
 			
 		} else {
 			
 			throw new IllegalStateException("Should never be here!");
 		}
 	}
 	
 	
 	
 }
 


VELO

_____________________________________
Mentawai Developer
"When the only tool you have is a hammer, everything looks like a nail"
http://en.wikipedia.org/wiki/Golden_hammer
[Email] [MSN]
saoj



Joined: 01/07/2005 09:59:17
Messages: 2846
Location: Rio de Janeiro, RJ
Offline


Como o velo falou, os formatadores já suportam internacionalização.

Dá uma olhada na interface Formatter.

O problema é que o SimpleDateFormat não suporta internacionalização via locale. Só o DateFormat se eu não me engano.

Mas nada te impede de fazer o seu próprio Formatter com a sua lógica de internacionalização lá dentro, ou seja, com vários SimpleDateFormat, um para cada locale que vc suporta.



Sergio Oliveira

dtorres



Joined: 04/11/2006 12:53:44
Messages: 5
Offline

Legal, logo depois q postei comecei a baixar os fontes e vi essa possibilidade. Mas estava pensando para a versão 2.0 algo mais "mastigado" para o desenvolvedor que as vezes precisa de formatos variados de data.

Por exemplo ter um arquivo separado por pasta como acontece quando utiliza listas (/lists) só que um para o formatter (/formatters)

O arquivo poderia ser assim:

# pt_BR
simpleDate = dd/mm/YYYY
simpleDateWithDash = dd-mm-yyyy

# en_US
simpleDate = mm/dd/YYYY
simpleDateWithDash = mm-dd-yyyy 


Então no código java o ApplicationManager ficaria assim:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {

ActionConfig ac;

public void init(Context application) {
...
}

public void loadActions() {
...
}

public void loadLists() throws IOException {
...
}

public void loadLocales() {
LocaleManager.add(new Locale("pt", "BR"));
LocaleManager.add(new Locale("en", "US"));
}

public void loadFormatters() {
FormatterManager.addFormatter("simple", new DateFormatter("simpleDate"));
FormatterManager.addFormatter("withDash", new DateFormatter("simpleDateWithDash"));
}
}
 


E na view sairia assim por exemplo:

<mtw:out value="birthdate" formatter="simple"/>
<mtw:out value="birthdate" formatter="withDash"/>
 





velo


[Avatar]

Joined: 16/02/2006 13:33:54
Messages: 1197
Location: Jaraguá do Sul - SC
Offline

O construtor permite isso.

Você pode fazer miséria nesse sentido.
Code:
  	
  	public DateFormatter(int style) {
  		
  		this.style = style;
  	}
  	
  	public DateFormatter(String pattern) {
  		
  		this.sdf = new SimpleDateFormat(pattern);
  	}
  	
 


Eh soh passar o pattern no construtor....

Podia sobrecarregar o formatador mais um pokinho e para aceitar uma instancia de DateFormatter, daih fica 300% flexivel.

VELO

_____________________________________
Mentawai Developer
"When the only tool you have is a hammer, everything looks like a nail"
http://en.wikipedia.org/wiki/Golden_hammer
[Email] [MSN]
dtorres



Joined: 04/11/2006 12:53:44
Messages: 5
Offline

Pode crer, não tinha pensado nisso!

Vlw mesmo!

velo


[Avatar]

Joined: 16/02/2006 13:33:54
Messages: 1197
Location: Jaraguá do Sul - SC
Offline

Todos casos, digivolvi o DateFormatter...

Coloquei mais um construtor e umas constraints.
Code:
 /*
  * Mentawai Web Framework <a href="http://mentawai.lohis.com.br/" target="_blank" rel="nofollow">http://mentawai.lohis.com.br/</a>
  * Copyright (C) 2005  Sergio Oliveira Jr. (sergio.oliveira.jr@gmail.com)
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 package org.mentawai.formatter;
 
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Locale;
 
 public class DateFormatter implements Formatter {
 
 	/**
 	 * Constant for full style pattern.
 	 */
 	public static final int FULL = 0;
 
 	/**
 	 * Constant for long style pattern.
 	 */
 	public static final int LONG = 1;
 
 	/**
 	 * Constant for medium style pattern.
 	 */
 	public static final int MEDIUM = 2;
 
 	/**
 	 * Constant for short style pattern.
 	 */
 	public static final int SHORT = 3;
 
 	private int dateStyle = -1;
 
 	private DateFormat dateFormatter = null;
 
 	/**
 	 * @param dateStyle
 	 *            a value from 0 to 3 indicating the time format, ignored if
 	 *            flags is 1
 	 */
 	public DateFormatter(int dateStyle) {
 
 		this.dateStyle = dateStyle;
 	}
 
 	public DateFormatter(String pattern) {
 
 		this.dateFormatter = new SimpleDateFormat(pattern);
 	}
 	
 	public DateFormatter() {
 		this.dateFormatter = DateFormat.getDateInstance();
 	}
 
 	public DateFormatter(DateFormat dateFormatter) {
 		if(dateFormatter == null){
 			throw new NullPointerException("Date Formatter can't be null, use Default constructor for that.");
 		}
 		this.dateFormatter = dateFormatter;
 	}
 
 	public String format(Object value, Locale loc) {
 
 		if (!(value instanceof Date))
 			return value.toString();
 
 		Date d = (Date) value;
 
 		if (dateFormatter != null) {
 
 			return dateFormatter.format(d);
 
 		} else if (dateStyle != -1) {
 
 			DateFormat df = DateFormat.getDateInstance(dateStyle, loc);
 
 			return df.format(d);
 
 		} else {
 
 			throw new IllegalStateException("Should never be here!");
 		}
 	}
 
 }
 

_____________________________________
Mentawai Developer
"When the only tool you have is a hammer, everything looks like a nail"
http://en.wikipedia.org/wiki/Golden_hammer
[Email] [MSN]
 
Forum Index -> Comentários Gerais
Go to:   
Powered by JForum 2.1.6 © JForum Team