diff --git a/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h b/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h index f1d491690..3600b97b0 100644 --- a/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h +++ b/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.2 $ + Version : $Revision: 1.3 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h,v $ ------------------------------------------------------------------------------*/ @@ -43,6 +43,7 @@ #include #include +#include #include "LiveSupport/Core/Ptr.h" @@ -62,7 +63,7 @@ namespace Core { * to make localized life easier. * * @author $Author: maroy $ - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ */ class LocalizedObject { @@ -133,6 +134,47 @@ class LocalizedObject virtual Ptr::Ref getResourceString(const char * key) throw (std::invalid_argument); + + /** + * A convenience function to format a message. + * For more information, see the ICU MessageFormat class + * documentation. + * + * @param pattern the pattern to format + * @param arguments the arguments to use in the formatting + * @param nArguments the number of arguments supplied + * @return the formatted string + * @exception std::invalid_argument if the pattern is bad, or + * the arguments do not match + * @see http://oss.software.ibm.com/icu/apiref/classMessageFormat.html + */ + static Ptr::Ref + formatMessage(Ptr::Ref pattern, + Formattable * arguments, + unsigned int nArguments) + throw (std::invalid_argument); + + /** + * A convenience function to format a message, based on a pattern + * loaded from a resource. + * For more information, see the ICU MessageFormat class + * documentation. + * + * @param patternKey the key of the pattern to format + * @param arguments the arguments to use in the formatting + * @param nArguments the number of arguments supplied + * @return the formatted string + * @exception std::invalid_argument if the pattern is bad, or + * the arguments do not match, or there is no resource + * specified by patternKey + * @see http://oss.software.ibm.com/icu/apiref/classMessageFormat.html + */ + virtual Ptr::Ref + formatMessage(const char * patternKey, + Formattable * arguments, + unsigned int nArguments) + throw (std::invalid_argument); + }; /* ================================================= external data structures */ diff --git a/livesupport/modules/core/src/LocalizedObject.cxx b/livesupport/modules/core/src/LocalizedObject.cxx index acf93fc2c..b97c1d9bb 100644 --- a/livesupport/modules/core/src/LocalizedObject.cxx +++ b/livesupport/modules/core/src/LocalizedObject.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.2 $ + Version : $Revision: 1.3 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/LocalizedObject.cxx,v $ ------------------------------------------------------------------------------*/ @@ -33,6 +33,8 @@ #include "configure.h" #endif +#include + #include "LiveSupport/Core/LocalizedObject.h" @@ -85,3 +87,36 @@ LocalizedObject :: getResourceString(const char * key) return unicodeStr; } + +/*------------------------------------------------------------------------------ + * Format a message + *----------------------------------------------------------------------------*/ +Ptr::Ref +LocalizedObject :: formatMessage(Ptr::Ref pattern, + Formattable * arguments, + unsigned int nArguments) + throw (std::invalid_argument) +{ + Ptr::Ref message(new UnicodeString()); + UErrorCode err = U_ZERO_ERROR; + MessageFormat::format(*pattern, arguments, nArguments, *message, err); + if (!U_SUCCESS(err)) { + throw std::invalid_argument("can't format string"); + } + + return message; +} + + +/*------------------------------------------------------------------------------ + * Format a message, based on a resource key for its pattern + *----------------------------------------------------------------------------*/ +Ptr::Ref +LocalizedObject :: formatMessage(const char * patternKey, + Formattable * arguments, + unsigned int nArguments) + throw (std::invalid_argument) +{ + return formatMessage(getResourceString(patternKey), arguments, nArguments); +} + diff --git a/livesupport/modules/core/src/LocalizedObjectTest.cxx b/livesupport/modules/core/src/LocalizedObjectTest.cxx index f60677ce6..3d8516974 100644 --- a/livesupport/modules/core/src/LocalizedObjectTest.cxx +++ b/livesupport/modules/core/src/LocalizedObjectTest.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/LocalizedObjectTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -187,3 +187,41 @@ LocalizedObjectTest :: unicodeTest(void) } } + +/*------------------------------------------------------------------------------ + * Test message formatting. + *----------------------------------------------------------------------------*/ +void +LocalizedObjectTest :: formatMessageTest(void) + throw (CPPUNIT_NS::Exception) +{ + UErrorCode status = U_ZERO_ERROR; + Ptr::Ref bundle(new ResourceBundle("./tmp/" PACKAGE_NAME, + "root", + status)); + CPPUNIT_ASSERT(U_SUCCESS(status)); + + try { + Ptr::Ref message; + Ptr::Ref locObj(new LocalizedObject(bundle)); + Ptr::Ref messages(new LocalizedObject( + locObj->getBundle("messages"))); + Formattable arguments[] = { "p1", "p2" }; + + // test formatting through a key + message = messages->formatMessage("aMessage", arguments, 2); + CPPUNIT_ASSERT( + message->compare("parameter 0: p1, parameter 1: p2" == 0)); + + // test formatting through an explicit pattern + Ptr::Ref pattern(new UnicodeString( + "only 1 parameter: {0}")); + message = LocalizedObject::formatMessage(pattern, arguments, 1); + CPPUNIT_ASSERT(message->compare("only 1 parameter: p1") == 0); + + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(e.what()); + } +} + + diff --git a/livesupport/modules/core/src/LocalizedObjectTest.h b/livesupport/modules/core/src/LocalizedObjectTest.h index f78fa3d7f..868a2dce9 100644 --- a/livesupport/modules/core/src/LocalizedObjectTest.h +++ b/livesupport/modules/core/src/LocalizedObjectTest.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/LocalizedObjectTest.h,v $ ------------------------------------------------------------------------------*/ @@ -58,7 +58,7 @@ namespace Core { * Unit test for the LocalizedObject class. * * @author $Author: maroy $ - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * @see LocalizedObject */ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture @@ -67,6 +67,7 @@ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST(simpleTest); CPPUNIT_TEST(fallbackTest); CPPUNIT_TEST(unicodeTest); + CPPUNIT_TEST(formatMessageTest); CPPUNIT_TEST_SUITE_END(); protected: @@ -96,6 +97,14 @@ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture void unicodeTest(void) throw (CPPUNIT_NS::Exception); + /** + * A test to see if message formatting works all right. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + formatMessageTest(void) throw (CPPUNIT_NS::Exception); + public: diff --git a/livesupport/modules/core/var/root.txt b/livesupport/modules/core/var/root.txt index d15b58d03..19ccb4d89 100644 --- a/livesupport/modules/core/var/root.txt +++ b/livesupport/modules/core/var/root.txt @@ -5,5 +5,10 @@ root:table foo:string { "foo" } bar:string { "bar" } } + + messages:table + { + aMessage:string { "parameter 0: {0}, parameter 1: {1}" } + } }