Codeigniter - SPLessons

CodeIgniter Language

Home > Lesson > Chapter 28
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Language

CodeIgniter Language

shape Description

Many of the modern applications have one of the important feature of providing pages with multi-languages which helps for internationalization. This is possible with CodeIgniter Language class and the functions in this class helps in fetching the text and the files of Language.

Default Language

To set the default language of a site, open the file application/config/config.php file and edit the code with following. [php]$config['language'] = 'english';[/php]

Create language files

shape Description

In the file location application/language various language files can be created and within those files, sub-folders can be created for every language like German, French. To save the language files, the file name must have the suffix _lang. For example, initially create a file "english" which consists of another file data_lang.php with the translated data. [html] application/ |----language/ | |---english/ | | |---message_lang.php | | | …… | |---french/ | | |---message_lang.php | | | …… | |---german/ | | |---message_lang.php | | | …… | | …… | | [/html] Inside the language file (data_lang.php) the following code has to be entered by adding the required data to the array $lang. [php]$lang['language_key'] = 'The actual message to be shown';[/php] Some of the language files will be as follows. english/data_lang.php file: [php]<?php $lang['welcome_message'] = 'Welcome to SPLessons';[/php] french/data_lang.php file: [php]<?php $lang['welcome_message'] = 'Bienvenue à SPLessons';[/php] german/data_lang.php file: [php]<?php $lang['welcome_message'] = 'Willkommen in SPLessons';[/php]

Load language files

shape Description

The second step is to load the language files in the controller using the function __construct() by entering the below code in it. [php]$this->lang->load('message','english');[/php] Note: All the controllers of the application should load the language files in the above process.

Retrieving files

Once the language file is loaded, the data can be retrieved using below code. [php]$this->lang->line('welcome_message');[/php]

Summary

shape Key Points

  • CodeIgniter Language class helps for internationalization.
  • Language files should have suffix _lang.
  • Language files are loaded inside the __construct() function of controller.