blob: 10449b935362b979f9115dbb0453ce72d7935fb2 [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
Andrey Andreevae72dc62014-01-03 18:15:24 +020022.. contents::
23 :local:
24
25.. raw:: html
26
27 <div class="custom-index container"></div>
28
Andrey Andreevde1fe7d2014-01-20 17:06:16 +020029************************
30Using the Language Class
31************************
32
Derek Jones8ede1a22011-10-05 13:34:52 -050033Creating Language Files
34=======================
35
Andrey Andreevae72dc62014-01-03 18:15:24 +020036Language files must be named with **_lang.php** as the filename extension.
37For example, let's say you want to create a file containing error messages.
Derek Jones8ede1a22011-10-05 13:34:52 -050038You might name it: error_lang.php
39
40Within the file you will assign each line of text to an array called
Andrey Andreevb11b9f32012-11-26 23:01:24 +020041``$lang`` with this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Andrey Andreevb11b9f32012-11-26 23:01:24 +020043 $lang['language_key'] = 'The actual message to be shown';
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45.. note:: It's a good practice to use a common prefix for all messages
46 in a given file to avoid collisions with similarly named items in other
47 files. For example, if you are creating error messages you might prefix
48 them with error\_
49
50::
51
Andrey Andreevb11b9f32012-11-26 23:01:24 +020052 $lang['error_email_missing'] = 'You must submit an email address';
53 $lang['error_url_missing'] = 'You must submit a URL';
54 $lang['error_username_missing'] = 'You must submit a username';
Derek Jones8ede1a22011-10-05 13:34:52 -050055
56Loading A Language File
57=======================
58
59In order to fetch a line from a particular file you must load the file
60first. Loading a language file is done with the following code::
61
62 $this->lang->load('filename', 'language');
63
64Where filename is the name of the file you wish to load (without the
65file extension), and language is the language set containing it (ie,
66english). If the second parameter is missing, the default language set
Andrey Andreevb11b9f32012-11-26 23:01:24 +020067in your **application/config/config.php** file will be used.
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030068
69.. note:: The *language* parameter can only consist of letters.
Derek Jones8ede1a22011-10-05 13:34:52 -050070
71Fetching a Line of Text
72=======================
73
74Once your desired language file is loaded you can access any line of
75text using this function::
76
77 $this->lang->line('language_key');
78
Andrey Andreevce0c9562012-11-22 17:26:29 +020079Where *language_key* is the array key corresponding to the line you wish
Derek Jones8ede1a22011-10-05 13:34:52 -050080to show.
81
Andrey Andreevce0c9562012-11-22 17:26:29 +020082You can optionally pass FALSE as the second argument of that method to
83disable error logging, in case you're not sure if the line exists::
84
85 $this->lang->line('misc_key', FALSE);
86
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030087.. note:: This method simply returns the line. It does not echo it.
Derek Jones8ede1a22011-10-05 13:34:52 -050088
89Using language lines as form labels
90-----------------------------------
91
92This feature has been deprecated from the language library and moved to
Derek Jones123bb202013-07-19 16:37:51 -070093the :func:`lang()` function of the :doc:`Language Helper
Andrey Andreevb11b9f32012-11-26 23:01:24 +020094<../helpers/language_helper>`.
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96Auto-loading Languages
97======================
98
99If you find that you need a particular language globally throughout your
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200100application, you can tell CodeIgniter to :doc:`auto-load
101<../general/autoloader>` it during system initialization. This is done
102by opening the **application/config/autoload.php** file and adding the
Andrey Andreevae72dc62014-01-03 18:15:24 +0200103language(s) to the autoload array.
104
105***************
106Class Reference
107***************
108
109.. class:: CI_Lang
110
111 .. method:: load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]])
112
113 :param string $langfile: Language file to load
114 :param string $idiom: Language name (i.e. 'english')
115 :param bool $return: Whether to return the loaded array of translations
116 :param bool $add_suffix: Whether to add the '_lang' suffix to the language file name
117 :param string $alt_path: An alternative path to look in for the language file
118 :returns: void or array if the third parameter is set to TRUE
119
120 Loads a language file.
121
122 .. method:: line($line[, $log_errors = TRUE])
123
124 :param string $line: Language line key name
125 :param bool $log_errors: Whether to log an error if the line isn't found
126 :returns: string or FALSE on failure
127
128 Fetches a single translation line from the already loaded language files,
129 based on the line's name.