5

Does Phalcon provide any facility for multilingual web sites?

What is best practice for developing an MVC phalcon based site? How should I implement view layer?

Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173

3 Answers3

4

This sample application (https://github.com/phalcon/php-site) implements translation using Phalcon.

twistedxtra
  • 2,699
  • 1
  • 18
  • 13
2

Phalcon does not offer I18n functionality. There is a PECL extension that offers this kind of functionality, called intl (see manual).

However, if you are mostly interested in presenting the website in different languages, you can use the

\Phalcon\Translate\Adapter\NativeArray 

component. This component uses an array of key/values that contain the language aware strings. For instance you can use this in your config:

$trans_config = array(
                    'en' => array(
                        'bye'       => 'Good Bye',
                        'song-key'  => 'This song is %song% (%artist%)',
                    ),
                    'es' => array(
                        'bye'       => 'Adiós',
                        'song-key'  => 'La canción es %song% (%artist%)',
                    ),
                );

A test to demonstrate the above usage is:

public function testVariableSubstitutionTwoEnglish()
{
    $language   = $trans_config['en'];
    $params     = array('content' => $language);
    $translator = new \Phalcon\Translate\Adapter\NativeArray($params);

    $vars     = array(
        'song'   => 'Dust in the wind',
        'artist' => 'Kansas',
    );
    $expected = 'This song is Dust in the wind (Kansas)';
    $actual   = $translator->_('song-key', $vars);

    $this->assertEquals(
        $expected,
        $actual,
        'Translator does not translate English correctly - many parameters'
    );
}

The above just shows how you can get translated messages with placeholder variables. To simply get a string in a different language, you just call the _() on the translator with the relevant key and no variables passed.

EDIT In the view you can work as you like. You can set variables that are displayed in the view layer or pass the translating object and perform the translation there. Up to you.

HTH

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
  • Thanks, What other adapter do you plan to implement? Creating and using such arrays is a little wordy, also it was better if `_('x',array('1','2'))` could be used in addition to `_('x',array('a'=>'1','b'=>'2'))`. Is it a good job to create a service namee `t` to be accessed via views? – Handsome Nerd Oct 20 '12 at 17:00
  • What is pros and cons of using `\Phalcon\Translate\Adapter\NativeArray` instead of Gettext? – Handsome Nerd Oct 20 '12 at 17:02
  • Re Gettext very small, mostly performance and preference based. Performance actually is not a huge issue. – Nikolaos Dimopoulos Oct 20 '12 at 18:00
  • There is no such service (yet) but can definitely be addressed later on. At the moment you can just grab the array and assign it in a view variable, and then access it in the view. – Nikolaos Dimopoulos Oct 20 '12 at 18:01
  • I believe I heard that there will be at some point a PO adapter but not sure there. This particular question could be answered better by the developers/community. – Nikolaos Dimopoulos Oct 20 '12 at 18:02
2

I think the best solution is to use Phalcon Gettext Adapter. The most important advantage of Gettext is handling plurals.

IH2
  • 21
  • 1