blob: e772e8e275953f778bf92f3f7ad0217e0bfce90f [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##############
2Language Class
3##############
4
5The Language Class provides functions to retrieve language files and
6lines of text for purposes of internationalization.
7
8In your CodeIgniter system folder you'll find one called language
9containing sets of language files. You can create your own language
10files as needed in order to display error and other messages in other
11languages.
12
Andrey Andreevb11b9f32012-11-26 23:01:24 +020013Language files are typically stored in your **system/language/** directory.
14Alternately you can create a directory called language inside your
15application folder and store them there. CodeIgniter will always load the
16one in **system/language/** first and will then look for an override in
17your **application/language/** directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050018
19.. note:: Each language should be stored in its own folder. For example,
20 the English files are located at: system/language/english
21
22Creating Language Files
23=======================
24
Andrey Andreevb11b9f32012-11-26 23:01:24 +020025Language files must be named with **_lang.php** as the file extension. For
Derek Jones8ede1a22011-10-05 13:34:52 -050026example, let's say you want to create a file containing error messages.
27You might name it: error_lang.php
28
29Within the file you will assign each line of text to an array called
Andrey Andreevb11b9f32012-11-26 23:01:24 +020030``$lang`` with this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050031
Andrey Andreevb11b9f32012-11-26 23:01:24 +020032 $lang['language_key'] = 'The actual message to be shown';
Derek Jones8ede1a22011-10-05 13:34:52 -050033
34.. note:: It's a good practice to use a common prefix for all messages
35 in a given file to avoid collisions with similarly named items in other
36 files. For example, if you are creating error messages you might prefix
37 them with error\_
38
39::
40
Andrey Andreevb11b9f32012-11-26 23:01:24 +020041 $lang['error_email_missing'] = 'You must submit an email address';
42 $lang['error_url_missing'] = 'You must submit a URL';
43 $lang['error_username_missing'] = 'You must submit a username';
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45Loading A Language File
46=======================
47
48In order to fetch a line from a particular file you must load the file
49first. Loading a language file is done with the following code::
50
51 $this->lang->load('filename', 'language');
52
53Where filename is the name of the file you wish to load (without the
54file extension), and language is the language set containing it (ie,
55english). If the second parameter is missing, the default language set
Andrey Andreevb11b9f32012-11-26 23:01:24 +020056in your **application/config/config.php** file will be used.
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030057
58.. note:: The *language* parameter can only consist of letters.
Derek Jones8ede1a22011-10-05 13:34:52 -050059
60Fetching a Line of Text
61=======================
62
63Once your desired language file is loaded you can access any line of
64text using this function::
65
66 $this->lang->line('language_key');
67
Andrey Andreevce0c9562012-11-22 17:26:29 +020068Where *language_key* is the array key corresponding to the line you wish
Derek Jones8ede1a22011-10-05 13:34:52 -050069to show.
70
Andrey Andreevce0c9562012-11-22 17:26:29 +020071You can optionally pass FALSE as the second argument of that method to
72disable error logging, in case you're not sure if the line exists::
73
74 $this->lang->line('misc_key', FALSE);
75
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030076.. note:: This method simply returns the line. It does not echo it.
Derek Jones8ede1a22011-10-05 13:34:52 -050077
78Using language lines as form labels
79-----------------------------------
80
81This feature has been deprecated from the language library and moved to
Derek Jones123bb202013-07-19 16:37:51 -070082the :func:`lang()` function of the :doc:`Language Helper
Andrey Andreevb11b9f32012-11-26 23:01:24 +020083<../helpers/language_helper>`.
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85Auto-loading Languages
86======================
87
88If you find that you need a particular language globally throughout your
Andrey Andreevb11b9f32012-11-26 23:01:24 +020089application, you can tell CodeIgniter to :doc:`auto-load
90<../general/autoloader>` it during system initialization. This is done
91by opening the **application/config/autoload.php** file and adding the
92language(s) to the autoload array.