at 0x41a66e0>
CodeIgniter

3.0-dev User Guide

Language Class

The Language Class provides functions to retrieve language files and lines of text for purposes of internationalization.

In your CodeIgniter system folder you’ll find one called language containing sets of language files. You can create your own language files as needed in order to display error and other messages in other languages.

Language files are typically stored in your system/language/ directory. Alternately you can create a directory called language inside your application folder and store them there. CodeIgniter will always load the one in system/language/ first and will then look for an override in your application/language/ directory.

Note

Each language should be stored in its own folder. For example, the English files are located at: system/language/english

Using the Language Class

Creating Language Files

Language files must be named with _lang.php as the filename extension. For example, let’s say you want to create a file containing error messages. You might name it: error_lang.php

Within the file you will assign each line of text to an array called $lang with this prototype:

$lang['language_key'] = 'The actual message to be shown';

Note

It’s a good practice to use a common prefix for all messages in a given file to avoid collisions with similarly named items in other files. For example, if you are creating error messages you might prefix them with error_

$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';

Loading A Language File

In order to fetch a line from a particular file you must load the file first. Loading a language file is done with the following code:

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

Where filename is the name of the file you wish to load (without the file extension), and language is the language set containing it (ie, english). If the second parameter is missing, the default language set in your application/config/config.php file will be used.

Note

The language parameter can only consist of letters.

Fetching a Line of Text

Once your desired language file is loaded you can access any line of text using this function:

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

Where language_key is the array key corresponding to the line you wish to show.

You can optionally pass FALSE as the second argument of that method to disable error logging, in case you’re not sure if the line exists:

$this->lang->line('misc_key', FALSE);

Note

This method simply returns the line. It does not echo it.

Using language lines as form labels

This feature has been deprecated from the language library and moved to the lang() function of the Language Helper.

Auto-loading Languages

If you find that you need a particular language globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the language(s) to the autoload array.

Class Reference

class CI_Lang
load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]])
Parameters:
  • $langfile (string) – Language file to load
  • $idiom (string) – Language name (i.e. ‘english’)
  • $return (bool) – Whether to return the loaded array of translations
  • $add_suffix (bool) – Whether to add the ‘_lang’ suffix to the language file name
  • $alt_path (string) – An alternative path to look in for the language file
Returns:

Array of language lines if $return is set to TRUE, otherwise void

Return type:

mixed

Loads a language file.

line($line[, $log_errors = TRUE])
Parameters:
  • $line (string) – Language line key name
  • $log_errors (bool) – Whether to log an error if the line isn’t found
Returns:

Language line string or FALSE on failure

Return type:

string

Fetches a single translation line from the already loaded language files, based on the line’s name.