added message formatting functions
This commit is contained in:
parent
7be7d6b0d9
commit
70968ffbcf
5 changed files with 135 additions and 6 deletions
|
@ -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 <stdexcept>
|
||||
|
||||
#include <unicode/resbund.h>
|
||||
#include <unicode/fmtable.h>
|
||||
|
||||
#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<UnicodeString>::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<UnicodeString>::Ref
|
||||
formatMessage(Ptr<const UnicodeString>::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<UnicodeString>::Ref
|
||||
formatMessage(const char * patternKey,
|
||||
Formattable * arguments,
|
||||
unsigned int nArguments)
|
||||
throw (std::invalid_argument);
|
||||
|
||||
};
|
||||
|
||||
/* ================================================= external data structures */
|
||||
|
|
|
@ -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 <unicode/msgfmt.h>
|
||||
|
||||
#include "LiveSupport/Core/LocalizedObject.h"
|
||||
|
||||
|
||||
|
@ -85,3 +87,36 @@ LocalizedObject :: getResourceString(const char * key)
|
|||
return unicodeStr;
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Format a message
|
||||
*----------------------------------------------------------------------------*/
|
||||
Ptr<UnicodeString>::Ref
|
||||
LocalizedObject :: formatMessage(Ptr<const UnicodeString>::Ref pattern,
|
||||
Formattable * arguments,
|
||||
unsigned int nArguments)
|
||||
throw (std::invalid_argument)
|
||||
{
|
||||
Ptr<UnicodeString>::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<UnicodeString>::Ref
|
||||
LocalizedObject :: formatMessage(const char * patternKey,
|
||||
Formattable * arguments,
|
||||
unsigned int nArguments)
|
||||
throw (std::invalid_argument)
|
||||
{
|
||||
return formatMessage(getResourceString(patternKey), arguments, nArguments);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<ResourceBundle>::Ref bundle(new ResourceBundle("./tmp/" PACKAGE_NAME,
|
||||
"root",
|
||||
status));
|
||||
CPPUNIT_ASSERT(U_SUCCESS(status));
|
||||
|
||||
try {
|
||||
Ptr<UnicodeString>::Ref message;
|
||||
Ptr<LocalizedObject>::Ref locObj(new LocalizedObject(bundle));
|
||||
Ptr<LocalizedObject>::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<UnicodeString>::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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
|
@ -5,5 +5,10 @@ root:table
|
|||
foo:string { "foo" }
|
||||
bar:string { "bar" }
|
||||
}
|
||||
|
||||
messages:table
|
||||
{
|
||||
aMessage:string { "parameter 0: {0}, parameter 1: {1}" }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue