blob: 3cee5d7f681e7cc7c62a7196b0dd9b2942b71f2a [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
Derek Jones8ede1a22011-10-05 13:34:52 -050029Creating Language Files
30=======================
31
Andrey Andreevae72dc62014-01-03 18:15:24 +020032Language files must be named with **_lang.php** as the filename extension.
33For example, let's say you want to create a file containing error messages.
Derek Jones8ede1a22011-10-05 13:34:52 -050034You might name it: error_lang.php
35
36Within the file you will assign each line of text to an array called
Andrey Andreevb11b9f32012-11-26 23:01:24 +020037``$lang`` with this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Andrey Andreevb11b9f32012-11-26 23:01:24 +020039 $lang['language_key'] = 'The actual message to be shown';
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41.. note:: It's a good practice to use a common prefix for all messages
42 in a given file to avoid collisions with similarly named items in other
43 files. For example, if you are creating error messages you might prefix
44 them with error\_
45
46::
47
Andrey Andreevb11b9f32012-11-26 23:01:24 +020048 $lang['error_email_missing'] = 'You must submit an email address';
49 $lang['error_url_missing'] = 'You must submit a URL';
50 $lang['error_username_missing'] = 'You must submit a username';
Derek Jones8ede1a22011-10-05 13:34:52 -050051
52Loading A Language File
53=======================
54
55In order to fetch a line from a particular file you must load the file
56first. Loading a language file is done with the following code::
57
58 $this->lang->load('filename', 'language');
59
60Where filename is the name of the file you wish to load (without the
61file extension), and language is the language set containing it (ie,
62english). If the second parameter is missing, the default language set
Andrey Andreevb11b9f32012-11-26 23:01:24 +020063in your **application/config/config.php** file will be used.
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030064
65.. note:: The *language* parameter can only consist of letters.
Derek Jones8ede1a22011-10-05 13:34:52 -050066
67Fetching a Line of Text
68=======================
69
70Once your desired language file is loaded you can access any line of
71text using this function::
72
73 $this->lang->line('language_key');
74
Andrey Andreevce0c9562012-11-22 17:26:29 +020075Where *language_key* is the array key corresponding to the line you wish
Derek Jones8ede1a22011-10-05 13:34:52 -050076to show.
77
Andrey Andreevce0c9562012-11-22 17:26:29 +020078You can optionally pass FALSE as the second argument of that method to
79disable error logging, in case you're not sure if the line exists::
80
81 $this->lang->line('misc_key', FALSE);
82
Andrey Andreev2dce1ff2012-10-24 20:49:04 +030083.. note:: This method simply returns the line. It does not echo it.
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85Using language lines as form labels
86-----------------------------------
87
88This feature has been deprecated from the language library and moved to
Derek Jones123bb202013-07-19 16:37:51 -070089the :func:`lang()` function of the :doc:`Language Helper
Andrey Andreevb11b9f32012-11-26 23:01:24 +020090<../helpers/language_helper>`.
Derek Jones8ede1a22011-10-05 13:34:52 -050091
92Auto-loading Languages
93======================
94
95If you find that you need a particular language globally throughout your
Andrey Andreevb11b9f32012-11-26 23:01:24 +020096application, you can tell CodeIgniter to :doc:`auto-load
97<../general/autoloader>` it during system initialization. This is done
98by opening the **application/config/autoload.php** file and adding the
Andrey Andreevae72dc62014-01-03 18:15:24 +020099language(s) to the autoload array.
100
101***************
102Class Reference
103***************
104
105.. class:: CI_Lang
106
107 .. method:: load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]])
108
109 :param string $langfile: Language file to load
110 :param string $idiom: Language name (i.e. 'english')
111 :param bool $return: Whether to return the loaded array of translations
112 :param bool $add_suffix: Whether to add the '_lang' suffix to the language file name
113 :param string $alt_path: An alternative path to look in for the language file
114 :returns: void or array if the third parameter is set to TRUE
115
116 Loads a language file.
117
118 .. method:: line($line[, $log_errors = TRUE])
119
120 :param string $line: Language line key name
121 :param bool $log_errors: Whether to log an error if the line isn't found
122 :returns: string or FALSE on failure
123
124 Fetches a single translation line from the already loaded language files,
125 based on the line's name.