GithubHelp home page GithubHelp logo

translate's Introduction

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

translate's People

Contributors

delfimov avatar wolxxx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

translate's Issues

PHP Fatal error: Declaration of DElfimov\\Translate\\Loader\\LoaderInterface::has($language) must be compatible with Psr\\Container\\ContainerInterface::has(string $id): bool

Hello!

When i use the following code to initialize translate:

$t = new DElfimov\Translate\Translate(
        new DElfimov\Translate\Loader\PhpFilesLoader(realpath($_SERVER["DOCUMENT_ROOT"])."/translations"),
        ["default" => $_ENV["DEFAULT_LANGUAGE"], "available" => explode("|", $_ENV["AVAILABALE_LANGUAGE"])]
    );

I get the following error:
PHP Fatal error: Declaration of DElfimov\\Translate\\Loader\\LoaderInterface::has($language) must be compatible with Psr\\Container\\ContainerInterface::has(string $id): bool

Am i missing a module or something like that?

Thank you in advance!

Just want to say hello and spacibo

hi dmitry,
i just started another project, thought about translations and how easy it is to use your project.
so i required it, made everything ready and now it is running under php 8.1 and 8.2.

spacibo and greetings in these awkward times.

peace!

Auo-Update fails on packagist

hi!
at first: thank you for having the pull request accepted and merged into master and created a new release for it.
unfortunatly, the new version of this package is not available on packagist. a look there tells me:
"delfimov/translate (Legacy Auto-Update, Needs Attention)"
https://packagist.org/about#how-to-update-packages suggests to add web hooks calls or log in into packagist with github access and let packagist automatically add the web hook calls.

could you please have a look there?

spacibo!

greetz

add logging mechanism for missing translations?

hi
i stumbled today over this nice repository and am willing to use this package.
after setting up successfully the first translations, a feature wish came up :)

it would be awesome to provide a mechanism that logs missing translations.
the psr-3 standard may help or directly use the awesome monolog package https://github.com/Seldaek/monolog

if you want me to do this task, tell me more. otherwise it would be great to have this feature in a future release :)

thx

Fatal error

When I test in my script php, I have this error : Fatal error: Interface 'Psr\Container\ContainerInterface' not found
And when I run the exemple, I have the same error :
translateprob
PHP7.2, host : OVH

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.