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?
This sample application (https://github.com/phalcon/php-site) implements translation using Phalcon.
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
I think the best solution is to use Phalcon Gettext Adapter. The most important advantage of Gettext is handling plurals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With