One of the most useful features of CodeIgniter is its language class and support. It allows you to store content and set that content to belong to various languages; it is then possible to switch between languages to display different text in the same place holders in the view files. It's really easy to set up and this is how you do it.
A little information about language files. You'll need to know the rules for naming them. Language files are stored at /path/to/codeigniter/application/system/language/[language_name]/
.
Where [language_name]
is the name of the language you wish to support. So, for example, if you want to support English, French, and German you will create three file names:
/path/to/codeigniter/application/language/english/en_lang.php
/path/to/codeigniter/application/language/french/fr_lang.php
/path/to/codeigniter/application/language/german/de_lang.php
You can see above that the three files are named 'en', 'fr', and 'de'. Appended to the names is _lang.php
; you must append each file with _lang.php
so that CodeIgniter knows it is a language file.
So, in order to create an English language file create the following files:
/path/to/codeigniter/application/system/language/english/en_lang.php
/path/to/codeigniter/application/controllers/lang.php
/path/to/codeigniter/application/views/lang/english.php
/path/to/codeigniter/application/controllers/lang.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'), class Lang extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper('form'), $this->load->helper('url'), $this->load->helper('language'), $this->lang->load('en', 'english'), } public function index() { redirect('lang/submit'), } public function submit() { $this->load->library('form_validation'), $this->form_validation->set_error_delimiters('', '<br />'), // Set validation rules $this->form_validation->set_rules('email', $this->lang->line('form_email'), 'required|min_length[1]|max_length[50]|valid_email'), // Begin validation if ($this->form_validation->run() == FALSE) { $this->load->view('lang/form'), } else { echo $this->lang->line('form_confirm_email') . $this->input->post('email'), } } }
/path/to/codeigniter/application/views/lang/form.php
<html> <body> <h2><?php echo $this->lang->line('form_title') ; ?></h2> <?php echo validation_errors() ; ?> <?php echo form_open('lang/submit') ; ?> <?php echo $this->lang->line('form_email') ; ?> <?php echo form_input(array('name' => 'email','id' => 'email','value' => '','maxlength' => '100','size' => '50','style' => 'width:10%')) ; ?> <?php echo form_submit('', $this->lang->line('form_submit_button')) ; ?> <?php echo form_close() ; ?> </body> </html>
/path/to/codeigniter/language/english/en_lang.php
<?php $lang['form_title'] = "Form title in English"; $lang['form_email'] = "Email"; $lang['form_submit_button'] = "Submit"; $lang['form_confirm_email'] = "Your email is: "; ?>
In the constructor of the controller /path/to/codeigniter/application/controllers/lang.php
we're loading helpers, such as form and URL, but we're also doing two language-related things, loading the language helper and setting the language to be used:
$this->load->helper('language'), $this->lang->load('en', 'english'),
Where 'en'
is the language and 'English'
is the folder we're storing all English-related content in.
We're loading the language helper and declaring the language filename and the language to be used, specifically the line:
$this->lang->load('filename','language'),
Here, the first parameter is the name of the language file minus the _lang.php
(so en_lang.php will be just 'en'
, fr_lang.php
will just be 'fr'
and so on). The second parameter is the language (in this case, it is the folder in the /path/to/codeigniter/application/language/
folder).
Once we have loaded the language class and defined the correct language and filename, we can then begin to pull out items in the $lang
array. The way we pull items out of the $lang
array is by echoing $this->lang->line(array_element_name);
so, to pull out the form title we would write echo $this->lang->line('form_title'),
13.58.60.192