Codeigniter Language Class Library


Codeigniter Language Class Library – This library provides various functions which are used to transitive language files and path of text for purpose of internationalization. We can easily create or incorporate own language files. This library provides translation of core message into another language. Here in this tutorial, we are going to explain how to use language class library.


Codeigniter language class library | Example

Let us understand how language class library works in codeigniter with examples.

Functions:-

There are following functions available in language class library. Now we will explain one by one with example.

  • 1. Sample Language Files.
  • 2. Example of switching languages.
  • 3. Creating Language Files.
  • 4. Loading A Language File.
  • 5. Fetching a Line of Text.

1. Sample Language Files.

Here is simple demo of sample language files.

How to create multiple language file in language folder:-

Codeigniter Language Class Library

2. Example of switching languages.

Here is simple demo of switching language.

Syntax of switching language:-

$language = $this->session->get_userdata('english');
$this->lang->load('error_messages', $language);
$oops = $this->lang->line('message_key');

3. Creating language files.

Here is simple demo of creating language.

Language file must be named with _lang.php as the file extension name.

Syntax of creating language:-

$lang['Welcome_message'] = 'You are welcome';
$lang['error_email_missing'] = 'You must submit an email address';
$lang['error_url_missing'] = 'You must submit a URL';
$lang['error_username_missing'] = 'You must submit a username';

4. Loading a language file.

Here is simple demo of loading a language files.

Syntax of loading a language files:-

$this->lang->load('alert_language', 'english');

5. Fetching a Line of Text.

Here is simple demo of fetching a line of text.

Syntax of fetching a line of text:-

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

EXAMPLE

Here is simple example of language class library.

Controller code:-

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class language_controller extends CI_Controller 
{
public function input()
{
$this->load->view('library/language_view');
$this->lang->load('alert_language', 'english');
echo $this->lang->line('welcome_message');
$this->lang->load('alert_language', 'hindi');
echo $this->lang->line('welcome_message');
}
}
?>

You must have created new folder with extension _lang.php in application/language folder.

Folder path: application/language/english/alert_language_lang.php

<?php
$lang['welcome_message'] = 'You are Welcome!';
?>

Folder path: application/language/hindi/alert_language_lang.php

<?php
$lang['welcome_message'] = 'आपका स्वागत है !';
?>

The output will be like this –

Codeigniter Language Class Library

Advertisements

User Ans/Comments

You also need to first enable hook for using language library of CodeIgniter, like done here: https://www.cloudways.com/blog/multi-language-codeigniter/Hooks are enabled and configured before using languageloader class.
 olidev

Add Comment

📖 Read More