📜 ⬆️ ⬇️

How I did multilanguage on Codeigniter

First of all I want to say thanks for the comments. I think this topic will be still relevant for many, as I occasionally receive messages on Skype from users of Habra to help create a “module” of multilingualism.
image
This post is completely rewritten, since at the moment I use a completely different approach to developing multilingualism for CodeIgniter 3. Also, the post is as short as possible with an example and a brief description.

Solution to the problem



In general, with time and a more detailed study of this framework, I came to the conclusion that multilingualism should be done in a router .

The principle of multilingual work


The logic of multilingual, painfully simple. One function in the updated MY_Router.php that will add to the beginning of all the rules - the languages ​​of the site. After the function, CodeIgniter 3 will work with the new route list. It will turn out like this :)
')
It was
$route['default_controller'] = "pages/index"; $route['pages/(.+)'] = 'pages/index/$1'; $route['news/(.+)'] = 'news/view/$1'; 


Will become
 $route['default_controller'] = "pages/index"; $route['(by|ru|kz|en)/pages/(.+)'] = 'pages/index/$2'; $route['(by|ru|kz|en)/news/(.+)'] = 'news/view/$2'; 


But first, let's create a file with settings, where we will store the available languages ​​of the site and set the default language config / localize_config.php

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); /** *   . * * default_key =>     * list =>    * * @author Sergey Makhlenko * @version 1.0 */ $config['ROUTE_LOCALIZE'] = array( 'default_key' => 1, //  -,     "list" (0 -> by, 1 -> ru, .... 4 -> en) 'list' => array('by', 'ru', 'kz', 'ua', 'en'), //     ); 


We undertake to expand the standard system / core / Router.php.

Extend CI_Router (MX_Router)

Create the MY_Router.php file in the application / core directory. If you are using HMVC, you must first download it Router.php
MY_Router.php add the following code block to the beginning

 <?php (defined('BASEPATH')) OR exit('No direct script access allowed'); // load the MX_Router class if ( file_exists(APPPATH."third_party/MX/Router.php") ) { require APPPATH."third_party/MX/Router.php"; } .... 


2) After that, create an extension of the class CI_Router or if with HMVC MX_Router
 class MY_Router extends CI_Router { /** * Language user or default language *     - */ public $user_lang = ''; /** * Class constructor * * Run the route mapping function. * * @param array $routing * @return void */ public function __construct($routing = NULL) { parent::__construct(); } ... } 


3) And now the most interesting. We need to extend the standard function CI_Router -> _set_routing . Perhaps from version to version, this function will be changed by the CI developers, I advise you to simply sort through these lines so that after any update of the framework, your MY_Routing file is up to date. Moreover, you need to add only 1 line.

So let's start, copy the function (method) _set_routing from system / core / Router.php and insert MY_Routing into our newly created class.
find the lines after which the function loaded routes.php

 ..... if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) { include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); } // Validate & get reserved routes if (isset($route) && is_array($route)) { //     ... ..... 


and include in this section of code our line for changing the received routers.

 // Update Routing Localize $this->__localize_init($route); 


4) And here is the function itself that will remake our rules under the understanding of various languages.

 /** * Append to routing localize lang * * @param array $route Route is config/routes.php * @return array */ private function __localize_init( &$route = array() ) { // Loader config localize if (file_exists(APPPATH.'config/localize_config.php')) { include(APPPATH.'config/localize_config.php'); $localize = $config['ROUTE_LOCALIZE']; } else { return FALSE; } /* --------------------------------------------------------- */ // Check config localize if ( !isset($localize) or !isset($localize['list']) ) { return FALSE; } if ( !isset($localize['default_key']) ) { $localize['default_key'] = 0; } $localize['default_key'] = intval($localize['default_key']); /* --------------------------------------------------------- */ // Language join list $lang_list = implode('|', $localize['list']); // Create new route list foreach ( $route as $key => $item ) { $_route[$key] = $item; if ( $key == 'default_controller' ) { $_route['('.$lang_list.')'] = $route['default_controller']; $_route['('.$lang_list.')/(.+)'] = '$2'; } } /* --------------------------------------------------------- */ // Check default language if ( isset( $localize['list'][ $localize['default_key'] ] ) ) { $this->user_lang = $localize['list'][ $localize['default_key'] ]; } // User select language if ( array_search( $this->uri->segment(1), $localize['list'] ) !== FALSE ) { $this->user_lang = $this->uri->segment(1); } $route = $_route; } 


That's all!


Now your website will understand the links vashsite.com/by , vashsite.com/ru or vashsite.com (using the default language).

You always have the opportunity to get the selected language (by, ru, ..... kz) using the variable described below in your controllers or models.
 $this->router->user_lang; 


Then it's up to your imagination how to work with user_lang :) you already have everything you need.

Ready version on bitbucket.org

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


All Articles