blob: 6949c11c958872f1a5b0ce4cf645aff24d90e19f [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
Gabriel Potkány0e924ce2014-11-06 11:35:46 +010069You can also load multiple language files at the same time by passing an array of language files as first parameter.
70::
71
72 $this->lang->load(array('filename1', 'filename2'));
73
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030074.. note:: The *language* parameter can only consist of letters.
Derek Jones8ede1a22011-10-05 13:34:52 -050075
76Fetching a Line of Text
77=======================
78
79Once your desired language file is loaded you can access any line of
80text using this function::
81
82 $this->lang->line('language_key');
83
Andrey Andreevce0c9562012-11-22 17:26:29 +020084Where *language_key* is the array key corresponding to the line you wish
Derek Jones8ede1a22011-10-05 13:34:52 -050085to show.
86
Andrey Andreevce0c9562012-11-22 17:26:29 +020087You can optionally pass FALSE as the second argument of that method to
88disable error logging, in case you're not sure if the line exists::
89
90 $this->lang->line('misc_key', FALSE);
91
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030092.. note:: This method simply returns the line. It does not echo it.
Derek Jones8ede1a22011-10-05 13:34:52 -050093
94Using language lines as form labels
95-----------------------------------
96
97This feature has been deprecated from the language library and moved to
Derek Jones123bb202013-07-19 16:37:51 -070098the :func:`lang()` function of the :doc:`Language Helper
Andrey Andreevb11b9f32012-11-26 23:01:24 +020099<../helpers/language_helper>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
101Auto-loading Languages
102======================
103
104If you find that you need a particular language globally throughout your
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200105application, you can tell CodeIgniter to :doc:`auto-load
106<../general/autoloader>` it during system initialization. This is done
107by opening the **application/config/autoload.php** file and adding the
Andrey Andreevae72dc62014-01-03 18:15:24 +0200108language(s) to the autoload array.
109
110***************
111Class Reference
112***************
113
114.. class:: CI_Lang
115
116 .. method:: load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]])
117
Gabriel Potkány2d7e0582014-11-07 08:02:14 +0100118 :param mixed $langfile: Language file to load or array with multiple files
Andrey Andreev28c2c972014-02-08 04:27:48 +0200119 :param string $idiom: Language name (i.e. 'english')
120 :param bool $return: Whether to return the loaded array of translations
121 :param bool $add_suffix: Whether to add the '_lang' suffix to the language file name
122 :param string $alt_path: An alternative path to look in for the language file
123 :returns: Array of language lines if $return is set to TRUE, otherwise void
124 :rtype: mixed
Andrey Andreevae72dc62014-01-03 18:15:24 +0200125
126 Loads a language file.
127
128 .. method:: line($line[, $log_errors = TRUE])
129
Andrey Andreev28c2c972014-02-08 04:27:48 +0200130 :param string $line: Language line key name
131 :param bool $log_errors: Whether to log an error if the line isn't found
132 :returns: Language line string or FALSE on failure
133 :rtype: string
Andrey Andreevae72dc62014-01-03 18:15:24 +0200134
135 Fetches a single translation line from the already loaded language files,
136 based on the line's name.