PHPackages                             phalcon/incubator-translate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. phalcon/incubator-translate

ActiveLibrary[Framework](/categories/framework)

phalcon/incubator-translate
===========================

Extra Translate adapters for Phalcon Translate component

v0.2.0(5y ago)42.8k4MITPHPPHP &gt;=7.2

Since Jan 6Pushed 4y ago4 watchersCompare

[ Source](https://github.com/phalcon/incubator-translate)[ Packagist](https://packagist.org/packages/phalcon/incubator-translate)[ Docs](https://phalcon.io)[ GitHub Sponsors](https://github.com/phalcon)[ Fund](https://opencollective.com/phalcon)[ RSS](/packages/phalcon-incubator-translate/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (3)Dependencies (10)Versions (4)Used By (0)

Phalcon\\Incubator\\Translate
=============================

[](#phalconincubatortranslate)

[![Discord](https://camo.githubusercontent.com/28c6fc95b5decf0e67719a5438501589c00b2db2b15228e67479d6548bbc9f6b/68747470733a2f2f696d672e736869656c64732e696f2f646973636f72642f3331303931303438383135323337353239373f6c6162656c3d446973636f7264)](http://phalcon.io/discord)[![Packagist Version](https://camo.githubusercontent.com/d6af8f7c690f04c7715d0f06872e282afe70d0853300dbe056789af87e8f6d20/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068616c636f6e2f696e63756261746f722d7472616e736c617465)](https://packagist.org/packages/phalcon/incubator-translate)[![PHP from Packagist](https://camo.githubusercontent.com/5c407092711bad3f8864a3f26018cdd72e3efbcbe4afa829259a2852d297546a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7068616c636f6e2f696e63756261746f722d7472616e736c617465)](https://packagist.org/packages/phalcon/incubator-translate)[![codecov](https://camo.githubusercontent.com/94815495963009bb96497bb1424d1593caf797c73ba1c650599ec7bada59d947/68747470733a2f2f636f6465636f762e696f2f67682f7068616c636f6e2f696e63756261746f722d7472616e736c6174652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/phalcon/incubator-translate)[![Packagist](https://camo.githubusercontent.com/9db4cb6d19b19fbf7172decd13366bdcaf12c4a09fe9d53e222f38f14688a7ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64642f7068616c636f6e2f696e63756261746f722d7472616e736c617465)](https://packagist.org/packages/phalcon/incubator-translate/stats)

Issues tracker
--------------

[](#issues-tracker)

Database
--------

[](#database)

You can use your database to store the translations, too.

First of all, you need to up your database. To do this, use \[DI\]\[1\] (in `/public/index.php`). Take a look:

```
use Phalcon\Db\Adapter\Pdo\Mysql;

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

Then, you should get the translation through your `controller`. Put this on it:

```
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
            ]
        );
    }

    // ...
}
```

To store the translations, the following table is recommended:

```
CREATE TABLE `translations` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `language` VARCHAR(5) NOT NULL COLLATE 'utf8_bin',
    `key_name` VARCHAR(48) NOT NULL COLLATE 'utf8_bin',
    `value` TEXT NOT NULL COLLATE 'utf8_bin',
    PRIMARY KEY (`id`)
)
```

Optional create unique index

```
CREATE UNIQUE INDEX translations_language_key_name_unique_index ON translations (language, key_name);
```

The columns are self-described, but pay attention to `language` — it's a column that stores the language that the user is using, that can be `en`, `en-us` or `en-US`. Now it's your responsibility to decide which pattern you want to use.

To display for your users the translated words you need to set up a variable to store the `expressions/translations`from your database. *This step happens in your controller.* Follow the example:

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

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

Then, just output the`phrase/sentence/word` in your view:

```

```

Or, if you wish you can use \[Volt\]\[2\]:

```
{{ expression._("IndexPage_Hello_World") }}
```

Mongo
-----

[](#mongo)

Implements a Mongo adapter for translations.

A generic collection with a source to store the translations must be created and passed as a parameter.

```
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');
```

MultiCsv
--------

[](#multicsv)

This adapter extends *Phalcon\\Translate\\Adapter\\Csv* by allowing one csv file for several languages.

- CSV example (blank spaces added for readability):

```
#ignored;     en_US;  fr_FR;   es_ES
label_street; street; rue;     calle
label_car;    car;    voiture; coche
label_home;   home;   maison;  casa
```

- PHP example :

```
// 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).

```php
