blob: c3f9b6d5a604e0bf2bb845e25ef9c4c3f7d05ea1 [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
James L Parryd36f8522014-11-24 00:57:25 -08008In your CodeIgniter **system** folder, you will find a **language**
9subfolder containing a set of language files for the **english** idiom.
10The files in this folder (**system/language/english/**) define the regular messages,
11error messages, and other generally output terms or expressions, for the different parts
12of the CodeIgniter core framework.
Derek Jones8ede1a22011-10-05 13:34:52 -050013
James L Parryd36f8522014-11-24 00:57:25 -080014You can create or incorporate your own language
15files, as needed, in order to provide application-specific error and other messages,
16or to provide translations of the core messages into other languages.
17These translations or additional messages would go inside your application/language folder,
18with separate subfolders for each idiom (for instance french or german).
19
20The CodeIgniter framework comes with a set of language files for the "english" idiom.
21Additional approved translations for different idioms may be found in the
22`CodeIgniter 3 Translations repositories <https://github.com/codeigniter3-translations>`_.
23Each repository deals with a single idiom.
24
25When CodeIgniter loads language files, it will load the
Andrey Andreevb11b9f32012-11-26 23:01:24 +020026one in **system/language/** first and will then look for an override in
27your **application/language/** directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050028
29.. note:: Each language should be stored in its own folder. For example,
30 the English files are located at: system/language/english
31
Andrey Andreevae72dc62014-01-03 18:15:24 +020032.. contents::
33 :local:
34
35.. raw:: html
36
37 <div class="custom-index container"></div>
38
James L Parryd36f8522014-11-24 00:57:25 -080039***************************
40Handling Multiple Languages
41***************************
42
43If you want to support multiple languages in your application, you would provide folders inside
44your **application/language/** directory for each of them, and you would specify the default
45language in your **application/config/config.php**.
46
47The **application/language/english/** directory would contain any additional language files
48needed by your application, for instance for error messages.
49
50Each of the other idiom-specific directories would contain the core language files that you
51obtained from the translations repositories, or that you translated yourself, as well as
52any additional ones needed by your application.
53
54You would store the language you are currently using, for instance in a session variable.
55
56Sample Language Files
57=====================
58
59::
60
61 system/
62 language/
63 english/
64 ...
65 email_lang.php
66 form_validation_lang.php
67 ...
68
69 application/
70 language/
71 english/
72 error_messages_lang.php
73 french/
74 ...
75 email_lang.php
76 error_messages_lang.php
77 form_validation_lang.php
78 ...
79
80Example of switching languages
81==============================
82
83::
84
85 $idiom = $this->session->get_userdata('language');
86 $this->lang->load('error_messages',$idiom);
87 $oops = $this->lang->line('nessage_key');
88
89********************
90Internationalization
91********************
92
93The Language class in CodeIgniter is meant to provide an easy and lightweight way to support multiple
94languages in your application. It is not meant to be a full implementation of what is commonly called
95`internationalization and localization <http://en.wikipedia.org/wiki/Internationalization_and_localization>`_.
96
97We use the term "idiom" to refer to a language using its common name,
98rather than using any of the international standards, such as "en", "en-US", or "en-CA-x-ca" for English
99and some of its variants.
100
101.. note:: There is nothing to prevent you from using those abbreviations in your application!
102
Andrey Andreevde1fe7d2014-01-20 17:06:16 +0200103************************
104Using the Language Class
105************************
106
Derek Jones8ede1a22011-10-05 13:34:52 -0500107Creating Language Files
108=======================
109
Andrey Andreevae72dc62014-01-03 18:15:24 +0200110Language files must be named with **_lang.php** as the filename extension.
111For example, let's say you want to create a file containing error messages.
Derek Jones8ede1a22011-10-05 13:34:52 -0500112You might name it: error_lang.php
113
114Within the file you will assign each line of text to an array called
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200115``$lang`` with this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200117 $lang['language_key'] = 'The actual message to be shown';
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
119.. note:: It's a good practice to use a common prefix for all messages
120 in a given file to avoid collisions with similarly named items in other
121 files. For example, if you are creating error messages you might prefix
122 them with error\_
123
124::
125
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200126 $lang['error_email_missing'] = 'You must submit an email address';
127 $lang['error_url_missing'] = 'You must submit a URL';
128 $lang['error_username_missing'] = 'You must submit a username';
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130Loading A Language File
131=======================
132
133In order to fetch a line from a particular file you must load the file
134first. Loading a language file is done with the following code::
135
136 $this->lang->load('filename', 'language');
137
138Where filename is the name of the file you wish to load (without the
139file extension), and language is the language set containing it (ie,
140english). If the second parameter is missing, the default language set
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200141in your **application/config/config.php** file will be used.
Andrey Andreev2dce1ff2012-10-24 20:49:04 +0300142
Gabriel Potkány0e924ce2014-11-06 11:35:46 +0100143You can also load multiple language files at the same time by passing an array of language files as first parameter.
144::
145
146 $this->lang->load(array('filename1', 'filename2'));
147
Andrey Andreev2dce1ff2012-10-24 20:49:04 +0300148.. note:: The *language* parameter can only consist of letters.
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
150Fetching a Line of Text
151=======================
152
153Once your desired language file is loaded you can access any line of
154text using this function::
155
156 $this->lang->line('language_key');
157
Andrey Andreevce0c9562012-11-22 17:26:29 +0200158Where *language_key* is the array key corresponding to the line you wish
Derek Jones8ede1a22011-10-05 13:34:52 -0500159to show.
160
Andrey Andreevce0c9562012-11-22 17:26:29 +0200161You can optionally pass FALSE as the second argument of that method to
162disable error logging, in case you're not sure if the line exists::
163
164 $this->lang->line('misc_key', FALSE);
165
Andrey Andreev2dce1ff2012-10-24 20:49:04 +0300166.. note:: This method simply returns the line. It does not echo it.
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
168Using language lines as form labels
169-----------------------------------
170
171This feature has been deprecated from the language library and moved to
Derek Jones123bb202013-07-19 16:37:51 -0700172the :func:`lang()` function of the :doc:`Language Helper
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200173<../helpers/language_helper>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
175Auto-loading Languages
176======================
177
178If you find that you need a particular language globally throughout your
Andrey Andreevb11b9f32012-11-26 23:01:24 +0200179application, you can tell CodeIgniter to :doc:`auto-load
180<../general/autoloader>` it during system initialization. This is done
181by opening the **application/config/autoload.php** file and adding the
Andrey Andreevae72dc62014-01-03 18:15:24 +0200182language(s) to the autoload array.
183
184***************
185Class Reference
186***************
187
188.. class:: CI_Lang
189
190 .. method:: load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]])
191
Gabriel Potkány2d7e0582014-11-07 08:02:14 +0100192 :param mixed $langfile: Language file to load or array with multiple files
Andrey Andreev28c2c972014-02-08 04:27:48 +0200193 :param string $idiom: Language name (i.e. 'english')
194 :param bool $return: Whether to return the loaded array of translations
195 :param bool $add_suffix: Whether to add the '_lang' suffix to the language file name
196 :param string $alt_path: An alternative path to look in for the language file
197 :returns: Array of language lines if $return is set to TRUE, otherwise void
198 :rtype: mixed
Andrey Andreevae72dc62014-01-03 18:15:24 +0200199
200 Loads a language file.
201
202 .. method:: line($line[, $log_errors = TRUE])
203
Andrey Andreev28c2c972014-02-08 04:27:48 +0200204 :param string $line: Language line key name
205 :param bool $log_errors: Whether to log an error if the line isn't found
206 :returns: Language line string or FALSE on failure
207 :rtype: string
Andrey Andreevae72dc62014-01-03 18:15:24 +0200208
209 Fetches a single translation line from the already loaded language files,
210 based on the line's name.