Translate icon indicating copy to clipboard operation
Translate copied to clipboard

PHP translation library

Latest Stable Version Build Status StyleCI SensioLabsInsight License

Translate

Easy to use i18n translation PHP class for multi-language websites with language auto detection and plurals.

PSR-6 translation containers. PSR-3 logger.

Requirements

How to install

Add this line to your composer.json file:

"delfimov/translate": "~2.0"

or

composer require delfimov/translate

Alternatively, copy the contents of the Translate folder into one of your project's directories and require 'src/Translate.php';, require 'src/Loader/LoaderInterface.php';, require 'src/Loader/PhpFilesLoader.php'; If you don't speak git or just want a tarball, click the 'zip' button at the top of the page in GitHub.

A Simple Example

See example directory for sources.

example\example.php

<pre><?php

use DElfimov\Translate\Translate;
use DElfimov\Translate\Loader\PhpFilesLoader;
use Monolog\Logger; // PSR-3 logger, not required  
use Monolog\Handler\StreamHandler;

$log = new Logger('Translate');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

$t = new Translate(
    new PhpFilesLoader(__DIR__ . "/messages"),
    [
        "default" => "en",
        "available" => ["en", "ru"],
    ],
    $log // optional
);

$num = rand(0, 100);

$t->setLanguage("en"); // this is not required, language will be auto detected with Accept-Language HTTP header
echo $t->t('some string') . "\n\n"; // or $t('some string');
echo $t->plural('%d liters', $num) . "\n\n";
echo $t->plural("The %s contains %d monkeys", $num, ['tree', $num]) . "\n\n";

$num = rand(0, 100);

$t->setLanguage("ru");
echo $t->t('some string')."\n\n"; // or $t('some string');
echo $t->plural('%d liters', $num) . "\n\n";
echo $t->plural("The %s contains %d monkeys", $num, ['tree', $num]) . "\n\n";

?></pre>

example\messages\en\messages.php

<?php
return [
    'some string' => 'Some string',
    '%d liters' => ['%d liter', '%d liters'],
    '%d liters alt' => '%d liter|%d liters',
    'The %s contains %d monkeys' => ['The %s contains %d monkey', 'The %s contains %d monkeys'],
    'The %s contains %d monkeys alt' => 'The %s contains %d monkey|The %s contains %d monkeys',
];

example\messages\ru\messages.php

<?php
return [
    'some string' => 'Просто строка',
    '%d liters' => '%d литр|%d литра|%d литров',
    'The %s contains %d monkeys' => ['На %s сидит %d обезьяна', 'На %s сидят %d обезьяны', 'На %s сидят %d обезьян'],
    'The %s contains %d monkeys alt' => 'На %s сидит %d обезьяна|На %s сидят %d обезьяны|На %s сидят %d обезьян',
    'tree' => 'дереве'
];

TODO

  • Better code coverage