Modules: app/util/locale/messageFormatUtil

Utilities to get messages from a messageFormat bundle

Source:

Methods


<static> getMessage(definitions, id, params)

Looks up a message in the given MessageFormat messages bundle

Parameters:
Name Type Description
definitions

A message file generated by calling (new MessageFormat()).compile()

id

The id of the message to get

params

Parameters for formatting this message, if any

Source:
Returns:

The formatted message

Type
string

<static> hasMessage(definitions, id)

Checks if a message in the given MessageFormat messages bundle exists

Parameters:
Name Type Description
definitions

A message file generated by calling (new MessageFormat()).compile()

id

The id of the message to get

Source:
Returns:

true when the message exists

Type
boolean

<static> postProcessMessage(message, customValueMap)

Post-processes a message after being pre-processed and retrieved from the getMessage function, so that custom variable replacement can be applied. It will replace any of the variable placeholders left in tact because of the pre-processing work.

If a param value is an object (most likely JSX) it will inlined there. If the param value is a function, it will be called. When the special {link}foo{_link} syntax is used in the message string, the contents between the variable placeholders (foo in this case) will be passed as argument to that function. You can use the pipe | symbol to pass multiple arguments to the function: {link}url|text{_link}.

Since this will probably only used for JSX, in this cases the return value will be a JSX span instead of a string. If you are just returning a string from your custom values, you should instead use a messageFormat extension.

Parameters:
Name Type Description
message string

The response of the messageFormat getMessage call

customValueMap object

A map of custom param values, result from the pre-processing step

Source:
Returns:

Either the original string of no custom values are passed, otherwise a JSX object

Type
*

<static> preProcessMessage(params)

Pre-processes the params for the message, so that any custom (non-primitive) values passed will be stored to be used later. The param will be replaced by the param syntax itself, so when getting the message, they remain as variables in the output string. This is required for the post-processing step where the stored custom values will be replaced.

Parameters:
Name Type Description
params Object

The params passed to the getMessage function, a modified copy of this will be returned.

Source:
Returns:

An object with multiple return values. The updatedParams is a cloned modification of the original that must be passed to the getMessage function. The others will need to be passed to the postProcessMessage function.

Type
Object
Example
const {
    updatedParams,
    ...preProcessValues
  } = preProcessMessage(params);

  message = getMessage(this.messages, id, updatedParams);
  return postProcessMessage(message, preProcessValues);