PHPackages                             mb4it/messages - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. mb4it/messages

ActiveLibrary[Localization &amp; i18n](/categories/localization)

mb4it/messages
==============

description

1.0.1(3mo ago)061MITPHPPHP &gt;=8.2

Since Feb 16Pushed 3mo agoCompare

[ Source](https://github.com/Dictator90/mb-messages)[ Packagist](https://packagist.org/packages/mb4it/messages)[ RSS](/packages/mb4it-messages/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (1)

MB Messages
===========

[](#mb-messages)

A package for working with translations and messages, implementing logic similar to Laravel's `illuminate/translation`. Supports in-memory storage (arrays) and loading from the filesystem (PHP and JSON). Uses `mb4it/collections` for deep key nesting and ICU plural rules for pluralization.

Requirements
------------

[](#requirements)

- PHP 8.2+

Installation
------------

[](#installation)

```
composer require mb4it/messages
```

Components
----------

[](#components)

### MessagesInterface

[](#messagesinterface)

Contract for translation providers. Declares methods:

- `get(string $key, array $replace = [], ?string $locale = null): string` — get translation by key
- `choice(string $key, int $number, array $replace = [], ?string $locale = null): string` — get translation with pluralization
- `has(string $key, ?string $locale = null): bool` — check if translation exists

### ArrayMessages

[](#arraymessages)

Implementation of `MessagesInterface` for storing translations in memory (arrays). Useful for programmatic loading and tests.

```
use MB\Messages\ArrayMessages;

$messages = new ArrayMessages([], locale: 'en', fallback: 'ru');

// Adding translations: addMessages(locale, namespace, array)
$messages->addMessages('en', 'validation', [
    'required' => 'The :attribute field is required.',
    'email' => 'The :attribute must be a valid email address.',
    'min' => [
        'string' => 'The :attribute must be at least :min characters.',
        'numeric' => 'The :attribute must be at least :min.',
    ],
    'attributes' => [
        'name' => 'name',
        'email' => 'email address',
    ],
]);

// Get translation
$messages->get('validation.required', ['attribute' => 'email']);
// "The email field is required."

// Nested keys
$messages->get('validation.min.string', ['attribute' => 'password', 'min' => 8]);
// "The password must be at least 8 characters."

// Attributes
$messages->get('validation.attributes.email');
// "email address"
```

**ArrayMessages methods:**

MethodDescription`__construct(array $messages, string $locale, ?string $fallback, ?MessageSelector $selector)`Create instance`addMessages(string $locale, string $namespace, array $messages)`Add translations for locale and group`mergeIntoLocale(string $locale, array $messages)`Merge translations into locale root (for JSON)`setMessages(array $messages)`Replace all translations`setLocale(string $locale)`Set current locale`getLocale(): string`Get current locale`setFallback(?string $locale)`Set fallback locale`getFallback(): ?string`Get fallback locale`setPlaceholderFormat(string $format)`Set placeholder format (`{key}` is the marker)`getPlaceholderFormat(): string`Get current placeholder format`setPlaceholderReplacer(?callable $fn)`Set callback `(string $text, array $replace): string``getPlaceholderReplacer(): ?callable`Get current callback### FileMessages

[](#filemessages)

Implementation of `MessagesInterface` for loading translations from files. Internally uses `ArrayMessages` — data from PHP and JSON files is loaded into it, all operations are delegated. Supports:

- PHP group files: `{path}/{locale}/{group}.php` (e.g. `resources/lang/en/validation.php`)
- JSON files: `{path}/{locale}.json` (flat keys)

Groups are loaded lazily on first key access.

**Directory structure:**

```
resources/lang/
├── en/
│   ├── validation.php
│   └── messages.php
├── ru/
│   ├── validation.php
│   └── messages.php
├── en.json
└── ru.json

```

**Example PHP file (validation.php):**

```
