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 | |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 22 | .. contents:: |
| 23 | :local: |
| 24 | |
| 25 | .. raw:: html |
| 26 | |
| 27 | <div class="custom-index container"></div> |
| 28 | |
Andrey Andreev | de1fe7d | 2014-01-20 17:06:16 +0200 | [diff] [blame] | 29 | ************************ |
| 30 | Using the Language Class |
| 31 | ************************ |
| 32 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | Creating Language Files |
| 34 | ======================= |
| 35 | |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 36 | Language files must be named with **_lang.php** as the filename extension. |
| 37 | For example, let's say you want to create a file containing error messages. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | You might name it: error_lang.php |
| 39 | |
| 40 | 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] | 41 | ``$lang`` with this prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 43 | $lang['language_key'] = 'The actual message to be shown'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | .. note:: It's a good practice to use a common prefix for all messages |
| 46 | in a given file to avoid collisions with similarly named items in other |
| 47 | files. For example, if you are creating error messages you might prefix |
| 48 | them with error\_ |
| 49 | |
| 50 | :: |
| 51 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 52 | $lang['error_email_missing'] = 'You must submit an email address'; |
| 53 | $lang['error_url_missing'] = 'You must submit a URL'; |
| 54 | $lang['error_username_missing'] = 'You must submit a username'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | |
| 56 | Loading A Language File |
| 57 | ======================= |
| 58 | |
| 59 | In order to fetch a line from a particular file you must load the file |
| 60 | first. Loading a language file is done with the following code:: |
| 61 | |
| 62 | $this->lang->load('filename', 'language'); |
| 63 | |
| 64 | Where filename is the name of the file you wish to load (without the |
| 65 | file extension), and language is the language set containing it (ie, |
| 66 | english). If the second parameter is missing, the default language set |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 67 | in your **application/config/config.php** file will be used. |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 68 | |
| 69 | .. note:: The *language* parameter can only consist of letters. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
| 71 | Fetching a Line of Text |
| 72 | ======================= |
| 73 | |
| 74 | Once your desired language file is loaded you can access any line of |
| 75 | text using this function:: |
| 76 | |
| 77 | $this->lang->line('language_key'); |
| 78 | |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 79 | 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] | 80 | to show. |
| 81 | |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 82 | You can optionally pass FALSE as the second argument of that method to |
| 83 | disable error logging, in case you're not sure if the line exists:: |
| 84 | |
| 85 | $this->lang->line('misc_key', FALSE); |
| 86 | |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 87 | .. note:: This method simply returns the line. It does not echo it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
| 89 | Using language lines as form labels |
| 90 | ----------------------------------- |
| 91 | |
| 92 | This feature has been deprecated from the language library and moved to |
Derek Jones | 123bb20 | 2013-07-19 16:37:51 -0700 | [diff] [blame] | 93 | the :func:`lang()` function of the :doc:`Language Helper |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 94 | <../helpers/language_helper>`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
| 96 | Auto-loading Languages |
| 97 | ====================== |
| 98 | |
| 99 | If you find that you need a particular language globally throughout your |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 100 | application, you can tell CodeIgniter to :doc:`auto-load |
| 101 | <../general/autoloader>` it during system initialization. This is done |
| 102 | by opening the **application/config/autoload.php** file and adding the |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 103 | language(s) to the autoload array. |
| 104 | |
| 105 | *************** |
| 106 | Class Reference |
| 107 | *************** |
| 108 | |
| 109 | .. class:: CI_Lang |
| 110 | |
| 111 | .. method:: load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]]) |
| 112 | |
| 113 | :param string $langfile: Language file to load |
| 114 | :param string $idiom: Language name (i.e. 'english') |
| 115 | :param bool $return: Whether to return the loaded array of translations |
| 116 | :param bool $add_suffix: Whether to add the '_lang' suffix to the language file name |
| 117 | :param string $alt_path: An alternative path to look in for the language file |
| 118 | :returns: void or array if the third parameter is set to TRUE |
| 119 | |
| 120 | Loads a language file. |
| 121 | |
| 122 | .. method:: line($line[, $log_errors = TRUE]) |
| 123 | |
| 124 | :param string $line: Language line key name |
| 125 | :param bool $log_errors: Whether to log an error if the line isn't found |
| 126 | :returns: string or FALSE on failure |
| 127 | |
| 128 | Fetches a single translation line from the already loaded language files, |
| 129 | based on the line's name. |