Skip to content

Commit

Permalink
ICU-22921 Add howto guide to try MF 2.0 final candidate draft impls
Browse files Browse the repository at this point in the history
See #3334
  • Loading branch information
echeran authored and macchiati committed Jan 15, 2025
1 parent 2d6a418 commit 2c5e021
Show file tree
Hide file tree
Showing 3 changed files with 403 additions and 0 deletions.
Binary file added docs/userguide/format_parse/messages/HelloMF2.zip
Binary file not shown.
37 changes: 37 additions & 0 deletions docs/userguide/format_parse/messages/hello_mf2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// © 2025 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#include <iostream>

#include "unicode/utypes.h"
#include "unicode/calendar.h"
#include "unicode/errorcode.h"
#include "unicode/locid.h"
#include "unicode/messageformat2.h"

using namespace icu;

int main() {
ErrorCode errorCode;
UParseError parseError;

icu::Calendar* cal(Calendar::createInstance(errorCode));
cal->set(2025, Calendar::JANUARY, 28);
UDate date = cal->getTime(errorCode);

message2::MessageFormatter::Builder builder(errorCode);
message2::MessageFormatter mf = builder
.setPattern("Hello {$user}, today is {$now :date style=long}!", parseError, errorCode)
.setLocale(Locale("en_US"))
.build(errorCode);

std::map<UnicodeString, message2::Formattable> argsBuilder;
argsBuilder["user"] = message2::Formattable("John");
argsBuilder["now"] = message2::Formattable::forDate(date);
message2::MessageArguments arguments(argsBuilder, errorCode);

icu::UnicodeString result = mf.formatToString(arguments, errorCode);
std::string strResult;
result.toUTF8String(strResult);
std::cout << strResult << std::endl;
}
Loading

0 comments on commit 2c5e021

Please sign in to comment.