📜 ⬆️ ⬇️

PHPixie Amalgama - Internationalization in PHPixie

I have been using PHPixie for quite some time now and am very pleased, the only thing I lacked was support for multilingualism. Since I work in Kazakhstan, most customers want several localizations on their website, especially for government agencies. PHPixie was my first framework, I didn’t want to switch to something else (although I confess I almost changed it with an old woman, Kohana) and I wrote “ugly crutches” to implement multilingualism. In this article, we will discuss my new, quite convenient and elegant “crutch”, which I decided to share - the “PHPixie Amalgama” module.

About module


In the arsenal, the module has basic functionality for working with line breaks, as well as a small extension for those who do not want to prescribe the language settings in the routes. The module is easy to install, easy to configure, convenient to use for many tasks.

Installation and Setup


Register the package in the “require” section of the “composer.json” file like this:

"phpixie/amalgama": "2.*@dev" 

Run the following command in the site directory:
')
 php composer.phar update -o --prefer-dist 

Add amalgama.php config to / assets / config / and write our settings:

 return array( //    'list' => array('en', 'ru', 'kk'), //    'default' => 'en', // ,     'autorouting' => true, //    ,      'autoroutingExcept' => '^admin_' ); 

Expand our Pixie.php:

 namespace App; class Pixie extends \PHPixie\Amalgama\Pixie { ... protected function after_bootstrap() { parent::after_bootstrap(); } } 

There we define a module like this:

 protected $modules = array( ... 'amalgama' => '\PHPixie\Amalgama' ); 

Expand our base controller:

 <?php namespace App; class Page extends \PHPixie\Amalgama\Controller { public function before() { parent::before(); ... } ... } 

Define routes if autorouting is false:

 'default' => array( array('(/<lang>)(/<controller>(/<action>(/<id>)))', array('lang' => '(en|ru|kk)') array( 'controller' => 'hello', 'action' => 'index', 'lang' => 'en' ), ), 

Add transfer files to the / assets / config / amalgama directory:

 //ru.php <?php return array( 'Hello World!' => ' !', 'Hello <?>!' => ' <?>!' ); //kk.php return array( 'Hello World!' => 'CÓ™ Ó™!', 'Hello <?>!' => 'CÓ™ <?>!' ); 

As you can see, translations support parameters, and for the default language, translations are not required.
It should also be said that the module itself extends the Route class to correctly generate addresses from the routes and View \ Helper, so that it is convenient to translate into views.

Using


Thanks to the helper, you can use the following abbreviated entry in representations for line breaks:

 <?php echo $__('Hello World!'); //  " !"    ?> <?php echo $__('Hello <?>!', array('')); //  " !"    ?> 

Displaying the language switch is also very simple:

 <?php foreach($this->helper->getLangList() as $lang) : ?> <?php if ($lang == $this->helper->getCurrentLang()) : ?> <span><?php echo $lang; ?></span> <?php else: ?> <a href="<?php echo $this->helper->langSwitchLink($lang); ?>"><?php echo $lang; ?></a> <?php endif; ?> <?php endforeach; ?> 

The Paginate module works as it should, just do not forget to transmit the current language:

 $page = $this->request->param('page'); $comments = $this->pixie->orm->get('comment'); $pager = $this->pixie->paginate->orm($comments, $page, 10); $pager->set_url_route('comments', array('lang' => $this->lang)); 

Well, as without validation:

 $validator->field('username') ->rule('filled') ->error($this->__('Field <?> must not be empty', array($this->__('username')))); 

Conclusion


As you can see, PHPixie is very easy to expand. For most small projects, it has everything you need, and if something is missing, then it’s not difficult to finish it yourself. In the future, support for the plural is planned, the basic functionality is already there, but there is not enough time to bring it to the mind and write documentation. It is possible to translate from Russian without too much strain with such functionality. If you find a bug, errors in the documentation or have any ideas, I will be glad to get feedback and pullrequest.

Module repository here: Github

Source: https://habr.com/ru/post/244113/


All Articles