Title: [131749] trunk/Source
Revision
131749
Author
[email protected]
Date
2012-10-18 07:32:08 -0700 (Thu, 18 Oct 2012)

Log Message

Add Localizer::monthFormat and implementations
https://bugs.webkit.org/show_bug.cgi?id=99704

Reviewed by Kentaro Hara.

Source/WebCore:

Localizer::monthFormat will be used for constructing input[type=month] UI.

Tests: Add unit tests to Source/WebKit/chromium/tests/.

* platform/text/Localizer.h:
(Localizer): Declare pure virtual monthFormat function.

* platform/text/LocaleNone.cpp:
(LocaleNone): Declare monthFormat.
(WebCore::LocaleNone::monthFormat):
Added. Always reutrns an ISO-8601 format, "yyyy-MM"

* platform/text/LocaleICU.h:
(LocaleICU): Declare monthFormat.
* platform/text/LocaleICU.cpp:
(WebCore::getFormatForSkeleton):
A helper to get a format for the specified skeleton.
The overflow-allocalte-try-again pattern is similar to
LocaleICU::decimalSymbol and LocaleICU::decimalTextAttribute.
(WebCore::LocaleICU::monthFormat):
Added. Calls getFormatForSkeleton with "yyyyMMM".

* platform/text/mac/LocaleMac.h:
(LocaleMac): Declare monthFormat.
* platform/text/mac/LocaleMac.mm:
(WebCore::LocaleMac::monthFormat):
Added. Calls NSDateFormatter::dateFormatFromTemplate with "yyyyMMM".

* platform/text/LocaleWin.h:
(LocaleWin): Declare monthFormat.
* platform/text/LocaleWin.cpp:
(WebCore::LocaleWin::monthFormat):
Get a format by LOCALE_SYEARMONTH, and convert it to an LDML format.

Source/WebKit/chromium:

* tests/LocaleMacTest.cpp:
(LocaleMacTest::monthFormat): A helper function.
(TEST_F): Added some tests.
* tests/LocaleWinTest.cpp:
(LocaleWinTest::monthFormat): A helper function.
(TEST_F): Added some tests.
* tests/LocalizedDateICUTest.cpp:
(LocalizedDateICUTest::monthFormat): A helper function.
(TEST_F): Added some tests.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (131748 => 131749)


--- trunk/Source/WebCore/ChangeLog	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/ChangeLog	2012-10-18 14:32:08 UTC (rev 131749)
@@ -1,3 +1,44 @@
+2012-10-18  Kent Tamura  <[email protected]>
+
+        Add Localizer::monthFormat and implementations
+        https://bugs.webkit.org/show_bug.cgi?id=99704
+
+        Reviewed by Kentaro Hara.
+
+        Localizer::monthFormat will be used for constructing input[type=month] UI.
+
+        Tests: Add unit tests to Source/WebKit/chromium/tests/.
+
+        * platform/text/Localizer.h:
+        (Localizer): Declare pure virtual monthFormat function.
+
+        * platform/text/LocaleNone.cpp:
+        (LocaleNone): Declare monthFormat.
+        (WebCore::LocaleNone::monthFormat):
+        Added. Always reutrns an ISO-8601 format, "yyyy-MM"
+
+        * platform/text/LocaleICU.h:
+        (LocaleICU): Declare monthFormat.
+        * platform/text/LocaleICU.cpp:
+        (WebCore::getFormatForSkeleton):
+        A helper to get a format for the specified skeleton.
+        The overflow-allocalte-try-again pattern is similar to
+        LocaleICU::decimalSymbol and LocaleICU::decimalTextAttribute.
+        (WebCore::LocaleICU::monthFormat):
+        Added. Calls getFormatForSkeleton with "yyyyMMM".
+
+        * platform/text/mac/LocaleMac.h:
+        (LocaleMac): Declare monthFormat.
+        * platform/text/mac/LocaleMac.mm:
+        (WebCore::LocaleMac::monthFormat):
+        Added. Calls NSDateFormatter::dateFormatFromTemplate with "yyyyMMM".
+
+        * platform/text/LocaleWin.h:
+        (LocaleWin): Declare monthFormat.
+        * platform/text/LocaleWin.cpp:
+        (WebCore::LocaleWin::monthFormat):
+        Get a format by LOCALE_SYEARMONTH, and convert it to an LDML format.
+
 2012-10-18  Pavel Feldman  <[email protected]>
 
         Web Inspector: add X-WebKit-CSP header into inspector.html

