PHP code example of carrooi / translator

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

    

carrooi / translator example snippets


php composer.phar 

$translator->addPluralForm(
	'cs',
	3,
	'(n===0) ? 2 : ((n===1) ? 0 : ((n>=2 && n<=4) ? 1 : 2))'
);

$message = $translator->translate('dictionary.info', null, array(		// output: display some random variable: 2 books
	'something' => '2 books'
));

$message = $translator->translate('dictionary.info', array(		// output: display some random variable: 2 books
	'something' => '2 books'
));

$translator->addFilter(function($message) {
	return strrev($message);
});

var_dump($translator->translate('homepage.title'));			// output is reversed title in homepage dictionary

$translator->addHelper('truncate', function($translation, $length) {
	return substr($translation, 0, $length);
});
$translator->addHelper('firstUpper', function($translation) {
	return ucfirst($translation);
});

var_dump($translator->translate('homepage.promo.box.description|truncate:4|firstUpper');		// output: Some

$messages = $translator->translate('dictionary.fruits', 6);		// output: array(6 oranges, 6 bananas)

$messages = array(
    'homepage.promo.box.title',
    'homepage.promo.box.description',
    'homepage.promo.box.text'
);

$result = $translator->translateMap($messages);

$messages = array(
    'title',
    'description',
    'text'
);

$result = $translator->translateMap($messages, null, null, 'homepage.promo.box.title');

$result = $translator->translateMap($messages, null, array('type' => 'book'));

$translator = new \DK\Translator\Translator('/app/lang');
$translator->setLanguage('en');

$message = $translator->translate('cs|homepage.title');