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

Problem with gettext

I am trying to use \Phalcon\Translate\Adapter\Gettext to create a multilanguage site.

I have searched in the forum and i 've found a previous discussions about the same problem (https://forum.phalcon.io/discussion/1150/need-help-with-gettext-solved#C28767), but even following that I did not manage to succeed.

I created the folders following the instructions (path/to/modules/moduleName/Language/it_IT/LC_MESSAGES/ and messages.po and .mo inside)

In my BaseController I have the method _getTranslation() with the following content:

$translator =  new \Phalcon\Translate\Adapter\Gettext(array(
'locale' => 'it_IT.utf8',
'file' => 'messages',
'directory' => __DIR__ . '../Language/'
));
return $translator;

Then the IndexController extends that with:

$this->view->setVar("translator", $this->_getTranslation());

And finally i call it in volt:


{{translator._("Hello world")}}

So far I've followed the instructions that seemed to solve the problem in the other dicusssion. Than I saw that I don't have the file `/etc/locale.gen` (that is required I think), but even with the command `locale-gen` it is not created.

Anybody knows a possible reason for that? Is there another way to create it maybe?


43.9k

Hi,

be sure that all the languages you want to use in your app are configured as locales on your box. With ubuntu, this might help:

https://askubuntu.com/questions/76013/how-do-i-add-locale-to-ubuntu-server

I tried the command locale -a and this is the result:

C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
it_CH.utf8
it_IT.utf8
POSIX

I don't know if they are all the languages that I will use, but now for the example, I'd just need it_IT.utf8 and it's there, so I don't think that's my problem. Please correct me if I'm wrong.

Thanks for your answer, but do you have any other ideas?



39.2k
Accepted
answer
edited Jan '16

The issue https://github.com/phalcon/cphalcon/issues/11311

is fixed https://github.com/phalcon/cphalcon/pull/11312

Create /tmp/test.php

<?php
_('Hello');
_('Amazing %name%!');

Next run xgettext /tmp/test.php (this creates a file called messages.po)

Change the "Content-Type: text/plain; charset=CHARSET\n" in this file and replace CHARSET with UTF-8 in this case.

Change the "Language: \n" in this file: "Language: uk\n"

Next translate all strings:

msgid "Hello"
msgstr "Привіт"

msgid "Amazing %name%!"
msgstr "Чудовий %name%!"

Save and use the command msgfmt -o messages.mo messages.po which will create a compiled .mo file called messages.mo

Create locale dir mkdir -p /tmp/locale/uk_UA.utf8/LC_MESSAGES

Place this file in this directory /tmp/locale/uk_UA.utf8/LC_MESSAGES/messages.mo

Create test script /tmp/gettext.php:

<?php

use Phalcon\Translate\Adapter\Gettext;

$translator = new Gettext(
    [
        'locale'        => 'uk_UA.utf8',
        'defaultDomain' => 'messages',
        'directory'     => '/tmp/locale',
    ]
);

echo $translator->query('Hello'), PHP_EOL;
echo $translator->t('Amazing %name%!', ['name' => 'Phalcon']), PHP_EOL;
echo $translator->_('Hello'), PHP_EOL;

Note: Your system will only support the locales installed on your OS, in the exact format given by your OS. For using Gettext with different locales you must have all locales installed that you use. See locale -a

No forget to set category

<?php

use Phalcon\Translate\Adapter\Gettext;

$translator = new Gettext(
    [
        'locale'        => 'uk_UA.utf8',
        'defaultDomain' => 'messages',
        'directory'     => '/tmp/locale',
       'category' => LC_MESSAGES
    ]
);

echo $translator->query('Hello'), PHP_EOL;
echo $translator->t('Amazing %name%!', ['name' => 'Phalcon']), PHP_EOL;
echo $translator->_('Hello'), PHP_EOL;

Yes, category is necessary.!

No forget to set category

<?php

use Phalcon\Translate\Adapter\Gettext;

$translator = new Gettext( [ 'locale' => 'uk_UA.utf8', 'defaultDomain' => 'messages', 'directory' => '/tmp/locale', 'category' => LC_MESSAGES ] );

echo $translator->query('Hello'), PHP_EOL; echo $translator->t('Amazing %name%!', ['name' => 'Phalcon']), PHPEOL; echo $translator->('Hello'), PHP_EOL;

only work if:

locale -a

locale is register in system.

sudo locale-gen ru_RU.utf8
sudo update-locale  

For Ubuntu 16.04

work fine.