# GettextMessageSource

> GettextMessageSource represents a message source that is based on GNU Gettext.

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

| class | `yii\i18n\GettextMessageSource` |
| --- | --- |
| Extends | [`MessageSource`](/docs/api/yii/i18n/messagesource/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/GettextMessageSource.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/i18n/GettextMessageSource.php#L31) |

GettextMessageSource represents a message source that is based on GNU Gettext.

Each GettextMessageSource instance represents the message translations
for a single domain. And each message category represents a message context
in Gettext. Translated messages are stored as either a MO or PO file,
depending on the [`$useMoFile`](/docs/api/yii/i18n/gettextmessagesource/index.md#property-useMoFile) property value.

All translations are saved under the [`$basePath`](/docs/api/yii/i18n/gettextmessagesource/index.md#property-basePath) directory.

Translations in one language are kept as MO or PO files under an individual
subdirectory whose name is the language ID. The file name is specified via
[`$catalog`](/docs/api/yii/i18n/gettextmessagesource/index.md#property-catalog) property, which defaults to 'messages'.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-basePath"></span>`$basePath` | `string` | Base directory of messages files Default: `'@app/messages'`. | `GettextMessageSource` |
| <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-catalog"></span>`$catalog` | `string` | Sub-directory of messages files Default: `'messages'`. | `GettextMessageSource` |
| <span id="property-forceTranslation"></span>[`$forceTranslation`](/docs/api/yii/i18n/messagesource/index.md#property-forceTranslation) | `bool` | Whether to force message translation when the source and target languages are the same. Default: `false`. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |
| <span id="property-sourceLanguage"></span>[`$sourceLanguage`](/docs/api/yii/i18n/messagesource/index.md#property-sourceLanguage) | `string\|null` | The language that the original messages are in. Default: `null`. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |
| <span id="property-useBigEndian"></span>`$useBigEndian` | `bool` | Whether to use big-endian when reading and writing an integer Default: `false`. | `GettextMessageSource` |
| <span id="property-useMoFile"></span>`$useMoFile` | `bool` | Whether to use generated MO files Default: `true`. | `GettextMessageSource` |

## 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/i18n/messagesource/index.md#method-init) | Initializes this component. | [`MessageSource`](/docs/api/yii/i18n/messagesource/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) |
| <span id="method-translate"></span>[`translate()`](/docs/api/yii/i18n/messagesource/index.md#method-translate) | Translates a message to the specified language. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |
| <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) |
| [`getMessageFilePath()`](#method-getMessageFilePath) protected | Returns message file path for the specified language and category. | `GettextMessageSource` |
| [`loadFallbackMessages()`](#method-loadFallbackMessages) protected | The method is normally called by [`loadMessages()`](/docs/api/yii/i18n/gettextmessagesource/index.md#method-loadMessages) to load the fallback messages for the language. | `GettextMessageSource` |
| [`loadMessages()`](#method-loadMessages) protected | Loads the message translation for the specified $language and $category. | `GettextMessageSource` |
| [`loadMessagesFromFile()`](#method-loadMessagesFromFile) protected | Loads the message translation for the specified language and category or returns null if file doesn't exist. | `GettextMessageSource` |
| <span id="method-translateMessage"></span>[`translateMessage()`](/docs/api/yii/i18n/messagesource/index.md#method-translateMessage) protected | Translates the specified message. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_MISSING_TRANSLATION"></span>[`EVENT_MISSING_TRANSLATION`](/docs/api/yii/i18n/messagesource/index.md#event-EVENT_MISSING_TRANSLATION) | [`MissingTranslationEvent`](/docs/api/yii/i18n/missingtranslationevent/index.md) | An event that is triggered when a message translation is not found. Event name: `'missingTranslation'`. | [`MessageSource`](/docs/api/yii/i18n/messagesource/index.md) |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-MO_FILE_EXT"></span>`MO_FILE_EXT` | `'.mo'` |  | `GettextMessageSource` |
| <span id="const-PO_FILE_EXT"></span>`PO_FILE_EXT` | `'.po'` |  | `GettextMessageSource` |

## Method details

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

```php
protected function getMessageFilePath(string $language): string
```

Returns message file path for the specified language and category.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$language` | `string` | The target language |

**Returns**

`string`: Path to message file

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

```php
protected function loadFallbackMessages(string $category, string $fallbackLanguage, array $messages, string $originalMessageFile): array
```

The method is normally called by [`loadMessages()`](/docs/api/yii/i18n/gettextmessagesource/index.md#method-loadMessages) to load the fallback messages for the language.

Method tries to load the $category messages for the $fallbackLanguage and adds them to the $messages array.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$category` | `string` | The message category |
| `$fallbackLanguage` | `string` | The target fallback language |
| `$messages` | `array` | The array of previously loaded translation messages. The keys are original messages, and the values are the translated messages. |
| `$originalMessageFile` | `string` | The path to the file with messages. Used to log an error message in case when no translations were found. |

**Returns**

`array`: The loaded messages. The keys are original messages, and the values are the translated messages.

**Since**

2.0.7

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

```php
protected function loadMessages(string $category, string $language): array
```

Loads the message translation for the specified $language and $category.

If translation for specific locale code such as `en-US` isn't found it
tries more generic `en`. When both are present, the `en-US` messages will be merged
over `en`. See [`loadFallbackMessages()`](/docs/api/yii/i18n/gettextmessagesource/index.md#method-loadFallbackMessages) for details.
If the $language is less specific than [`$sourceLanguage`](/docs/api/yii/i18n/messagesource/index.md#property-sourceLanguage), the method will try to
load the messages for [`$sourceLanguage`](/docs/api/yii/i18n/messagesource/index.md#property-sourceLanguage). For example: [`$sourceLanguage`](/docs/api/yii/i18n/messagesource/index.md#property-sourceLanguage) is `en-GB`,
$language is `en`. The method will load the messages for `en` and merge them over `en-GB`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$category` | `string` | The message category |
| `$language` | `string` | The target language |

**Returns**

`array`: The loaded messages. The keys are original messages, and the values are translated messages.

**See also**

- [`loadFallbackMessages()`](/docs/api/yii/i18n/gettextmessagesource/index.md#method-loadFallbackMessages)
- [`$sourceLanguage`](/docs/api/yii/i18n/messagesource/index.md#property-sourceLanguage)

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

```php
protected function loadMessagesFromFile(string $messageFile, string $category): array|null
```

Loads the message translation for the specified language and category or returns null if file doesn't exist.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$messageFile` | `string` | Path to message file |
| `$category` | `string` | The message category |

**Returns**

`array|null`: Array of messages or null if file not found