Skip to content

References to runtime values and attributes #209

Closed
@eemeli

Description

@eemeli

Is your feature request related to a problem? Please describe.
With select messages, the case often arises that it's desirable for the selector value (in particular a number) to also be used within a select message, or for the selection to be made based on some attribute of a value that would then be used within the formatted message.

In MF1, the first of these is enabled by using # within a plural or selectordinal. In #52 (comment), @zbraniecki gives an example of the latter. This is also raised in #31 by @grhoten.

Describe the solution you'd like
We should determine how we can avoid error-prone duplication in MF2, so that e.g. numbers in select messages get formatted with the same options with which their cases were selected, and custom functions do not need to be called multiple times with the same arguments.

One option here is to e.g. include something like # in the MF2 data model, and to ensure that custom functions are pure, so that their results may be memoised for later calls with the same parameters.

Another option would be to introduce something like message-specific "macros" or "phrases" that have been suggested by @mihnita and @stasm in conversation elsewhere. The idea is to provide essentially a set of local variables that a message may use in its contents, with their values determined at runtime. This would be the solution that I'd prefer.

Describe why your solution should shape the standard
If such local variables are made available, they'll need a way to be defined in the syntax, as well as have a place in the data model. Using a made-up pseudosyntax, it might look something like this:

msg = [ $bar = number($foo, minimumSignificantDigits: 3) ] $bar ->
  [one] {$bar} thing
  [other] {$bar} things
{
  type: 'select',
  vars: {
    bar: {
      value: {
        type: 'function',
        func: 'number',
        args: [
          { type: 'variable', var_path: [{ type: 'literal', value: 'foo' }] }
        ],
        options: { minimumSignificantDigits: { type: 'literal', value: '3' } }
      }
    }
  },
  select: [
    { value: { type: 'variable', var_path: [{ type: 'literal', value: 'bar' }] } }
  ],
  cases: [
    {
      key: ['one'],
      value: [
        { type: 'variable', var_path: [{ type: 'literal', value: 'bar' }] },
        { type: 'literal', value: ' thing' }
      ]
    },
    {
      key: ['other'],
      value: [
        { type: 'variable', var_path: [{ type: 'literal', value: 'bar' }] },
        { type: 'literal', value: ' things' }
      ]
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleObsolete?blocker-candidateThe submitter thinks this might be a block for the next releasedata modelIssues related to the Interchange Data ModelfunctionsIssue pertains to the default function setresolve-candidateThis issue appears to have been answered or resolved, and may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions