# GettextMoFile

> GettextMoFile represents an MO Gettext message file.

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

| class | `yii\i18n\GettextMoFile` |
| --- | --- |
| Extends | [`GettextFile`](/docs/api/yii/i18n/gettextfile/index.md) » [`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/GettextMoFile.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/i18n/GettextMoFile.php#L45) |

GettextMoFile represents an MO Gettext message file.

This class is written by adapting Michael's Gettext_MO class in PEAR.
Please refer to the following license terms.

Copyright (c) 2004-2005, Michael Wallner &lt;mike@iworks.at>.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

## 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-useBigEndian"></span>`$useBigEndian` | `bool` | Whether to use big-endian when reading and writing an integer. Default: `false`. | `GettextMoFile` |

## 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) |
| <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) |
| <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) |
| [`load()`](#method-load) | Loads messages from an MO file. | `GettextMoFile` |
| <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) |
| [`save()`](#method-save) | Saves messages to an MO file. | `GettextMoFile` |
| <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) |
| [`readBytes()`](#method-readBytes) protected | Reads one or several bytes. | `GettextMoFile` |
| [`readInteger()`](#method-readInteger) protected | Reads a 4-byte integer. | `GettextMoFile` |
| [`readString()`](#method-readString) protected | Reads a string. | `GettextMoFile` |
| [`writeBytes()`](#method-writeBytes) protected | Write bytes. | `GettextMoFile` |
| [`writeInteger()`](#method-writeInteger) protected | Writes a 4-byte integer. | `GettextMoFile` |
| [`writeString()`](#method-writeString) protected | Writes a string. | `GettextMoFile` |

## Method details

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

```php
public function load(string $filePath, string $context): array
```

Loads messages from an MO file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$filePath` | `string` | File path |
| `$context` | `string` | Message context |

**Returns**

`array`: Message translations. Array keys are source messages and array values are translated messages: source message => translated message.

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if unable to read the MO file

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

```php
public function save(string $filePath, array $messages): mixed
```

Saves messages to an MO file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$filePath` | `string` | File path |
| `$messages` | `array` | Message translations. Array keys are source messages and array values are translated messages: source message => translated message. Note if the message has a context, the message ID must be prefixed with the context with chr(4) as the separator. |

**Returns**

`mixed`

**Throws**

- [`yii\base\Exception`](/docs/api/yii/base/exception/index.md): if unable to save the MO file

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

```php
protected function readBytes(resource $fileHandle, int $byteCount = 1): string|null
```

Reads one or several bytes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileHandle` | `resource` | To read from |
| `$byteCount` | `int` | To be read |

**Returns**

`string|null`: Bytes

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

```php
protected function readInteger(resource $fileHandle): int
```

Reads a 4-byte integer.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileHandle` | `resource` | To read from |

**Returns**

`int`: The result

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

```php
protected function readString(resource $fileHandle, int $length, int|null $offset = null): string|null
```

Reads a string.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileHandle` | `resource` | File handle |
| `$length` | `int` | Of the string |
| `$offset` | `int\|null` | Of the string in the file. If null, it reads from the current position. |

**Returns**

`string|null`: The result

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

```php
protected function writeBytes(resource $fileHandle, string $bytes): int
```

Write bytes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileHandle` | `resource` | To write to |
| `$bytes` | `string` | To be written |

**Returns**

`int`: How many bytes are written

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

```php
protected function writeInteger(resource $fileHandle, int $integer): int
```

Writes a 4-byte integer.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileHandle` | `resource` | To write to |
| `$integer` | `int` | To be written |

**Returns**

`int`: How many bytes are written

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

```php
protected function writeString(resource $fileHandle, string $string): int
```

Writes a string.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileHandle` | `resource` | To write to |
| `$string` | `string` | To be written |

**Returns**

`int`: How many bytes are written