Skip to content

Commit b4e6133

Browse files
committed
Add fallback to FunctionValue that can be used by function implementations
1 parent b94b977 commit b4e6133

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

icu4c/source/i18n/messageformat2.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static UnicodeString functionFallback(const InternalValue& operand,
6969
fallbackToUse += var;
7070
}
7171
// If it exists, create a BaseValue (FunctionValue) for it
72-
LocalPointer<BaseValue> result(BaseValue::create(locale, *val, errorCode));
72+
LocalPointer<BaseValue> result(BaseValue::create(locale, fallbackToUse, *val, errorCode));
7373
// Add fallback and return an InternalValue
7474
if (U_SUCCESS(errorCode)) {
7575
return InternalValue(result.orphan(), fallbackToUse);
@@ -110,6 +110,7 @@ static UnicodeString reserialize(const UnicodeString& s) {
110110

111111
// Create a BaseValue (FunctionValue) that wraps the literal
112112
LocalPointer<BaseValue> val(BaseValue::create(locale,
113+
fallbackToUse,
113114
Formattable(lit.unquoted()),
114115
errorCode));
115116
if (U_SUCCESS(errorCode)) {

icu4c/source/i18n/messageformat2_evaluation.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ using namespace data_model;
2626
// BaseValue
2727
// ---------
2828

29-
BaseValue::BaseValue(const Locale& loc, const Formattable& source)
29+
BaseValue::BaseValue(const Locale& loc, const UnicodeString& fb, const Formattable& source)
3030
: locale(loc) {
3131
operand = source;
32+
fallback += LEFT_CURLY_BRACE;
33+
fallback += fb;
34+
fallback += RIGHT_CURLY_BRACE;
3235
}
3336

3437
/* static */ BaseValue* BaseValue::create(const Locale& locale,
38+
const UnicodeString& fallback,
3539
const Formattable& source,
3640
UErrorCode& errorCode) {
37-
return message2::create<BaseValue>(BaseValue(locale, source), errorCode);
41+
return message2::create<BaseValue>(BaseValue(locale, fallback, source), errorCode);
3842
}
3943

4044
extern UnicodeString formattableToString(const Locale&, const UBiDiDirection, const Formattable&, UErrorCode&);
@@ -50,6 +54,7 @@ BaseValue& BaseValue::operator=(BaseValue&& other) noexcept {
5054
operand = std::move(other.operand);
5155
opts = std::move(other.opts);
5256
locale = other.locale;
57+
fallback = other.fallback;
5358

5459
return *this;
5560
}

icu4c/source/i18n/messageformat2_evaluation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ namespace message2 {
120120
// in a context that expects a FunctionValue.
121121
class BaseValue : public FunctionValue {
122122
public:
123-
static BaseValue* create(const Locale&, const Formattable&, UErrorCode&);
123+
static BaseValue* create(const Locale&, const UnicodeString&, const Formattable&, UErrorCode&);
124124
// Apply default formatters to the argument value
125125
UnicodeString formatToString(UErrorCode&) const override;
126126
UBool isSelectable() const override { return true; }
@@ -130,7 +130,7 @@ namespace message2 {
130130
private:
131131
Locale locale;
132132

133-
BaseValue(const Locale&, const Formattable&);
133+
BaseValue(const Locale&, const UnicodeString&, const Formattable&);
134134
}; // class BaseValue
135135

136136
// A NullValue represents the absence of an argument.

icu4c/source/i18n/messageformat2_function_registry.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ StandardFunctions::TestFunctionValue::TestFunctionValue(const TestFunction& pare
14081408
// If FailsFormat is true, attempting to format the placeholder to any
14091409
// formatting target will fail.
14101410
if (failsFormat) {
1411-
status = U_MF_FORMATTING_ERROR;
1411+
formattedString = arg.getFallback();
14121412
return;
14131413
}
14141414

@@ -1451,6 +1451,8 @@ UnicodeString StandardFunctions::TestFunctionValue::formatToString(UErrorCode& s
14511451
}
14521452
if (!canFormat || failsFormat) {
14531453
status = U_MF_FORMATTING_ERROR;
1454+
}
1455+
if (!canFormat) {
14541456
return {};
14551457
}
14561458
return formattedString;

icu4c/source/i18n/unicode/messageformat2_function_registry.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,15 @@ namespace message2 {
438438
* @deprecated This API is for technology preview only.
439439
*/
440440
virtual const UnicodeString& getFunctionName() const { return functionName; }
441+
/* Returns a fallback string that can be used as output
442+
* if processing this function results in an error.
443+
* *
444+
* @returns A string determined by the creator of this FunctionValue.
445+
*
446+
* @internal ICU 78 technology preview
447+
* @deprecated This API is for technology preview only.
448+
*/
449+
virtual const UnicodeString& getFallback() const { return fallback; }
441450
/**
442451
* Destructor.
443452
*
@@ -462,12 +471,19 @@ namespace message2 {
462471
FunctionOptions opts;
463472
/**
464473
* The name of the function that constructed this FunctionValue.
474+
*
465475
* @internal ICU 78 technology preview
466476
* @deprecated This API is for technology preview only.
467477
*/
468478
UnicodeString functionName;
469-
private:
470-
479+
/**
480+
* Fallback string that can be used if a later function encounters
481+
* an error when processing this FunctionValue.
482+
*
483+
* @internal ICU 78 technology preview
484+
* @deprecated This API is for technology preview only.
485+
*/
486+
UnicodeString fallback;
471487
}; // class FunctionValue
472488

473489
} // namespace message2

0 commit comments

Comments
 (0)