![]() |
load a lang file into an array - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: load a lang file into an array (/showthread.php?tid=67700) |
load a lang file into an array - muuucho - 03-28-2017 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? RE: load a lang file into an array - muuucho - 04-04-2017 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. RE: load a lang file into an array - Martin7483 - 04-04-2017 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 RE: load a lang file into an array - muuucho - 04-04-2017 Because when you do it like that CodeIgniter has already loaded its own lang file from the System into that array. RE: load a lang file into an array - Martin7483 - 04-05-2017 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 RE: load a lang file into an array - muuucho - 04-06-2017 Thanks Martin! That made it a lot more easy. +1 RE: load a lang file into an array - Martin7483 - 04-06-2017 (04-06-2017, 05:50 AM)muuucho Wrote: Thanks Martin! That made it a lot more easy. +1 You are welcome. I also keep learning new CI secrets when helping others out |