dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #31990
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16346: updated i18nformat to use system calender for format patterns
------------------------------------------------------------
revno: 16346
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-08-08 14:44:29 +0700
message:
updated i18nformat to use system calender for format patterns
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18nFormat.java
dhis-2/dhis-services/dhis-service-core/src/main/resources/i18n_global.properties
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java 2014-06-06 12:39:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/AbstractCalendar.java 2014-08-08 07:44:29 +0000
@@ -116,6 +116,15 @@
}
@Override
+ public String formattedDate( String dateFormat, DateUnit dateUnit )
+ {
+ return dateFormat
+ .replace( "yyyy", String.format( "%04d", dateUnit.getYear() ) )
+ .replace( "MM", String.format( "%02d", dateUnit.getMonth() ) )
+ .replace( "dd", String.format( "%02d", dateUnit.getDay() ) );
+ }
+
+ @Override
public String formattedIsoDate( DateUnit dateUnit )
{
dateUnit = toIso( dateUnit );
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java 2014-05-20 08:28:55 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/calendar/Calendar.java 2014-08-08 07:44:29 +0000
@@ -71,6 +71,16 @@
String formattedDate( DateUnit dateUnit );
/**
+ * Formats dateUnit using supplied date format
+ *
+ * @param dateFormat Date format to use
+ * @param dateUnit DateUnit representing local year, month, day
+ * @return Default date format
+ * @see #getDateFormat()
+ */
+ String formattedDate( String dateFormat, DateUnit dateUnit );
+
+ /**
* Formats dateUnit using dateFormat and ISO 8601
*
* @param dateUnit DateUnit representing local year, month, day
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18nFormat.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18nFormat.java 2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18nFormat.java 2014-08-08 07:44:29 +0000
@@ -28,6 +28,11 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import org.hisp.dhis.calendar.DateUnit;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.period.WeeklyPeriodType;
+
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.DecimalFormat;
@@ -36,9 +41,6 @@
import java.util.Date;
import java.util.ResourceBundle;
-import org.hisp.dhis.period.Period;
-import org.hisp.dhis.period.WeeklyPeriodType;
-
/**
* @author Pham Thi Thuy
* @author Nguyen Dang Quang
@@ -49,12 +51,12 @@
private static final DecimalFormat FORMAT_VALUE = new DecimalFormat( "#.#" ); // Fixed for now
private static final String EMPTY = "";
private static final String NAN = "NaN";
- private static final String INVALID_DATE="Invalid date format";
-
+ private static final String INVALID_DATE = "Invalid date format";
+
public static final String FORMAT_DATE = "yyyy-MM-dd";
public static final String FORMAT_TIME = "HH:mm";
public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm";
-
+
private ResourceBundle resourceBundle;
public I18nFormat( ResourceBundle resourceBundle )
@@ -88,16 +90,48 @@
String[] shortWeekdays = { "weekday.short.sunday", "weekday.short.monday", "weekday.short.tuesday",
"weekday.short.wednesday", "weekday.short.thursday", "weekday.short.friday", "weekday.short.saturday" };
+ String calendarName = PeriodType.getCalendar().name() + ".";
+
for ( int i = 0; i < 12; ++i )
{
- months[i] = resourceBundle.getString( months[i] );
- shortMonths[i] = resourceBundle.getString( shortMonths[i] );
+ if ( resourceBundle.containsKey( calendarName + months[i] ) )
+ {
+ months[i] = resourceBundle.getString( calendarName + months[i] );
+ }
+ else
+ {
+ months[i] = resourceBundle.getString( months[i] );
+ }
+
+ if ( resourceBundle.containsKey( calendarName + shortMonths[i] ) )
+ {
+ shortMonths[i] = resourceBundle.getString( calendarName + shortMonths[i] );
+ }
+ else
+ {
+ shortMonths[i] = resourceBundle.getString( shortMonths[i] );
+ }
}
for ( int i = 0; i < 7; ++i )
{
- weekdays[i] = resourceBundle.getString( weekdays[i] );
- shortWeekdays[i] = resourceBundle.getString( shortWeekdays[i] );
+ if ( resourceBundle.containsKey( calendarName + weekdays[i] ) )
+ {
+ weekdays[i] = resourceBundle.getString( calendarName + weekdays[i] );
+ }
+ else
+ {
+ weekdays[i] = resourceBundle.getString( weekdays[i] );
+ }
+
+ if ( resourceBundle.containsKey( calendarName + shortWeekdays[i] ) )
+ {
+ shortWeekdays[i] = resourceBundle.getString( calendarName + shortWeekdays[i] );
+ }
+ else
+ {
+ shortWeekdays[i] = resourceBundle.getString( shortWeekdays[i] );
+ }
}
SimpleDateFormat dateFormat = new SimpleDateFormat();
@@ -184,7 +218,7 @@
{
return null;
}
-
+
String typeName = period.getPeriodType().getName();
if ( typeName.equals( WeeklyPeriodType.NAME ) ) // Use ISO dates due to potential week confusion
@@ -194,9 +228,35 @@
String keyStartDate = "format." + typeName + ".startDate";
String keyEndDate = "format." + typeName + ".endDate";
-
- String startDate = commonFormatting( period.getStartDate(), resourceBundle.getString( keyStartDate ) );
- String endDate = commonFormatting( period.getEndDate(), resourceBundle.getString( keyEndDate ) );
+
+ String startPattern = resourceBundle.getString( keyStartDate );
+ String endPattern = resourceBundle.getString( keyEndDate );
+
+ boolean dayPattern = startPattern.contains( "dd" ) || endPattern.contains( "dd" );
+
+ Date periodStartDate = period.getStartDate();
+ Date periodEndDate = period.getEndDate();
+
+ DateUnit start = PeriodType.getCalendar().fromIso( DateUnit.fromJdkDate( periodStartDate ) );
+ DateUnit end = PeriodType.getCalendar().fromIso( DateUnit.fromJdkDate( periodEndDate ) );
+
+ String startDate;
+ String endDate;
+
+ if ( !dayPattern )
+ {
+ // set day to first of month, so that we don't overflow when we convert to jdk date
+ start.setDay( 1 );
+ end.setDay( 1 );
+
+ startDate = commonFormatting( new DateUnit( start, true ).toJdkDate(), startPattern );
+ endDate = commonFormatting( new DateUnit( end, true ).toJdkDate(), endPattern );
+ }
+ else
+ {
+ startDate = PeriodType.getCalendar().formattedDate( startPattern, start );
+ endDate = PeriodType.getCalendar().formattedDate( endPattern, end );
+ }
try
{
@@ -207,11 +267,11 @@
return INVALID_DATE;
}
}
-
+
/**
* Formats value. Returns empty string if value is null. Returns NaN if value
* is not a number.
- *
+ *
* @param value the value to format.
*/
public String formatValue( Object value )
@@ -220,7 +280,7 @@
{
return EMPTY;
}
-
+
try
{
return FORMAT_VALUE.format( value );
@@ -230,7 +290,7 @@
return NAN;
}
}
-
+
// -------------------------------------------------------------------------
// Support methods
// -------------------------------------------------------------------------
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/i18n_global.properties'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/i18n_global.properties 2014-08-08 06:38:03 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/i18n_global.properties 2014-08-08 07:44:29 +0000
@@ -234,8 +234,8 @@
format.FinancialApril.endDate=MMM yyyy
format.FinancialJuly.startDate=MMM yyyy 'to '
format.FinancialJuly.endDate=MMM yyyy
-format.FinancialOct.startDate='FY '
-format.FinancialOct.endDate= yyyy
+format.FinancialOct.startDate=MMM yyyy 'to '
+format.FinancialOct.endDate=MMM yyyy
#-- Months and weeks -----------------------------------------------------------#