We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Simple Multilingual Project using Gettext

Hello, I'm trying to configure a multilingual Phalcon website using Phalcon\Translate\Adapter\Gettext but it seems i'm making some mistakes that i can't figure out where they are.

I just only see the strings untranslated. For example if a echo this:

echo $this->translation->_("hola-mundo");

But I get this:

hola-mundo

First of all, I'm starting a new project via console:

phalcon project translate-gettext

Then I register the new service:

use Phalcon\Translate\Adapter\Gettext as TranslationService;
$di->setShared('translation', function () use ($di)
{
    $translate = new TranslationService(array(
        'locale'        => 'es_ES',
        'file'          => 'messages',
        'defaultDomain' => 'messages',
        'directory'     => APP_PATH . '/languages/',
        'category'      => LC_MESSAGES,
    ));
    return $translate;
});

My APP_PATH is at (config.php):

defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');

In ControllerBase.php, I create the method initialize() and set a var to use it inside my volt files (but, for this sample, I will not use it):

public function initialize()
{
    $this->view->setVars([
        't' => $this->translation
    ]);
}

Then, in IndexController.php:

public function indexAction()
{
    echo $this->translation->_("hola-mundo");
    $this->view->disable();
}

Now, I create the language directories:

app/languages/es_ES/LC_MESSAGES/

With Poedit, I start a new translation file, with español(España) as my language

I save the file inside app/languages/es_ES/LC_MESSAGES/ as messages.po

Then, I extract the strings form the source code, defining the directories to find these strings as app/

It detects me the string at IndexController.php, then I translate it, save the file, and Poedit inserts the string inside messages.po and compile it to messages.mo

This is the messages.po:

msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2016-10-01 11:57+0200\n"
"PO-Revision-Date: 2016-10-01 11:58+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: es_ES\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.8.9\n"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SearchPath-0: .\n"

#: controllers/IndexController.php:11
msgid "hola-mundo""
msgstr "Hola Mundo!"

OK, I think I made it all in the right way, then I visited the website (https://www.phalcon.dev/translate-gettext/) and it showed me:

hola-mundo

So, it is not working

What am I doing wrong?

I have echoed the language directory to view if it is correct:

echo $this->translation->getDirectory();

And it is ok:

/var/www/vhosts/phalcon.dev/translate-gettext/app/languages/

I have tried changing the language to en_US -> no changes at all

Also, changing the directory name to es_ES.utf8 and the 'locale' variable to 'es_ES.utf8' Apache gaves me an "500 Internal Server Error" This is the line in apache_error.log:

[error] [client ::1] FastCGI: incomplete headers (0 bytes) received from server "/Applications/MAMP/fcgi-bin/php7.0.10.fcgi", referer: https://www.phalcon.dev/

I thought I made all the steps in the right way, but it seems I don't.

Please, I need some help to make work this simple thing

edited Oct '16

What phalcon version you are using ? https://github.com/phalcon/cphalcon/issues/11311



3.9k

phalcon 3.0.1 in php 7.0.1 with MAMP in OS X 10.11.6

Well is it working with native php function ?



3.9k

Well is it working with native php function ?

Yes, and it works outside Phalcon

this is my test.php file in a new created domain

$locale = "es_ES";
putenv("LANG=" . $locale); 
setlocale(LC_ALL, $locale);
$domain = "messages";
//bindtextdomain($domain, "locale");  // Also works like this
bindtextdomain($domain, "/Users/andres/Dropbox/phalcon.dev/translate-gettext-native/locale");
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
echo _("hi-world");

It returns

Hola Mundo

But inside Phalcon I can't make it work the same code

I have removed all the lines relatives to Phalcon\Translate\Adapter\Gettext class

And also created a new domain just for that (gettext.dev)

And in IndexController.php i inserted this:

public function indexAction()
{
    $locale = "es_ES";
    putenv("LANG=" . $locale); 
    setlocale(LC_ALL, $locale);
    $domain = "messages";
    bindtextdomain($domain, "/Users/andres/Dropbox/gettext.dev/translate-gettext/app/languages");
    //bindtextdomain($domain, APP_PATH . '/languages/');  // This does not work either
    bind_textdomain_codeset($domain, 'UTF-8');
    textdomain($domain);

    echo _("hi-world");

    $this->view->disable();
}

And stills returns me

hi-world


3.9k

@andresrl Did you seen https://forum.phalcon.io/discussion/10159/problem-with-gettext

Thanks @sergeyklay that helped me.

This is what I have now in services.php:

use Phalcon\Translate\Adapter\Gettext as TranslationService;

$di->setShared('translation', function () use ($di) {
    $translate = new TranslationService(array(
        'locale'        => 'es_ES',
        'defaultDomain' => 'messages',
        'directory'     => APP_PATH . '/languages',
    ));
    return $translate;
});

and inside the IndexController.php:

echo $this->translation->_("Hello"), PHP_EOL;
echo $this->translation->_('Amazing %name%!', ['name' => 'Phalcon']);

Also I have retrieved the strings with Poedit, putting te messages.mo inside the languages dir

And It works now!

I still don't know what I was doing wrong anyway, but now I know how to do it right, Thanks!