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