Modified: trunk/Source/WebCore/platform/text/LocaleICU.cpp (131748 => 131749)


--- trunk/Source/WebCore/platform/text/LocaleICU.cpp	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/LocaleICU.cpp	2012-10-18 14:32:08 UTC (rev 131749)
@@ -33,6 +33,7 @@
 
 #include "LocalizedStrings.h"
 #include <limits>
+#include <unicode/udatpg.h>
 #include <unicode/uloc.h>
 #include <wtf/DateMath.h>
 #include <wtf/PassOwnPtr.h>
@@ -398,6 +399,36 @@
     return m_dateFormat;
 }
 
+static String getFormatForSkeleton(const char* locale, const String& skeleton)
+{
+    String format = ASCIILiteral("yyyy-MM");
+    UErrorCode status = U_ZERO_ERROR;
+    UDateTimePatternGenerator* patternGenerator = udatpg_open(locale, &status);
+    if (!patternGenerator)
+        return format;
+    status = U_ZERO_ERROR;
+    int32_t length = udatpg_getBestPattern(patternGenerator, skeleton.characters(), skeleton.length(), 0, 0, &status);
+    if (status == U_BUFFER_OVERFLOW_ERROR && length) {
+        Vector<UChar> buffer(length);
+        status = U_ZERO_ERROR;
+        udatpg_getBestPattern(patternGenerator, skeleton.characters(), skeleton.length(), buffer.data(), length, &status);
+        if (U_SUCCESS(status))
+            format = String::adopt(buffer);
+    }
+    udatpg_close(patternGenerator);
+    return format;
+}
+
+String LocaleICU::monthFormat()
+{
+    if (!m_monthFormat.isNull())
+        return m_monthFormat;
+    // Gets a format for "MMM", not "MM" because Windows API always provides
+    // formats for "MMM".
+    m_monthFormat = getFormatForSkeleton(m_locale.data(), ASCIILiteral("yyyyMMM"));
+    return m_monthFormat;
+}
+
 String LocaleICU::timeFormat()
 {
     initializeDateTimeFormat();

Modified: trunk/Source/WebCore/platform/text/LocaleICU.h (131748 => 131749)


--- trunk/Source/WebCore/platform/text/LocaleICU.h	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/LocaleICU.h	2012-10-18 14:32:08 UTC (rev 131749)
@@ -62,6 +62,7 @@
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     virtual String dateFormat() OVERRIDE;
+    virtual String monthFormat() OVERRIDE;
     virtual String timeFormat() OVERRIDE;
     virtual String shortTimeFormat() OVERRIDE;
     virtual const Vector<String>& timeAMPMLabels() OVERRIDE;
@@ -107,6 +108,7 @@
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     String m_dateFormat;
+    String m_monthFormat;
     UDateFormat* m_mediumTimeFormat;
     UDateFormat* m_shortTimeFormat;
     Vector<String> m_timeAMPMLabels;

Modified: trunk/Source/WebCore/platform/text/LocaleNone.cpp (131748 => 131749)


--- trunk/Source/WebCore/platform/text/LocaleNone.cpp	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/LocaleNone.cpp	2012-10-18 14:32:08 UTC (rev 131749)
@@ -42,6 +42,7 @@
 #endif
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     virtual String dateFormat() OVERRIDE;
+    virtual String monthFormat() OVERRIDE;
 #endif
 };
 
@@ -80,6 +81,11 @@
 {
     return ASCIILiteral("dd/MM/yyyyy");
 }
+
+String LocaleNone::monthFormat()
+{
+    return ASCIILiteral("yyyy-MM");
+}
 #endif
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/platform/text/LocaleWin.cpp (131748 => 131749)


--- trunk/Source/WebCore/platform/text/LocaleWin.cpp	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/LocaleWin.cpp	2012-10-18 14:32:08 UTC (rev 131749)
@@ -713,6 +713,14 @@
     return convertWindowsDateFormatToLDML(parseDateFormat(windowsFormat));
 }
 
