-
-
Notifications
You must be signed in to change notification settings - Fork 900
🚧 ICU-23424 Initial implementation of Builder::setFormattingLocale() #4021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,18 @@ namespace message2 { | |
| */ | ||
| U_I18N_API const Locale& getLocale() const { return locale; } | ||
|
|
||
| /** | ||
| * Acceses the formatting locale that this `MessageFormatter` object | ||
| * was created with. If no formatting locale was explicitly set, | ||
| * returns the same locale as getLocale(). | ||
| * | ||
| * @return A reference to the formatting locale. | ||
| * | ||
| * @internal ICU 79 technology preview | ||
| * @deprecated This API is for technology preview only. | ||
| */ | ||
| U_I18N_API const Locale& getFormattingLocale() { return formattingLocale; } | ||
|
|
||
| /** | ||
| * Serializes the data model as a string in MessageFormat 2.0 syntax. | ||
| * | ||
|
|
@@ -230,6 +242,7 @@ namespace message2 { | |
| // Ignored if `setPattern()` wasn't called | ||
| StaticErrors* errors; | ||
| Locale locale; | ||
| Locale formattingLocale; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation |
||
| // Not owned | ||
| const MFFunctionRegistry* customMFFunctionRegistry; | ||
| // Error behavior; see comment in `MessageFormatter` class | ||
|
|
@@ -244,7 +257,7 @@ namespace message2 { | |
| void clearState(); | ||
| public: | ||
| /** | ||
| * Sets the locale to use for formatting. | ||
| * Sets the locale to use for this context | ||
| * | ||
| * @param locale The desired locale. | ||
| * @return A reference to the builder. | ||
|
|
@@ -253,6 +266,18 @@ namespace message2 { | |
| * @deprecated This API is for technology preview only. | ||
| */ | ||
| U_I18N_API Builder& setLocale(const Locale& locale); | ||
| /** | ||
| * Sets the locale used by formatter factories (:number, :date, | ||
| * etc.) for rendering output. Selector factories (plural rules) | ||
| * continue to use the locale set via setLocale(). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of #3536 there are no factories anymore and formatters and selectors aren't distinguished, so I think this comment needs updating? Perhaps it just applies to built-in functions?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you're right, this was from pre-3536 |
||
| * The formatting locale defaults to the same one set by setLocale(). | ||
| * @param locale The desired locale | ||
| * @return A reference to the builder. | ||
| * | ||
| * @internal ICU 79 technology preview | ||
| * @deprecated This API is for technology preview only. | ||
| */ | ||
| U_I18N_API Builder& setFormattingLocale(const Locale& locale); | ||
| /** | ||
| * Sets the pattern (contents of the message) and parses it | ||
| * into a data model. If a data model was | ||
|
|
@@ -501,8 +526,10 @@ namespace message2 { | |
| void clearErrors() const; | ||
| void cleanup() noexcept; | ||
|
|
||
| // The locale this MessageFormatter was created with | ||
| /* const */ Locale locale; | ||
| // The locale set by setLocale() | ||
| Locale locale; | ||
| // The locale set by setformattinglocale() | ||
| Locale formattingLocale; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation |
||
|
|
||
| // Registry for built-in functions | ||
| MFFunctionRegistry standardMFFunctionRegistry; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ TestMessageFormat2::runIndexedTest(int32_t index, UBool exec, | |
| TESTCASE_AUTO(testLowLoneSurrogate); | ||
| TESTCASE_AUTO(testLoneSurrogateInQuotedLiteral); | ||
| TESTCASE_AUTO(dataDrivenTests); | ||
| TESTCASE_AUTO(testSetFormatLocale); | ||
| TESTCASE_AUTO_END; | ||
| } | ||
|
|
||
|
|
@@ -479,6 +480,43 @@ void TestMessageFormat2::dataDrivenTests() { | |
| jsonTestsFromFiles(errorCode); | ||
| } | ||
|
|
||
| void TestMessageFormat2::testSetFormatLocale() { | ||
| IcuTestErrorCode errorCode(*this, "testSetFormatLocale"); | ||
| UParseError parseError; | ||
|
|
||
| UnicodeString pattern(u"" | ||
| ".input {$count :number} .input {$name :string} .input {$birthday :date}\n" | ||
| ".match $count\n" | ||
| "many {{PASS {$count} @ {$birthday :date style=short}}}\n" | ||
| "* {{FAIL wrong bucket {$count} @ {$birthday :date style=short}}}\n" | ||
| ); | ||
| MessageFormatter mf = MessageFormatter::Builder(errorCode) | ||
| .setErrorHandlingBehavior(MessageFormatter::U_MF_BEST_EFFORT) | ||
| .setPattern(pattern, parseError, errorCode) | ||
| .setLocale(Locale("pl")) | ||
| .setFormattingLocale(Locale("en")) | ||
| .build(errorCode); | ||
|
|
||
| if (errorCode.errIfFailureAndReset("build")) { | ||
| errln("UParseError = %d:%d", parseError.line, parseError.offset); | ||
| return; | ||
| } | ||
|
|
||
| std::map<UnicodeString, message2::Formattable> argsBuilder; | ||
| argsBuilder["count"] = message2::Formattable((int64_t)13); | ||
| argsBuilder["name"] = message2::Formattable("US"); | ||
| argsBuilder["birthday"] = message2::Formattable("1776-07-04"); | ||
| MessageArguments args(argsBuilder, errorCode); | ||
|
|
||
| UnicodeString result = mf.formatToString(args, errorCode); | ||
| if (errorCode.errIfFailureAndReset("formatToString")) { | ||
| return; | ||
| } | ||
| UnicodeString expectedResult(u"PASS 13 @ 7/4/76"); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test will FAIL on the existing code if set to the "en" locale (because it won't get the Polish plural buckets). And the date format will be wrong (will be in Polish) if the formatting locale wasn't set to "en". |
||
| assertEquals("testSetFormatLocale", expectedResult, result); | ||
| } | ||
|
|
||
|
|
||
| TestCase::~TestCase() {} | ||
| TestCase::Builder::~Builder() {} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it confusing to refer to the two locales as "locale" and "formatting locale" -- perhaps rename "locale" to indicate what it does in contrast with the formatting locale?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Will need to address Mihai's comment also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps
selectionLocaleandformattingLocale?