Skip to content

Latest commit

 

History

History
 
 

Adapter

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Phalcon\Translate\Adapter

Usage examples of the adapters available here:

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 (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
		    'useIcuMessageFormatter' => true, // Optional, if need formatting message using ICU MessageFormatter
		]);
	}
	
	// ...
}

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 thephrase/sentence/word in your view:

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

Or, if you wish you can use Volt:

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

ICU MessageFormatter Example

// Example plural message with key 'cats'
// Peter has {nbCats, plural, =0{no cat} =1{a cat} other{# cats}}

$this->_getTranslation()->_('cats', ['nbCats' => rand(0, 10)]);

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;

$fmt = new MessageFormatter(
    "en_US",
    "{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree"
);

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

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

ResourceBundle

This adapter uses ResourceBundle as translation frontend.

The extension intl must be installed in PHP.

use Phalcon\Translate\Adapter\ResourceBundle;

$translate = new ResourceBundle([
    'bundle'   => '/path/to/bundle', // required
    'locale'   => 'en',              // required
    'fallback' => false              // optional, default - true
]);

echo $translate->t('application.title');
echo $translate->t('application.copyright', ['currentYear' => new \DateTime('now')]);

ResourceBundle source file example

root {
    application {
        title { "Hello world" }
        copyright { "&copy; 2001-{currentYear, date, Y}. Foobar" }
    }
}