Skip to content

Repository files navigation

Latest Stable VersionBuild StatusStyleCISensioLabsInsightLicense

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><?phpuseDElfimov\Translate\Translate;
useDElfimov\Translate\Loader\PhpFilesLoader;
useMonolog\Logger; // PSR-3 logger, not required useMonolog\Handler\StreamHandler;
$log = newLogger('Translate');
$log->pushHandler(newStreamHandler('path/to/your.log', Logger::WARNING));
$t = newTranslate(
newPhpFilesLoader(__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 headerecho$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

<?phpreturn [
'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

<?phpreturn [
'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

About

PHP translation library

Topics

Resources

Stars

16 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages