I am use gettext, all code in bootstrap or index.php
<?php
$config = new \Phalcon\Config([
'app' => [
'base' => '/',
'debug' => (bool) ($_SERVER['REMOTE_ADDR'] == '127.0.0.1'),
'locale' => 'es_ES',
],
'path' => [
'controllers' => __DIR__.'/../app/controllers/',
'libraries' => __DIR__.'/../app/libraries/',
'messages' => __DIR__.'/../app/messages/',
'models' => __DIR__.'/../app/models/',
'views' => __DIR__.'/../app/views/',
'tmp' => __DIR__.'/../tmp/',
],
]);
$di->set('view', function() use ($config) {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir($config->path->views);
$view->registerEngines(['.phtml' => function($view, $di) use ($config) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions([
'compiledPath' => $config->path->tmp,
'compileAlways' => $config->app->debug,
]);
$volt->getCompiler()->addFunction('_', 'gettext');
return $volt;
}]);
return $view;
}, true);
putenv('LANG='.$config->app->locale);
setlocale(LC_ALL, $config->app->locale.'.utf8');
bindtextdomain('messages', $config->path->messages);
bind_textdomain_codeset('messages', 'utf8');
textdomain('messages');
For each language I need the following structure:
app
-> messages
-> es_ES
-> LC_MESSAGES
-> messages.po
The messages.po file contains the translated strings, created and updated with poedit for example.
For simplicity of gettext scanner I have mapped the function _ () to volt.
In a controller: $this->flash->error(_('Bad credentials.'));
In a volt view: {{ _('Homepage') }}
Best regards. --cesar