|
| 1 | +# Expression Attributes |
| 2 | + |
| 3 | +<details> |
| 4 | + <summary>Metadata</summary> |
| 5 | + <dl> |
| 6 | + <dt>Contributors</dt> |
| 7 | + <dd>@eemeli</dd> |
| 8 | + <dt>First proposed</dt> |
| 9 | + <dd>2023-08-27</dd> |
| 10 | + <dt>Pull Request</dt> |
| 11 | + <dd><a href="https://github.com/unicode-org/message-format-wg/pull/458">#458</a></dd> |
| 12 | + </dl> |
| 13 | +</details> |
| 14 | + |
| 15 | +## Objective |
| 16 | + |
| 17 | +Define how attributes may be attached to expressions. |
| 18 | + |
| 19 | +## Background |
| 20 | + |
| 21 | +Function options may influence the resolution, selection, and formatting of annotated expressions. |
| 22 | +These provide a great solution for options like `minFractionDigits`, `dateStyle`, |
| 23 | +or other similar factors that influence the formatted result. |
| 24 | + |
| 25 | +However, this single bag of options is not appropriate in all cases, |
| 26 | +in particular for attributes that pertain to the expression as a selector or a placeholder. |
| 27 | +For example, many of the [XLIFF 2 inline element] attributes don't really make sense as function options. |
| 28 | + |
| 29 | +[XLIFF 2 inline element]: http://docs.oasis-open.org/xliff/xliff-core/v2.1/os/xliff-core-v2.1-os.html#inlineelements |
| 30 | + |
| 31 | +## Use-Cases |
| 32 | + |
| 33 | +At least the following expression attributes should be considered: |
| 34 | + |
| 35 | +- Attributes with a formatting runtime impact: |
| 36 | + |
| 37 | + - `fallback` — A value to use instead of the default fallback, |
| 38 | + should the expression's primary formatting fail in some way. |
| 39 | + - `locale` — An override for the locale used to format the expression. |
| 40 | + Should be expressed as a non-empty sequence of BCP 47 language codes. |
| 41 | + - `dir` — An override for the LTR/RTL/auto directionality of the expression. |
| 42 | + |
| 43 | +- Attributes relevant for translators, tools, and other message operations, |
| 44 | + but with no runtime impact: |
| 45 | + |
| 46 | + - `example` — A literal value representing |
| 47 | + what the expression's formatted value will look like. |
| 48 | + - `note` — A comment on the expression for translators. |
| 49 | + - `translate` — A boolean `yes`/`no` indicator communicating to translators |
| 50 | + whether the expression should or should not be localised. |
| 51 | + - `canCopy`, `canDelete`, `canOverlap`, `canReorder`, etc. — Flags supported by |
| 52 | + XLIFF 2 inline elements |
| 53 | + |
| 54 | +## Requirements |
| 55 | + |
| 56 | +Attributes can be assigned to any expression, |
| 57 | +including expressions without an annotation. |
| 58 | + |
| 59 | +Attributes are distinct from function options. |
| 60 | + |
| 61 | +Common attributes are defined by the MF2 specification |
| 62 | +and must be supported by all implementations. |
| 63 | + |
| 64 | +Users may define their own attributes. |
| 65 | + |
| 66 | +Implementations may define their own attributes. |
| 67 | + |
| 68 | +Some attributes may have an effect on the formatting of an expression. |
| 69 | +These cannot be defined within comments either within or outside a message. |
| 70 | + |
| 71 | +Each attribute relates to a specific expression. |
| 72 | + |
| 73 | +An attribute's scope is limited to the expression to which it relates. |
| 74 | + |
| 75 | +Multiple attributes should be assignable to a single expression. |
| 76 | + |
| 77 | +Attributes should be assignable to all expressions, not just placeholders. |
| 78 | + |
| 79 | +## Constraints |
| 80 | + |
| 81 | +If supported by new syntax, |
| 82 | +the syntax should be easy to parse by both humans and machines. |
| 83 | + |
| 84 | +If supported by new syntax at the end of an expression, |
| 85 | +the reserved/private-use rules will need to be adjusted to support attributes. |
| 86 | + |
| 87 | +## Proposed Design |
| 88 | + |
| 89 | +Add support for option-like `@key=value` attribute pairs at the end of any expression. |
| 90 | + |
| 91 | +If the syntax for function options is extended to support flag-like options |
| 92 | +(see <a href="https://github.com/unicode-org/message-format-wg/issues/386">#386</a>), |
| 93 | +also extend expression attribute syntax to match. |
| 94 | + |
| 95 | +To distinguish expression attributes from options, |
| 96 | +require `@` as a prefix for each attribute asignment. |
| 97 | +Examples: `@translate=yes` and `@locale=$exprLocale`. |
| 98 | + |
| 99 | +Define the meaning and supported values of some expression attributes in the specification, |
| 100 | +including at least `@dir` and `@locale`. |
| 101 | + |
| 102 | +To support later extension of the specified set of attributes while allowing user extensibility, |
| 103 | +require custom attribute names to include a U+002D Hyphen-Minus `-`. |
| 104 | +Examples: `@can-copy=no`, `@note-link=|https://...|`. |
| 105 | + |
| 106 | +Allow expression attributes to influence the formatting context, |
| 107 | +but do not directly pass them to user-defined functions. |
| 108 | + |
| 109 | +## Alternatives Considered |
| 110 | + |
| 111 | +### Do not support expression attributes |
| 112 | + |
| 113 | +If not explicitly defined, less information will be provided to translators. |
| 114 | + |
| 115 | +Function options may be used as a workaround, |
| 116 | +but each implementation and user will end up with different practices. |
| 117 | + |
| 118 | +### Use function options, but with some suggested prefix like `_` |
| 119 | + |
| 120 | +A bit less bad than the previous, but still mixes attributes and options into the same namespace. |
| 121 | + |
| 122 | +At least a no-op function is required for otherwise unannotated expressions. |
| 123 | + |
| 124 | +### Rely on semantic comments |
| 125 | + |
| 126 | +These will be defined within the message resource spec, |
| 127 | +so we introduce a dependence on that. |
| 128 | + |
| 129 | +Referring to specific expressions from outside the message is hard, |
| 130 | +esp. if a similar expression is used in multiple variants. |
| 131 | + |
| 132 | +Comments should not influence the runtime behaviour of a formatter. |
| 133 | + |
| 134 | +### Define `@attributes` as above, but do not namespace custom attributes |
| 135 | + |
| 136 | +Later spec versions will not be able to define _any_ new attributes |
| 137 | +without a danger of breaking implementations or users already using those names. |
0 commit comments