load a lang file into an array |
Hi!
Anyone who knows how I can populate an array with key/values from a lang file WITHOUT loading any from system first? I tried this: PHP Code: $this->lang->load('some_file_lang','english'); If I var_dump $this->lang->language I will see an 'array' starting with the key/values from the system files, and then the ones from some_file_lang above. The system key/values are already the docs says. Any way to get an array from my own file only?
Workaround:
open the lang file $string = file_get_contents($file); $lang_file_rows = array(); $lang_file_rows = preg_split("/\\r\\n|\\r|\\n/", $string); Loop through the array of rows and check if the row begins with $lang.... if so you explode each of this lines with "=" and save it into another array like $lang_file_variables But before that you need to trim the key and the value as follows: Run a rtrim on the key element, then remove the frist 7 ( $lang[' ) and the last two charaters ( '] ) Then run a ltrim on the value element, them remove the first character ( ' ) and the two last ( '; ) Now you have created an array from a lang file.
Why would you want to take an array and put it in a new array?
If the lang file is loaded and you know the key name al you need todo to get the value is PHP Code: $language_value = $this->lang->line('language_key'); OR use the helper PHP Code: $this->load->helper('language'); Info here: Language documentation CI 3.x
Because when you do it like that CodeIgniter has already loaded its own lang file from the System into that array.
I just noticed something, you can pass more than two arguments when loading a language file
If you pass TRUE as the third argument, the loaded array is returned. So you don't need to use that loop you created. PHP Code: $your_language_array = $this->lang->load('some_file_lang','english', TRUE); $your_language_array should be populated with the key=>value pairs of the loaded language file
|
Welcome Guest, Not a member yet? Register Sign In |