+String LocaleWin::monthFormat()
+{
+    if (!m_monthFormat.isNull())
+        return m_monthFormat;
+    m_monthFormat = convertWindowsDateFormatToLDML(parseDateFormat(getLocaleInfoString(LOCALE_SYEARMONTH)));
+    return m_monthFormat;
+}
+
 String LocaleWin::timeFormat()
 {
     if (m_localizedTimeFormatText.isEmpty())

Modified: trunk/Source/WebCore/platform/text/LocaleWin.h (131748 => 131749)


--- trunk/Source/WebCore/platform/text/LocaleWin.h	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/LocaleWin.h	2012-10-18 14:32:08 UTC (rev 131749)
@@ -57,6 +57,7 @@
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     virtual String dateFormat() OVERRIDE;
+    virtual String monthFormat() OVERRIDE;
     virtual String timeFormat() OVERRIDE;
     virtual String shortTimeFormat() OVERRIDE;
     virtual const Vector<String>& timeAMPMLabels() OVERRIDE;
@@ -98,6 +99,7 @@
     Vector<String> m_monthLabels;
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     String m_dateFormat;
+    String m_monthFormat;
 #endif
 #if ENABLE(CALENDAR_PICKER)
     Vector<String> m_weekDayShortLabels;

Modified: trunk/Source/WebCore/platform/text/Localizer.h (131748 => 131749)


--- trunk/Source/WebCore/platform/text/Localizer.h	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/Localizer.h	2012-10-18 14:32:08 UTC (rev 131749)
@@ -62,6 +62,9 @@
     // [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns
     virtual String dateFormat() = 0;
 
+    // Returns a year-month format in Unicode TR35 LDML.
+    virtual String monthFormat() = 0;
+
     // Returns time format in Unicode TR35 LDML[1] containing hour, minute, and
     // second with optional period(AM/PM), e.g. "h:mm:ss a"
     // [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns

Modified: trunk/Source/WebCore/platform/text/mac/LocaleMac.h (131748 => 131749)


--- trunk/Source/WebCore/platform/text/mac/LocaleMac.h	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/mac/LocaleMac.h	2012-10-18 14:32:08 UTC (rev 131749)
@@ -62,6 +62,7 @@
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     virtual String dateFormat() OVERRIDE;
+    virtual String monthFormat() OVERRIDE;
     virtual String timeFormat() OVERRIDE;
     virtual String shortTimeFormat() OVERRIDE;
     virtual const Vector<String>& timeAMPMLabels() OVERRIDE;
@@ -84,6 +85,7 @@
     RetainPtr<NSDateFormatter> shortTimeFormatter();
 
     String m_dateFormat;
+    String m_monthFormat;
     String m_localizedTimeFormatText;
     String m_localizedShortTimeFormatText;
     Vector<String> m_timeAMPMLabels;

Modified: trunk/Source/WebCore/platform/text/mac/LocaleMac.mm (131748 => 131749)


--- trunk/Source/WebCore/platform/text/mac/LocaleMac.mm	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebCore/platform/text/mac/LocaleMac.mm	2012-10-18 14:32:08 UTC (rev 131749)
@@ -246,6 +246,16 @@
     return m_dateFormat;
 }
 
