PHP code example of nyrados / translator

1. Go to this page and download the library: Download nyrados/translator 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/ */

    

nyrados / translator example snippets




use Nyrados\Translator\TranslatorApi;
use Nyrados\Translator\Provider\ArrayProvider;

 'article_header' => 'A meaningful title',
    'article' => 'A very interesting article',
]);
$provider->set('de', [
    'greet' => 'Hallo {name}!',
    'article_header' => 'Eine aussagekräftige Überschrift',
]);

$t = new Translator();
$t->addProvider($provider);
$t->setPreferences(['de-at', 'en-us']);



//Output: Hallo John!
$t->translate('greet', ['name' => 'John!']);



$trans = $t->translate([
    'article_header', 
    'article'
    //with context: 'article' => ["context" => "value"]
]);

//Output: <h1>A meaningful title</h1><hr>
echo "<h1>" . $trans . "</h1><hr>";

//Output: <p>A very interesting article</p>
echo "<p>" . $trans . "</p>";


$trans->rewind();
echo $trans->current()($context);

$trans->next();
echo $trans->current()($context);



echo $trans->get($context);
echo $trans($context) // via __invoke()
echo (string) $trans; // via __toString()


// Set from $_SERVER['HTTP_ACCEPT_LANGUAGE']
$t->setPreferences(Helper::preferencesFromAcceptLanguage());

// Set from a Psr\Http\Message\ServerRequestInterface instance
$t->setPreferences(Helper::preferencesFromAcceptLanguage($request));