Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############## |
| 2 | Language Class |
| 3 | ############## |
| 4 | |
| 5 | The Language Class provides functions to retrieve language files and |
| 6 | lines of text for purposes of internationalization. |
| 7 | |
| 8 | In your CodeIgniter system folder you'll find one called language |
| 9 | containing sets of language files. You can create your own language |
| 10 | files as needed in order to display error and other messages in other |
| 11 | languages. |
| 12 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 13 | Language files are typically stored in your **system/language/** directory. |
| 14 | Alternately you can create a directory called language inside your |
| 15 | application folder and store them there. CodeIgniter will always load the |
| 16 | one in **system/language/** first and will then look for an override in |
| 17 | your **application/language/** directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
| 19 | .. note:: Each language should be stored in its own folder. For example, |
| 20 | the English files are located at: system/language/english |
| 21 | |
| 22 | Creating Language Files |
| 23 | ======================= |
| 24 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 25 | Language files must be named with **_lang.php** as the file extension. For |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | example, let's say you want to create a file containing error messages. |
| 27 | You might name it: error_lang.php |
| 28 | |
| 29 | Within the file you will assign each line of text to an array called |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 30 | ``$lang`` with this prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 31 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 32 | $lang['language_key'] = 'The actual message to be shown'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
| 34 | .. note:: It's a good practice to use a common prefix for all messages |
| 35 | in a given file to avoid collisions with similarly named items in other |
| 36 | files. For example, if you are creating error messages you might prefix |
| 37 | them with error\_ |
| 38 | |
| 39 | :: |
| 40 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 41 | $lang['error_email_missing'] = 'You must submit an email address'; |
| 42 | $lang['error_url_missing'] = 'You must submit a URL'; |
| 43 | $lang['error_username_missing'] = 'You must submit a username'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | Loading A Language File |
| 46 | ======================= |
| 47 | |
| 48 | In order to fetch a line from a particular file you must load the file |
| 49 | first. Loading a language file is done with the following code:: |
| 50 | |
| 51 | $this->lang->load('filename', 'language'); |
| 52 | |
| 53 | Where filename is the name of the file you wish to load (without the |
| 54 | file extension), and language is the language set containing it (ie, |
| 55 | english). If the second parameter is missing, the default language set |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 56 | in your **application/config/config.php** file will be used. |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 57 | |
| 58 | .. note:: The *language* parameter can only consist of letters. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | |
| 60 | Fetching a Line of Text |
| 61 | ======================= |
| 62 | |
| 63 | Once your desired language file is loaded you can access any line of |
| 64 | text using this function:: |
| 65 | |
| 66 | $this->lang->line('language_key'); |
| 67 | |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 68 | Where *language_key* is the array key corresponding to the line you wish |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | to show. |
| 70 | |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 71 | You can optionally pass FALSE as the second argument of that method to |
| 72 | disable error logging, in case you're not sure if the line exists:: |
| 73 | |
| 74 | $this->lang->line('misc_key', FALSE); |
| 75 | |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 76 | .. note:: This method simply returns the line. It does not echo it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
| 78 | Using language lines as form labels |
| 79 | ----------------------------------- |
| 80 | |
| 81 | This feature has been deprecated from the language library and moved to |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 82 | the :php:func:`lang()` function of the :doc:`Language Helper |
| 83 | <../helpers/language_helper>`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | Auto-loading Languages |
| 86 | ====================== |
| 87 | |
| 88 | If you find that you need a particular language globally throughout your |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 89 | application, you can tell CodeIgniter to :doc:`auto-load |
| 90 | <../general/autoloader>` it during system initialization. This is done |
| 91 | by opening the **application/config/autoload.php** file and adding the |
| 92 | language(s) to the autoload array. |