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

Gettext

I am building a new application that will need the support of multilanguaging so I was wondering how to use the Phalcon\Translation\Adapter\Gettext Class in a normal app using Volt. My initial idea was to just register it:


    $di->set('message', function() {
        return new Phalcon\Translate\Adapter\Gettext(array(
            'category'      => LC_ALL,
            'locale'        => 'en_US',
            'defaultDomain' => 'messages',
            'directory'     => __DIR__ . '../locale/'
        ));
    });

But this approach raises two Problems:

First: I get a warning every time i use {{ message.query('Hello') }}: PHP Warning: func_get_arg(): Argument 2 not passed to function in .....

Second: In previous applications without phalcon i used xgettext to find all strings. Is there a similar way possible with the phalcon implementation?



16.4k

What I did

Done



43.9k
edited May '15

Hi,

You can use phalcon's incubator gettext adapter. See in incubator READMEto know how to enable incubator's libraries in your application.

This post may be helpfull if you're in pain with gettext.

xgettext parse php file but do not handle "templating" language like volt/smarty as says here see 5.1.3.

Maybe using phtml files for your views can do the trick for xgettext.

That's the way I'm doing it, bu I'm not sure how good it is, since is loading everything in DI

$translator = new \Phalcon\Translate\Adapter\NativeArray(array('content' => require $langFile));
$di->setShared('t', $translator);

and then in volt I just do

t._("text")

and in phtml

$this->t->_("text")

is giving me a short sintax in both volt and phtml

edited May '15

Okay, thanks for the Answers so far...

@stefanchiriac: Your answer seems to be the most straight forward but uses an Array. We don't really have plural support in arrays, so thats sadly not the option i was hoping for.

@le51: Incubator doesnt have a Gettext Extension anymore. The Gettext class is now part of Phalcon, thats why i am using it :)

@kaioken: So you are saying i should just use the native gettext without the Phalcon class wrapper?

@dschissler: Interesting approach, but it lacks some flexibility. I don't want to add a own rule for every possible way of using ngettext.

So i am still open for suggestions to this. I will try to figure out a more flexible way on dschisslers base.



43.9k

Hi,

I can't see any gettext in 2.0.0 branch of Phalcon\Translate .... (only native array and csv are built in adapters)

And Gettext adapter is still available in incubator.

Hey:)

Here are the Links to the repos i found. Am i on the wrong ones?



43.9k

You're right, it is implemented (didn't know that, because it is not documented in https://docs.phalcon.io/en/latest/api/index.html) in phalcon. And I'm was still on the 1.3 branch of incubator.