PHPackages                             lantosbro/cli-localizer - 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. [CLI &amp; Console](/categories/cli)
4. /
5. lantosbro/cli-localizer

ActiveLibrary[CLI &amp; Console](/categories/cli)

lantosbro/cli-localizer
=======================

A library for localization in CLI applications

1.0.0(1y ago)03MITPHPPHP ^7.4|^8.0

Since Oct 14Pushed 1y ago1 watchersCompare

[ Source](https://github.com/LantosBro/cli-localizer)[ Packagist](https://packagist.org/packages/lantosbro/cli-localizer)[ Docs](https://github.com/LantosBro/cli-localizer)[ RSS](/packages/lantosbro-cli-localizer/feed)WikiDiscussions master Synced 1mo ago

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

CliLocalizer
============

[](#clilocalizer)

CliLocalizer is a PHP library for easy localization in CLI applications with support for multiple translation file formats (JSON, YAML, PHP(Array) and MO).

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

[](#installation)

You can install the package via composer:

```
composer require lantosbro/cli-localizer
```

### Requirements

[](#requirements)

- PHP 7.4 or higher
- PHP YAML extension
- PHP Gettext extension

To install the YAML extension:

```
pecl install yaml
```

To install the Gettext extension:

```
pecl install gettext
```

Then add `extension=yaml.so` and `extension=gettext.so` to your php.ini file.

Usage
-----

[](#usage)

```
use CliLocalizer\Translator;
use CliLocalizer\CliOutput;

// Initialize the Translator with the required translations path, format, and optional locale and fallback locale
$translator = new Translator('/path/to/your/translations', 'mo', 'ru_RU', 'en_US');

// Create a CliOutput instance
$output = new CliOutput($translator);

// Use the output
$output->writeln('welcome_message', ['name' => 'User']);
$output->writeln('processing_files', ['count' => 5]);
```

Configuration
-------------

[](#configuration)

You must specify the path to your translations directory and the format of your translation files when initializing the Translator. Supported formats are:

- JSON ('json')
- YAML ('yaml')
- PHP ('php')
- MO ('mo')

Create translation files in your specified translations directory using the chosen format.

Example translation files:

JSON (en\_US.json):

```
{
    "welcome_message": "Welcome, :name!",
    "processing_files": "Processing :count files..."
}
```

YAML (en\_US.yaml):

```
welcome_message: "Welcome, :name!"
processing_files: "Processing :count files..."
```

PHP (en\_US.php):

```
