# MessageFormatter

> MessageFormatter allows formatting messages via [ICU message format](https://unicode-org.github.io/icu/userguide/format_parse/messages/).

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [i18n](/docs/api/yii/i18n/index.md) › MessageFormatter

| class | `yii\i18n\MessageFormatter` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/i18n/MessageFormatter.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/i18n/MessageFormatter.php#L47) |

MessageFormatter allows formatting messages via [ICU message format](https://unicode-org.github.io/icu/userguide/format_parse/messages/).

This class enhances the message formatter class provided by the PHP intl extension.

The following enhancements are provided:

- It accepts named arguments and mixed numeric and named arguments.
- Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be
  substituted.
- Fixes PHP 5.5 weird placeholder replacement in case no arguments are provided at all (https://bugs.php.net/bug.php?id=65920).
- Offers limited support for message formatting in case PHP intl extension is not installed.
  However it is highly recommended that you install [PHP intl extension](https://www.php.net/manual/en/book.intl.php) if you want
  to use MessageFormatter features.

  The fallback implementation only supports the following message formats:
  - plural formatting for english ('one' and 'other' selectors)
  - select format
  - simple parameters
  - integer number parameters

  The fallback implementation does NOT support the ['apostrophe-friendly' syntax](https://www.php.net/manual/en/messageformatter.formatmessage.php).
  Also messages that are working with the fallback implementation are not necessarily compatible with the
  PHP intl MessageFormatter so do not rely on the fallback if you are able to install intl extension somehow.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-errorCode"></span>`$errorCode` read-only | `string` | Code of the last error. | `MessageFormatter` |
| <span id="property-errorMessage"></span>`$errorMessage` read-only | `string` | Description of the last error. | `MessageFormatter` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`format()`](#method-format) | Formats a message via [ICU message format](https://unicode-org.github.io/icu/userguide/format_parse/messages/). | `MessageFormatter` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getErrorCode()`](#method-getErrorCode) | Get the error code from the last operation. | `MessageFormatter` |
| [`getErrorMessage()`](#method-getErrorMessage) | Get the error text from the last operation. | `MessageFormatter` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`parse()`](#method-parse) | Parses an input string according to an [ICU message format](https://unicode-org.github.io/icu/userguide/format_parse/messages/) pattern. | `MessageFormatter` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`fallbackFormat()`](#method-fallbackFormat) protected | Fallback implementation for MessageFormatter::formatMessage. | `MessageFormatter` |

## Method details

### format() <span id="method-format"></span>

```php
public function format(string $pattern, array $params, string $language): string|false
```

Formats a message via [ICU message format](https://unicode-org.github.io/icu/userguide/format_parse/messages/).

It uses the PHP intl extension's [MessageFormatter](https://www.php.net/manual/en/class.messageformatter.php)
and works around some issues.
If PHP intl is not installed a fallback will be used that supports a subset of the ICU message format.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$pattern` | `string` | The pattern string to insert parameters into. |
| `$params` | `array` | The array of name value pairs to insert into the format string. |
| `$language` | `string` | The locale to use for formatting locale-dependent parts |

**Returns**

`string|false`: The formatted pattern string or `false` if an error occurred

### getErrorCode() <span id="method-getErrorCode"></span>

```php
public function getErrorCode(): string
```

Get the error code from the last operation.

**Returns**

`string`: Code of the last error.

### getErrorMessage() <span id="method-getErrorMessage"></span>

```php
public function getErrorMessage(): string
```

Get the error text from the last operation.

**Returns**

`string`: Description of the last error.

### parse() <span id="method-parse"></span>

```php
public function parse(string $pattern, string $message, string $language): array|bool
```

Parses an input string according to an [ICU message format](https://unicode-org.github.io/icu/userguide/format_parse/messages/) pattern.

It uses the PHP intl extension's [MessageFormatter::parse()](https://www.php.net/manual/en/messageformatter.parsemessage.php)
and adds support for named arguments.
Usage of this method requires PHP intl extension to be installed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$pattern` | `string` | The pattern to use for parsing the message. |
| `$message` | `string` | The message to parse, conforming to the pattern. |
| `$language` | `string` | The locale to use for formatting locale-dependent parts |

**Returns**

`array|bool`: An array containing items extracted, or `FALSE` on error.

**Throws**

- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md): when PHP intl extension is not installed.

### fallbackFormat() <span id="method-fallbackFormat"></span>

```php
protected function fallbackFormat(string $pattern, array $args, string $locale): false|string
```

Fallback implementation for MessageFormatter::formatMessage.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$pattern` | `string` | The pattern string to insert things into. |
| `$args` | `array` | The array of values to insert into the format string |
| `$locale` | `string` | The locale to use for formatting locale-dependent parts |

**Returns**

`false|string`: The formatted pattern string or `false` if an error occurred