CodeIgniter - Internationalization

The dialect class in CodeIgniter gives a simple approach to bolster numerous dialects for internationalization. We can utilize this sorts of class with characterize content with all dialects content. To some degree, we can utilize distinctive dialect documents to show message in various dialects.

We can put diverse dialect records in application/dialect index. Framework dialect documents can be found at framework/dialect catalog, yet to add your own dialect to your application, you ought to make a different organizer for every dialect in application/dialect registry. What's more, you can characterize messages with various dialects.

Creating files Language

To start with make a dialect record, you should finished with _lang.php. For instance, you need to make a dialect petition for Germen dialect, and afterward you should spare it with germen_lang.php. Inside this records you should be store all your dialect messages in key, esteem blend in $lang cluster as demonstrated as follows. In the event that you need to show this controller then just call germen not germen_lang. It is just determined catchphrases.

$lang[‘key’] = ‘val’;

Loading Language file

To use any of the language in your application, you must first load the file of that particular language for retrieve various texts based on key values are stored in that file. You can use the following code to load the language file.

$this->lang->load('filename', 'language');

·         filename − It is the name of file you want to load. Don’t use extension of file here but only name of file. i.e If filename is germen_lang.php then Don’t use lang, only use germen.

·         Language − It is the language set containing it.

Fetching Language Text

To fetch a line from the language file simply execute the following code.

$this->lang->line('language_key');

Where language_key is the key parameter used to fetch value of the key  in the loaded language file.

Autoload Languages

On the off chance that you require some dialect comprehensively, then you can autoload it in application/config/autoload.php document as demonstrated as follows.

$lan=array(
    'en' => 'english',
    'de' => 'german',
    'fr' => 'french',
    'nl' => 'dutch'
  );
$autoload['language'] = array($lan);

Simply, pass the different languages to be autoloaded by CodeIgniter.

We also change the routing URL methods as like:

$route['^fr/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1"; 
// '/en' and '/fr' -> use default controller
$route['^fr$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];

First we make language as like below:

application/language/english/demo_lang.php

$lang['about.gender'] = "I'm a man";

application/language/french/demo_lang.php

$lang['about.gender'] = "Je suis un homme";

we must be include into the controller file as like below:

class About extends CI_Controller {
	function index()
	{
		// you might want to just autoload these two helpers
		$this->load->helper('language');
		$this->load->helper('url');
		// load language file
		$this->lang->load('demo'); 
		$this->load->view('demo');
	}
}

Then View side make demo.php file to diplay as like:


 

Now you can test as like domainname/en/demo.

 

Let's Think together, Say Something !