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 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 8 | In your CodeIgniter **system** folder, you will find a **language** sub-directory |
| 9 | containing a set of language files for the **english** idiom. |
| 10 | The files in this directory (**system/language/english/**) define the regular messages, |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 11 | error messages, and other generally output terms or expressions, for the different parts |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 12 | of the CodeIgniter framework. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 14 | You can create or incorporate your own language files, as needed, in order to provide |
| 15 | application-specific error and other messages, or to provide translations of the core |
| 16 | messages into other languages. These translations or additional messages would go inside |
| 17 | your **application/language/** directory, with separate sub-directories for each idiom |
| 18 | (for instance, 'french' or 'german'). |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 19 | |
| 20 | The CodeIgniter framework comes with a set of language files for the "english" idiom. |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 21 | Additional approved translations for different idioms may be found in the |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 22 | `CodeIgniter 3 Translations repositories <https://github.com/bcit-ci/codeigniter3-translations>`_. |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 23 | Each repository deals with a single idiom. |
| 24 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 25 | When CodeIgniter loads language files, it will load the one in **system/language/** |
| 26 | first and will then look for an override in your **application/language/** directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
| 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 Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 31 | .. contents:: |
| 32 | :local: |
| 33 | |
| 34 | .. raw:: html |
| 35 | |
| 36 | <div class="custom-index container"></div> |
| 37 | |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 38 | *************************** |
| 39 | Handling Multiple Languages |
| 40 | *************************** |
| 41 | |
| 42 | If you want to support multiple languages in your application, you would provide folders inside |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 43 | your **application/language/** directory for each of them, and you would specify the default |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 44 | language in your **application/config/config.php**. |
| 45 | |
| 46 | The **application/language/english/** directory would contain any additional language files |
| 47 | needed by your application, for instance for error messages. |
| 48 | |
| 49 | Each of the other idiom-specific directories would contain the core language files that you |
| 50 | obtained from the translations repositories, or that you translated yourself, as well as |
| 51 | any additional ones needed by your application. |
| 52 | |
| 53 | You would store the language you are currently using, for instance in a session variable. |
| 54 | |
| 55 | Sample Language Files |
| 56 | ===================== |
| 57 | |
| 58 | :: |
| 59 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 60 | system/ |
| 61 | language/ |
| 62 | english/ |
| 63 | ... |
| 64 | email_lang.php |
| 65 | form_validation_lang.php |
| 66 | ... |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 67 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 68 | 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 Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 78 | |
| 79 | Example of switching languages |
| 80 | ============================== |
| 81 | |
| 82 | :: |
| 83 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 84 | $idiom = $this->session->get_userdata('language'); |
| 85 | $this->lang->load('error_messages', $idiom); |
| 86 | $oops = $this->lang->line('message_key'); |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 87 | |
| 88 | ******************** |
| 89 | Internationalization |
| 90 | ******************** |
| 91 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 92 | The Language class in CodeIgniter is meant to provide an easy and lightweight |
| 93 | way to support multiplelanguages in your application. It is not meant to be a |
| 94 | full implementation of what is commonly called `internationalization and localization |
| 95 | <http://en.wikipedia.org/wiki/Internationalization_and_localization>`_. |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 96 | |
Andrey Andreev | 0972c21 | 2014-12-04 17:00:33 +0200 | [diff] [blame] | 97 | We use the term "idiom" to refer to a language using its common name, |
| 98 | rather than using any of the international standards, such as "en", "en-US", |
| 99 | or "en-CA-x-ca" for English and some of its variants. |
James L Parry | d36f852 | 2014-11-24 00:57:25 -0800 | [diff] [blame] | 100 | |
| 101 | .. note:: There is nothing to prevent you from using those abbreviations in your application! |
| 102 | |
Andrey Andreev | de1fe7d | 2014-01-20 17:06:16 +0200 | [diff] [blame] | 103 | ************************ |
| 104 | Using the Language Class |
| 105 | ************************ |
| 106 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | Creating Language Files |
| 108 | ======================= |
| 109 | |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 110 | Language files must be named with **_lang.php** as the filename extension. |
| 111 | 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] | 112 | You might name it: error_lang.php |
| 113 | |
| 114 | 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] | 115 | ``$lang`` with this prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 117 | $lang['language_key'] = 'The actual message to be shown'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
| 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 Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 126 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
| 130 | Loading A Language File |
| 131 | ======================= |
| 132 | |
| 133 | In order to fetch a line from a particular file you must load the file |
| 134 | first. Loading a language file is done with the following code:: |
| 135 | |
| 136 | $this->lang->load('filename', 'language'); |
| 137 | |
| 138 | Where filename is the name of the file you wish to load (without the |
| 139 | file extension), and language is the language set containing it (ie, |
| 140 | english). If the second parameter is missing, the default language set |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 141 | in your **application/config/config.php** file will be used. |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 142 | |
Gabriel Potkány | 0e924ce | 2014-11-06 11:35:46 +0100 | [diff] [blame] | 143 | You 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 Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 148 | .. note:: The *language* parameter can only consist of letters. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 149 | |
| 150 | Fetching a Line of Text |
| 151 | ======================= |
| 152 | |
| 153 | Once your desired language file is loaded you can access any line of |
| 154 | text using this function:: |
| 155 | |
| 156 | $this->lang->line('language_key'); |
| 157 | |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 158 | 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] | 159 | to show. |
| 160 | |
Andrey Andreev | ce0c956 | 2012-11-22 17:26:29 +0200 | [diff] [blame] | 161 | You can optionally pass FALSE as the second argument of that method to |
| 162 | disable error logging, in case you're not sure if the line exists:: |
| 163 | |
| 164 | $this->lang->line('misc_key', FALSE); |
| 165 | |
Andrey Andreev | 2dce1ff | 2012-10-24 20:49:04 +0300 | [diff] [blame] | 166 | .. note:: This method simply returns the line. It does not echo it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
| 168 | Using language lines as form labels |
| 169 | ----------------------------------- |
| 170 | |
| 171 | This feature has been deprecated from the language library and moved to |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 172 | the :php:func:`lang()` function of the :doc:`Language Helper |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 173 | <../helpers/language_helper>`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
| 175 | Auto-loading Languages |
| 176 | ====================== |
| 177 | |
| 178 | If you find that you need a particular language globally throughout your |
Andrey Andreev | b11b9f3 | 2012-11-26 23:01:24 +0200 | [diff] [blame] | 179 | application, you can tell CodeIgniter to :doc:`auto-load |
| 180 | <../general/autoloader>` it during system initialization. This is done |
| 181 | by opening the **application/config/autoload.php** file and adding the |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 182 | language(s) to the autoload array. |
| 183 | |
| 184 | *************** |
| 185 | Class Reference |
| 186 | *************** |
| 187 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 188 | .. php:class:: CI_Lang |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 189 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 190 | .. php:method:: load($langfile[, $idiom = ''[, $return = FALSE[, $add_suffix = TRUE[, $alt_path = '']]]]) |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 191 | |
Gabriel Potkány | 2d7e058 | 2014-11-07 08:02:14 +0100 | [diff] [blame] | 192 | :param mixed $langfile: Language file to load or array with multiple files |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 193 | :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 Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 199 | |
| 200 | Loads a language file. |
| 201 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 202 | .. php:method:: line($line[, $log_errors = TRUE]) |
Andrey Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 203 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 204 | :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 Andreev | ae72dc6 | 2014-01-03 18:15:24 +0200 | [diff] [blame] | 208 | |
| 209 | Fetches a single translation line from the already loaded language files, |
| 210 | based on the line's name. |