https://github.com/delfimov/translate
PHP translation library
https://github.com/delfimov/translate
i18n multi-language php plural plurals psr-3 psr-6 translate translation
Last synced: about 1 year ago
JSON representation
PHP translation library
- Host: GitHub
- URL: https://github.com/delfimov/translate
- Owner: delfimov
- License: mit
- Created: 2012-02-01T13:58:26.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2023-06-21T19:13:43.000Z (almost 3 years ago)
- Last Synced: 2025-04-12T15:57:29.950Z (about 1 year ago)
- Topics: i18n, multi-language, php, plural, plurals, psr-3, psr-6, translate, translation
- Language: PHP
- Homepage:
- Size: 102 KB
- Stars: 16
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/delfimov/translate)
[](https://travis-ci.org/delfimov/Translate)
[](https://styleci.io/repos/3325239)
[](https://insight.sensiolabs.com/projects/26b7cfce-3636-4385-be29-54f51b6dfe42)
[](https://github.com/delfimov/GDImage/blob/master/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
* [PHP >= 5.6](http://www.php.net/)
## How to install
Add this line to your composer.json file:
```json
"delfimov/translate": "~2.0"
```
or
```sh
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`](example) directory for sources.
`example\example.php`
```php
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";
?>
```
`example\messages\en\messages.php`
```php
'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
'Просто строка',
'%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