blob: de17c82888f64f0bbbcab139b334959f2c61fda7 [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
Andrey Andreev0972c212014-12-04 17:00:33 +02008In your CodeIgniter **system** folder, you will find a **language** sub-directory
9containing a set of language files for the **english** idiom.
10The files in this directory (**system/language/english/**) define the regular messages,
James L Parryd36f8522014-11-24 00:57:25 -080011error messages, and other generally output terms or expressions, for the different parts
Andrey Andreev0972c212014-12-04 17:00:33 +020012of the CodeIgniter framework.
Derek Jones8ede1a22011-10-05 13:34:52 -050013
Andrey Andreev0972c212014-12-04 17:00:33 +020014You can create or incorporate your own language files, as needed, in order to provide
15application-specific error and other messages, or to provide translations of the core
16messages into other languages. These translations or additional messages would go inside
17your **application/language/** directory, with separate sub-directories for each idiom
18(for instance, 'french' or 'german').
James L Parryd36f8522014-11-24 00:57:25 -080019
20The CodeIgniter framework comes with a set of language files for the "english" idiom.
Andrey Andreev0972c212014-12-04 17:00:33 +020021Additional approved translations for different idioms may be found in the
Master Yodabd2a7e42015-03-25 02:36:31 -070022`CodeIgniter 3 Translations repositories <https://github.com/bcit-ci/codeigniter3-translations>`_.
James L Parryd36f8522014-11-24 00:57:25 -080023Each repository deals with a single idiom.
24
Andrey Andreev0972c212014-12-04 17:00:33 +020025When CodeIgniter loads language files, it will load the one in **system/language/**
26first and will then look for an override in your **application/language/** directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050027
28.. note:: Each language should be stored in its own folder. For example,
29 the English files are located at: system/language/english
30
Andrey Andreevae72dc62014-01-03 18:15:24 +020031.. contents::
32 :local:
33
34.. raw:: html
35
36 <div class="custom-index container"></div>
37
James L Parryd36f8522014-11-24 00:57:25 -080038***************************
39Handling Multiple Languages
40***************************
41
42If you want to support multiple languages in your application, you would provide folders inside
Andrey Andreev0972c212014-12-04 17:00:33 +020043your **application/language/** directory for each of them, and you would specify the default
James L Parryd36f8522014-11-24 00:57:25 -080044language in your **application/config/config.php**.
45
46The **application/language/english/** directory would contain any additional language files
47needed by your application, for instance for error messages.
48
49Each of the other idiom-specific directories would contain the core language files that you
50obtained from the translations repositories, or that you translated yourself, as well as
51any additional ones needed by your application.
52
53You would store the language you are currently using, for instance in a session variable.
54
55Sample Language Files
56=====================
57
58::
59
Andrey Andreev0972c212014-12-04 17:00:33 +020060 system/
61 language/
62 english/
63 ...
64 email_lang.php
65 form_validation_lang.php
66 ...
James L Parryd36f8522014-11-24 00:57:25 -080067
Andrey Andreev0972c212014-12-04 17:00:33 +020068 application/
69 language/
70 english/
71 error_messages_lang.php
72 french/
73 ...
74 email_lang.php
75 error_messages_lang.php
76 form_validation_lang.php
77 ...
James L Parryd36f8522014-11-24 00:57:25 -080078
79Example of switching languages
80==============================
81
82::
83
Andrey Andreev0972c212014-12-04 17:00:33 +020084 $idiom = $this->session->get_userdata('language');
85 $this->lang->load('error_messages', $idiom);
86 $oops = $this->lang->line('message_key');
James L Parryd36f8522014-11-24 00:57:25 -080087
88********************
89Internationalization
90********************
91
Andrey Andreev0972c212014-12-04 17:00:33 +020092The Language class in CodeIgniter is meant to provide an easy and lightweight
93way to support multiplelanguages in your application. It is not meant to be a
94full implementation of what is commonly called `internationalization and localization
95<http://en.wikipedia.org/wiki/Internationalization_and_localization>`_.
James L Parryd36f8522014-11-24 00:57:25 -080096
Andrey Andreev0972c212014-12-04 17:00:33 +020097We 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",
99or "en-CA-x-ca" for English and some of its variants.
James L Parryd36f8522014-11-24 00:57:25 -0800100
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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200172the :php: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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200188.. php:class:: CI_Lang
Andrey Andreevae72dc62014-01-03 18:15:24 +0200189
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200190 .. php:method:: load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]])
Andrey Andreevae72dc62014-01-03 18:15:24 +0200191
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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200202 .. php:method:: line($line[, $log_errors = TRUE])
Andrey Andreevae72dc62014-01-03 18:15:24 +0200203
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.