+String LocaleMac::monthFormat()
+{
+    if (!m_monthFormat.isNull())
+        return m_monthFormat;
+    // Gets a format for "MMM", not "MM" because Windows API always provides
+    // formats for "MMM".
+    m_monthFormat = [NSDateFormatter dateFormatFromTemplate:@"yyyyMMM" options:0 locale:m_locale.get()];
+    return m_monthFormat;
+}
+
 String LocaleMac::timeFormat()
 {
     if (!m_localizedTimeFormatText.isNull())

Modified: trunk/Source/WebKit/chromium/ChangeLog (131748 => 131749)


--- trunk/Source/WebKit/chromium/ChangeLog	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebKit/chromium/ChangeLog	2012-10-18 14:32:08 UTC (rev 131749)
@@ -1,3 +1,20 @@
+2012-10-18  Kent Tamura  <[email protected]>
+
+        Add Localizer::monthFormat and implementations
+        https://bugs.webkit.org/show_bug.cgi?id=99704
+
+        Reviewed by Kentaro Hara.
+
+        * tests/LocaleMacTest.cpp:
+        (LocaleMacTest::monthFormat): A helper function.
+        (TEST_F): Added some tests.
+        * tests/LocaleWinTest.cpp:
+        (LocaleWinTest::monthFormat): A helper function.
+        (TEST_F): Added some tests.
+        * tests/LocalizedDateICUTest.cpp:
+        (LocalizedDateICUTest::monthFormat): A helper function.
+        (TEST_F): Added some tests.
+
 2012-10-17  Mike West  <[email protected]>
 
         Move mixed content logic out of FrameLoader

Modified: trunk/Source/WebKit/chromium/tests/LocaleMacTest.cpp (131748 => 131749)


--- trunk/Source/WebKit/chromium/tests/LocaleMacTest.cpp	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebKit/chromium/tests/LocaleMacTest.cpp	2012-10-18 14:32:08 UTC (rev 131749)
@@ -120,6 +120,12 @@
 #endif
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
+    String monthFormat(const String& localeString)
+    {
+        OwnPtr<LocaleMac> locale = LocaleMac::create(localeString);
+        return locale->monthFormat();
+    }
+
     String timeFormat(const String& localeString)
     {
         OwnPtr<LocaleMac> locale = LocaleMac::create(localeString);
@@ -236,6 +242,14 @@
 #endif
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
+TEST_F(LocaleMacTest, monthFormat)
+{
+    EXPECT_STREQ("MMM yyyy", monthFormat("en_US").utf8().data());
+    EXPECT_STREQ("MMM y", monthFormat("fr_FR").utf8().data());
+    EXPECT_STREQ("yyyy\xE5\xB9\xB4M\xE6\x9C\x88", monthFormat("ja_JP").utf8().data());
+    EXPECT_STREQ("LLL y", monthFormat("ru").utf8().data());
+}
+
 TEST_F(LocaleMacTest, timeFormat)
 {
     EXPECT_STREQ("h:mm:ss a", timeFormat("en_US").utf8().data());

Modified: trunk/Source/WebKit/chromium/tests/LocaleWinTest.cpp (131748 => 131749)


--- trunk/Source/WebKit/chromium/tests/LocaleWinTest.cpp	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebKit/chromium/tests/LocaleWinTest.cpp	2012-10-18 14:32:08 UTC (rev 131749)
@@ -129,6 +129,12 @@
 #endif
 
 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
+    String monthFormat(LCID lcid)
+    {
+        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
+        return locale->monthFormat();
+    }
+
     String timeFormat(LCID lcid)
     {
         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
@@ -284,6 +290,13 @@
     EXPECT_STREQ("yyyy'-'''''MMMM'-'dd", LocaleWin::dateFormat("yyyy-''''MMMM-dd").utf8().data());
 }
 
+TEST_F(LocaleWinTest, monthFormat)
+{
+    EXPECT_STREQ("MMMM', 'yyyy", monthFormat(EnglishUS).utf8().data());
+    EXPECT_STREQ("MMMM' 'yyyy", monthFormat(FrenchFR).utf8().data());
+    EXPECT_STREQ("yyyy'\xE5\xB9\xB4'M'\xE6\x9C\x88'", monthFormat(JapaneseJP).utf8().data());
+}
+
 TEST_F(LocaleWinTest, timeFormat)
 {
     EXPECT_STREQ("h:mm:ss a", timeFormat(EnglishUS).utf8().data());

Modified: trunk/Source/WebKit/chromium/tests/LocalizedDateICUTest.cpp (131748 => 131749)


--- trunk/Source/WebKit/chromium/tests/LocalizedDateICUTest.cpp	2012-10-18 14:18:58 UTC (rev 131748)
+++ trunk/Source/WebKit/chromium/tests/LocalizedDateICUTest.cpp	2012-10-18 14:32:08 UTC (rev 131749)
@@ -88,6 +88,12 @@
         return Labels(labels);
     }
 
+    String monthFormat(const char* localeString)
+    {
+        OwnPtr<LocaleICU> locale = LocaleICU::create(localeString);
+        return locale->monthFormat();
+    }
+
     String localizedDateFormatText(const char* localeString)
     {
         OwnPtr<LocaleICU> locale = LocaleICU::create(localeString);
@@ -126,6 +132,13 @@
     EXPECT_FALSE(isRTL("**invalid**"));
 }
 
+TEST_F(LocalizedDateICUTest, monthFormat)
+{
+    EXPECT_STREQ("MMM yyyy", monthFormat("en_US").utf8().data());
+    EXPECT_STREQ("MMM yyyy", monthFormat("fr").utf8().data());
+    EXPECT_STREQ("yyyy\xE5\xB9\xB4M\xE6\x9C\x88", monthFormat("ja").utf8().data());
+}
+
 TEST_F(LocalizedDateICUTest, localizedDateFormatText)
 {
     // Note: EXPECT_EQ(String, String) doesn't print result as string.
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to