You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by sc...@apache.org on 2011/07/21 19:15:17 UTC
svn commit: r1149273 - in /velocity/tools/branches/2.0.x/src:
main/java/org/apache/velocity/tools/generic/DateTool.java
test/java/org/apache/velocity/tools/generic/DateToolTests.java
Author: schultz
Date: Thu Jul 21 17:15:16 2011
New Revision: 1149273
URL: http://svn.apache.org/viewvc?rev=1149273&view=rev
Log:
Fixed VELTOOLS-144: DateTool can be improved to allow cross-localization of format strings
- Added DateTool.toLocalizedString method
- Added DateToolTests class to test the new method
Added:
velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java
Modified:
velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/generic/DateTool.java
Modified: velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/generic/DateTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/generic/DateTool.java?rev=1149273&r1=1149272&r2=1149273&view=diff
==============================================================================
--- velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/generic/DateTool.java (original)
+++ velocity/tools/branches/2.0.x/src/main/java/org/apache/velocity/tools/generic/DateTool.java Thu Jul 21 17:15:16 2011
@@ -743,6 +743,34 @@ public class DateTool extends FormatConf
return ConversionUtils.toCalendar(date, locale);
}
+ /**
+ * Returns a localized date format pattern for the given format.
+ *
+ * SimpleDateFormat uses patterns that are based upon English
+ * words (such as Month = M, Day = d, and Year = y). When displaying
+ * a format pattern to readers of other languages, it is appropriate
+ * to display these patterns using their localized expectations.
+ * For instance, the date pattern yyyy-MM-dd should, for French speakers
+ * appear as "aaaa-MM-jj". {@link SimpleDateFormat#toLocalizedPattern}
+ * provides this functionality, and this method merely calls
+ * that on an appropriately-constructed SimpleDateFormat object.
+ *
+ * @param format the custom or standard pattern to convert
+ * @param locale the {@link Locale} to format for pattern for
+ * @return a format string appropriate for the specified Locale
+ * @since VelocityTools 2.0
+ */
+ public String toLocalizedPattern(String format,
+ Locale locale)
+ {
+ DateFormat df = getDateFormat(format, locale, getTimeZone());
+
+ // Just in case DateFormat.getInstance doesn't return SimpleDateFormat
+ if(df instanceof SimpleDateFormat)
+ return ((SimpleDateFormat)df).toLocalizedPattern();
+ else
+ return null; // Got a better idea?
+ }
// ------------------------- default toString() implementation ------------
Added: velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java?rev=1149273&view=auto
==============================================================================
--- velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java (added)
+++ velocity/tools/branches/2.0.x/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java Thu Jul 21 17:15:16 2011
@@ -0,0 +1,88 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.Locale;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+//import org.apache.velocity.tools.generic.DateTool;
+
+/**
+ * <p>Tests for DateToolTests</p>
+ *
+ * @author Christopher Schultz
+ * @since VelocityTools 2.0
+ * @version $Id$
+ */
+public class DateToolTests
+{
+ public @Test void toLocalizedPattern_simplePattern() throws Exception
+ {
+ DateTool dt = new DateTool();
+
+ Locale frFR = new Locale("fr", "FR");
+
+ assertEquals("DateTool incorrectly localizes date format pattern",
+ new SimpleDateFormat("yyyy-MM-dd", frFR).toLocalizedPattern(), // "aaaa-MM-jj"
+ dt.toLocalizedPattern("yyyy-MM-dd", frFR));
+ }
+
+ public @Test void toLocalizedPattern_predefinedDateTimePattern() throws Exception
+ {
+ DateTool dt = new DateTool();
+
+ Locale frFR = new Locale("fr", "FR");
+
+ assertEquals("DateTool incorrectly localizes date format pattern",
+ ((SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, frFR)).toLocalizedPattern(),
+ dt.toLocalizedPattern("short", frFR));
+ }
+
+ public @Test void toLocalizedPattern_predefinedDatePattern() throws Exception
+ {
+ DateTool dt = new DateTool();
+
+ Locale frFR = new Locale("fr", "FR");
+
+ assertEquals("DateTool incorrectly localizes date format pattern",
+ ((SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT, frFR)).toLocalizedPattern(),
+ dt.toLocalizedPattern("short_date", frFR));
+ }
+
+ public @Test void toLocalizedPattern_predefinedTimePattern() throws Exception
+ {
+ DateTool dt = new DateTool();
+
+ Locale frFR = new Locale("fr", "FR");
+
+ assertEquals("DateTool incorrectly localizes date format pattern",
+ ((SimpleDateFormat)DateFormat.getTimeInstance(DateFormat.SHORT, frFR)).toLocalizedPattern(),
+ dt.toLocalizedPattern("short_time", frFR));
+ }
+}