PHP code example of phalcon / incubator-translate

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

    

phalcon / incubator-translate example snippets


use Phalcon\Db\Adapter\Pdo\Mysql;

$di->set(
    'db',
    function () {
        return new Mysql(
            [
                'host'     => 'localhost',
                'username' => 'root',
                'password' => 123456,
                'dbname'   => 'application',
            ]
        );
    }
);

use Phalcon\Translate\Adapter\Database;

class IndexController extends \Phalcon\Mvc\Controller
{
    protected function _getTranslation()
    {
        return new Database(
            [
                'db'       => $this->di->get('db'), // Here we're getting the database from DI
                'table'    => 'translations', // The table that is storing the translations
                'language' => $this->request->getBestLanguage(), // Now we're getting the best language for the user
            ]
        );
    }

    // ...
}

class IndexController extends \Phalcon\Mvc\Controller
{
    protected function _getTranslation()
    {
        // ...
    }

    public function indexAction()
    {
        $this->view->setVar(
            'expression',
            $this->_getTranslation()
        );
    }
}

<html>
    <head>
        <!-- ... -->
    </head>
    <body>
        <h1> echo $expression->_("IndexPage_Hello_World"); 

<h1>{{ expression._("IndexPage_Hello_World") }}</h1>

use MessageFormatter;
use Phalcon\Translate\Adapter\Mongo;
use My\Application\Collections\Translate;

$translate = new Mongo(
    [
        'collection' => Translate::class,
        'language'   => 'en',
    ]
);

echo $translate->t('application.title');

// the constructor is inherited from Phalcon\Incubator\Translate\Adapter\CsvMulti
$titles_translater = new Phalcon\Translate\Adapter\MultiCsv(
    "{$config->langDir}/titles.csv",
    "es_ES",
    new InterpolatorFactory(),
    []
);

echo $titles_translater->query('label_home'); // string 'casa'


## Interpolator Intl

It needs the extension [intl](php.net/manual/book.intl.php) to be installed in PHP, and it uses [MessageFormatter](http://php.net/manual/en/class.messageformatter.php) objects in an interpolator interface.
More about the syntax convention can be read on this [formating guide](https://www.sitepoint.com/localization-demystified-understanding-php-intl/) and on the [ICU documentation](http://userguide.icu-project.org/formatparse/messages).




use Phalcon\Translate\Adapter\NativeArray;
use Phalcon\Translate\Interpolator\Intl;

$translate = new NativeArray(
    [
        'interpolator' => new Intl('fr_FR'), // this interpolator must be locale aware
        'content' => [
            'apples' => "{count, plural, =0{Je n'ai aucune pomme} =1{J'ai une pomme} other{J'ai # pommes}}.",
        ],
    ]
);

// thousands separator is " " (blank space) for fr_FR
echo $translate->_(
    'apples',
    [
        'count' => 1000,
    ]
); // J'ai 1 000 pommes
csv
#ignored;     en_US;  fr_FR;   es_ES
label_street; street; rue;     calle
label_car;    car;    voiture; coche
label_home;   home;   maison;  casa