PHP code example of chipslays / phrase

1. Go to this page and download the library: Download chipslays/phrase library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chipslays / phrase example snippets


// Engine constructor
__construct(string $root, string $locale, ?string $fallback);

use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\JsonEngine;

$engine = new JsonEngine(__DIR__ . '/locales/json', 'en_US');
$phrase = new Phrase($engine);
$phrase->get(...);

use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\YamlEngine;

$engine = new YamlEngine(__DIR__ . '/locales/yaml', 'en_US');
$phrase = new Phrase($engine);
$phrase->get(...);

use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\JsonEngine;

$engine = new JsonEngine(__DIR__ . '/locales/json', 'en_US');
Phrase::setEngine($engine);
Phrase::get(...);

$phrase->get(string $key, ?array $replace = null, ?string $locale = null);

# key - locale message key
# replace - array with interpolation
# locale - force locale (useful when using multiple locales at the same time)

$phrase->get('hello');
Phrase::get('hello');
__('hello');

$phrase->get('hello', null, 'ru_RU');
Phrase::get('hello', null, 'ru_RU');
__('hello', null, 'ru_RU');

$phrase->get('hello', ['{name}' => 'John Doe']);
Phrase::get('hello', ['{name}' => 'John Doe']);
__('hello', ['{name}' => 'John Doe']);

// Hello John Doe!

$phrase->get('plural', ['{count}' => 1, '{money}' => 100])
Phrase::get('plural', ['{count}' => 1, '{money}' => 100])
__('plural', ['{count}' => 1, '{money}' => 100])

// I have 1 melon and 100 dollars.

use Chipslays\Phrase\Phrase;
use Chipslays\Phrase\Engine\YamlEngine;

$engine = new YamlEngine(__DIR__ . '/locales/yaml', 'en_US');
$phrase = new Phrase($engine);

// this method loaded en_US.yml file from MyPlugin dir and merge with previously loaded locale en_US
$phrase->patch(__DIR__ . '/locales/plugins/MyPlugin/', 'en_US');

// this method delete and overwrite all previous messages
$phrase->load(__DIR__ . '/locales/yaml', 'en_US');


use Chipslays\Phrase\Plural;

echo Plural::eng(10, 'melon'); // melons
echo Plural::rus(10, ['арбуз', 'арбуза', 'арбузов']); // арбузов

__(string $key, ?array $replace = null, ?string $locale = null): string|array

__('hello', ['{name}' => 'John Doe'], 'en_US');

use Chipslays\Phrase\Engine\AbstractEngine;
use Chipslays\Phrase\Exceptions\PhraseException;

class YamlEngine extends AbstractEngine
{
    /**
     * @param string $locale
     * @return void
     *
     * @throws PhraseException
     */
    protected function load(string $locale): void
    {
        $path = $this->root . '/' . $locale . '.yml';

        if (!file_exists($path)) {
            throw new PhraseException("Locale file not found in path {$path}", 1);
        }

        $this->locales[$locale] = yaml_parse_file($path);
    }
}