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: funcgetarg(): 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?



11.5k

What I did

Done



30.7k
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.



46.8k
edited May '15

I was using _ but xgettext message extraction will confuse the JS underscore.js library with this and so I went to explicit t and n

$compiler->addFunction('t', function ($resolvedArgs, $exprArgs) {
    if (count($exprArgs) === 1) {
        $str = sprintf('$this->translate->gettext(\'%s\')', $exprArgs[0]['expr']['value']);
    } else {
        $str = 'Volt Compiler Error for translation function.';
        error_log('Volt Compiler Error for translation function.');
    }

    return $str;
});


$compiler->addFunction('n', function ($resolvedArgs, $exprArgs) {
    if (count($exprArgs) === 3) {
        $val1 = $exprArgs[0]['expr']['value'];
        $val2 = $exprArgs[1]['expr']['value'];
        $val3 = $exprArgs[2]['expr']['value'];
        $str = sprintf('$this->translate->ngettext(\'%s\', \'%s\', %d)', $val1, $val2, $val3);
    } else {
        $str = 'Volt Compiler Error for translation function.';
        error_log('Volt Compiler Error for translation function.');
    }

    return $str;
});

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.



30.7k

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?



30.7k

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